我正在使用hibernate和spring来构建一个应用程序。我有很多实体,因此我实现了一个通用 DAO、一些扩展这个通用 DAO 的 DAO 和一些使用这些 DAO 的服务。
public class GenericDAOImpl<T, PK extends Serializable> implements IGenericDAO<T, PK> {
private SessionFactory sessionFactory;
private Class<T> type;
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
public Session getSession() {
return sessionFactory.getCurrentSession();
}
public GenericDAOImpl(Class<T> type) {
this.type = type;
}
public void create(T o) {
getSession().save(o);
}
@SuppressWarnings("unchecked")
public T read(PK id) {
return (T) getSession().get(type, id);
}
public void update(T o) {
getSession().update(o);
}
}
这是我的地区 DAO,我也有一个国家 DAO。
public class PaysDAOImpl extends GenericDAOImpl<Pays, String> implements IPaysDAO {
public PaysDAOImpl(Class<Pays> type) {
super(type);
}
public void deleteRegion(Pays region) {
// TODO Auto-generated method stub
}
}
这是我的服务类:
public class RegionServiceImpl implements IRegionService {
private IRegionDAO regionDAO;
public void setRegionDAO(IRegionDAO regionDAO) {
this.regionDAO = regionDAO;
}
public void saveRegion(Region region) {
regionDAO.create(region);
}
public void deleteRegion(Region region) {
// TODO Auto-generated method stub
}
}
最后是我的主要课程:
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
IRegionService regionBO=(IRegionService)appContext.getBean("myRegionService");
IPaysService paysBO=(IPaysService)appContext.getBean("myPaysService");
Region r=new Region();
r.setNom("EUROPE");
Pays p=new Pays();
p.setNom("Belgique");
p.setNomRegion(r);
r.getLesPays().add(p);
regionBO.saveRegion(r);
paysBO.savePays(p);
应用程序上下文文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="baselineDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/baseline" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="baselineSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="baselineDataSource" />
<property name="annotatedClasses">
<list>
<value>domain.entites.Region </value>
<value>domain.entites.Pays </value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="current_session_context_class">thread</prop>
</props>
</property>
</bean>
<!-- Hibernate Transaction Manager Definition -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="baselineSessionFactory" />
</bean>
<!-- ========================= Start of DAO DEFINITIONS ========================= -->
<!-- proxy for DAO using generic DAO -->
<bean id="myGenericDAO" abstract="true">
<property name="sessionFactory" ref="baselineSessionFactory" />
</bean>
<!-- Region DAO Definition -->
<bean id="myRegionDAO" class="dao.impl.RegionDAOImpl" parent="myGenericDAO">
<constructor-arg value="domain.entites.Region" />
</bean>
<!-- Region DAO Definition -->
<bean id="myPaysDAO" class="dao.impl.PaysDAOImpl" parent="myGenericDAO">
<constructor-arg value="domain.entites.Pays" />
</bean>
<!-- ========================= Start of SERVICE DEFINITIONS ========================= -->
<!-- Transactional proxy for Services -->
<bean id="proxyService"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED, readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED, readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED, -java.lang.Exception</prop>
</props>
</property>
</bean>
<!-- autoproxy -->
<bean id="transactionBeanNameProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*Service</value>
</property>
<property name="interceptorNames">
<list>
<value>proxyService</value>
</list>
</property>
</bean>
<bean id="myRegionService" class="domain.services.impl.RegionServiceImpl">
<property name="regionDAO" ref="myRegionDAO" />
</bean>
<bean id="myPaysService" class="domain.services.impl.PaysServiceImpl">
<property name="paysDAO" ref="myPaysDAO" />
</bean>
我的问题是,当我运行我的主类并插入一个区域时,然后在一个引用该区域的国家/地区之后,会创建两个会话并使用额外的查询,如您在此处看到的:
Hibernate: insert into REGIONS (nom_region) values (?)
Hibernate: select region_.nom_region from REGIONS region_ where region_.nom_region=?
Hibernate: insert into PAYS (nom_region, nom_pays) values (?, ?)
当我查看日志时,我看到为插入区域打开了第一个会话,然后关闭并打开了第二个会话以插入国家/地区。我想为我的所有交易使用一个会话。也许你能帮助我