问题描述
我正在尝试使用休眠上下文会话来实现通用DAO。以下是我的镜头:|
import java.io.Serializable;
public interface GenericDao< T,ID extends Serializable> {
/ **将newInstance对象存入数据库* /
ID create(T newInstance);
$ b $ / **
*使用
*指示的ID作为主键检索以前持久保存到数据库的对象
* /
T读取(ID primaryKey);
/ **保存对持久对象所做的更改。 * /
void update(T transientObject);
**从数据库中的永久性存储中移除一个对象* /
void delete(T persistentObject);
}
import java.io.Serializable;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.transaction.annotation.Transactional;
@Transactional
@SuppressWarnings(unchecked)
public class GenericDaoImpl< T,ID extends Serializable>实现
GenericDao< T,ID> {
private SessionFactory sessionFactory;
public void setSessionFactory(final SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
@Override
public ID create(final T newInstance){
ID id = null;
final Session session = sessionFactory.openSession();
final Transaction tx = session.beginTransaction();
尝试{
id =(ID)session.save(newInstance);
tx.commit();
session.close();
} catch(final Exception e){
if(tx!= null){
tx.rollback();
}
e.printStackTrace();
} finally {
if(session.isOpen()){
session.close();
}
}
return id;
}
@Override
public T read(final ID primaryKey){
T id = null;
final Session session = sessionFactory.openSession();
final Transaction tx = session.beginTransaction();
尝试{
id =(T)session.get(T,primaryKey);
tx.commit();
session.close();
} catch(final Exception e){
if(tx!= null){
tx.rollback();
}
e.printStackTrace();
} finally {
if(session.isOpen()){
session.close();
}
}
return id;
}
@Override
public void update(final T transientObject){
final Session session = sessionFactory.openSession();
final Transaction tx = session.beginTransaction();
尝试{
session.saveOrUpdate(transientObject);
tx.commit();
session.close();
} catch(final Exception e){
if(tx!= null){
tx.rollback();
}
e.printStackTrace();
} finally {
if(session.isOpen()){
session.close();
$ b @Override
public void delete(final T persistentObject){
final Session session = sessionFactory.openSession( );
final Transaction tx = session.beginTransaction();
尝试{
session.delete(persistentObject);
tx.commit();
session.close();
} catch(final Exception e){
if(tx!= null){
tx.rollback();
}
e.printStackTrace();
} finally {
if(session.isOpen()){
session.close();
$ b code
$ b applicationContext :
< bean id =domainDaoclass =com.foo.dao.DomainDao>
< property name =sessionFactory>
< ref bean =sessionFactory>< / ref>
< / property>
< / bean>
< bean id =domainDao2class =com.foo.dao.GenericDaoImpl>
< property name =sessionFactory>
< ref bean =sessionFactory>< / ref>
< / property>
< / bean>
< tx:annotation-driven transaction-manager =txManager/>
< bean id =txManager
class =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
Ours是一个新的应用程序,我们试图使用Spring 3.0.3和Hibernate 3.5.5 。
虽然我确实实施了它并且正在开展工作,但是我是否以正确的方式进行了操作?
Q2。我如何使用泛型实现 find()
操作?
id = (T)session.get(T,primaryKey);
这一行提供了编译错误。
UPDATE :错误是因为第一个参数的类型为 Class
。
public Object get(Class clazz,Serializable id)
throws HibernateException
Q3。如何将 T
转换为 T.class
?
解决方案以下技巧通常用于通用DAO类来访问实际子类的类型参数:
public abstract class GenericDAO< T,ID extends Serializable> {
私人课程< T>对PersistentClass;
...
$ b @SuppressWarnings(unchecked)
public GenericDAO(){
this.persistentClass =(Class< T>)((ParameterizedType)getClass ().getGenericSuperclass())getActualTypeArguments()[0];
}
public T get(ID id){
return(T)session.get(persistentClass,id);
}
...
}
和实际的DAO子类:
public class FooDAO extends GenericDAO< Foo,Long> {}
I'm trying to implement a Generic DAO using the Hibernates Context Sessions. Following was my shot:|
import java.io.Serializable;
public interface GenericDao<T, ID extends Serializable> {
/** Persist the newInstance object into database */
ID create(T newInstance);
/**
* Retrieve an object that was previously persisted to the database using
* the indicated id as primary key
*/
T read(ID primaryKey);
/** Save changes made to a persistent object. */
void update(T transientObject);
/** Remove an object from persistent storage in the database */
void delete(T persistentObject);
}
import java.io.Serializable;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.transaction.annotation.Transactional;
@Transactional
@SuppressWarnings("unchecked")
public class GenericDaoImpl<T, ID extends Serializable> implements
GenericDao<T, ID> {
private SessionFactory sessionFactory;
public void setSessionFactory(final SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public ID create(final T newInstance) {
ID id = null;
final Session session = sessionFactory.openSession();
final Transaction tx = session.beginTransaction();
try {
id = (ID) session.save(newInstance);
tx.commit();
session.close();
} catch (final Exception e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
if (session.isOpen()) {
session.close();
}
}
return id;
}
@Override
public T read(final ID primaryKey) {
T id = null;
final Session session = sessionFactory.openSession();
final Transaction tx = session.beginTransaction();
try {
id = (T) session.get(T, primaryKey);
tx.commit();
session.close();
} catch (final Exception e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
if (session.isOpen()) {
session.close();
}
}
return id;
}
@Override
public void update(final T transientObject) {
final Session session = sessionFactory.openSession();
final Transaction tx = session.beginTransaction();
try {
session.saveOrUpdate(transientObject);
tx.commit();
session.close();
} catch (final Exception e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
if (session.isOpen()) {
session.close();
}
}
}
@Override
public void delete(final T persistentObject) {
final Session session = sessionFactory.openSession();
final Transaction tx = session.beginTransaction();
try {
session.delete(persistentObject);
tx.commit();
session.close();
} catch (final Exception e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
if (session.isOpen()) {
session.close();
}
}
}
}
applicationContext:
<bean id="domainDao" class="com.foo.dao.DomainDao">
<property name="sessionFactory">
<ref bean="sessionFactory"></ref>
</property>
</bean>
<bean id="domainDao2" class="com.foo.dao.GenericDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"></ref>
</property>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
Ours is a new application which we are trying to implement using Spring 3.0.3 and Hibernate 3.5.5.
Q1. Although I did implement it and is working, did I do in the correct way?
Q2. How can I implement find()
operation using generics?
id = (T) session.get(T, primaryKey);
This line is giving compilation error.
UPDATE: The error is because the first param is of type Class
.
public Object get(Class clazz, Serializable id)
throws HibernateException
Q3. How to convert T
to T.class
?
解决方案 The following trick is often used in generic DAO classes to access type parameters of actual subclasses:
public abstract class GenericDAO<T, ID extends Serializable> {
private Class<T> persistentClass;
...
@SuppressWarnings("unchecked")
public GenericDAO() {
this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public T get(ID id) {
return (T) session.get(persistentClass, id);
}
...
}
And the actual DAO subclass:
public class FooDAO extends GenericDAO<Foo, Long> {}
这篇关于如何使用Hibernate Context会话创建通用DAO类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!