gpt4 book ai didi

java - 如何使用 Hibernate Context session 创建通用 DAO 类

转载 作者:行者123 更新时间:2023-12-01 16:40:48 25 4
gpt4 key购买 nike

我正在尝试使用 Hibernates 上下文 session 来实现通用 DAO。以下是我的镜头:|

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();
}
}
}
}

应用程序上下文:

<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>

我们的应用程序是一个新的应用程序,我们正在尝试使用 Spring 3.0.3 和 Hibernate 3.5.5 来实现。

Q1。虽然我确实实现了它并且正在工作,但我的方法正确吗?

第二季度。如何使用泛型实现 find() 操作?

id = (T) session.get(T, primaryKey);

这一行给出了编译错误。

更新:错误是因为第一个参数的类型为Class

public Object get(Class clazz, Serializable id)
throws HibernateException

第三季度。如何将T转换为T.class

最佳答案

以下技巧经常在通用 DAO 类中使用来访问实际子类的类型参数:

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);
}

...
}

以及实际的 DAO 子类:

public class FooDAO extends GenericDAO<Foo, Long> {}

关于java - 如何使用 Hibernate Context session 创建通用 DAO 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3573479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com