이전에는 트랜잭션을 처리 한 getTransaction() 및 commitTransaction() 메소드가있는 BaseDAO를 사용했습니다.Spring - @Transactional이 트랜잭션을 시작하지 않습니다.
package services;
import daos.interfaces.InterfaceEmployerDAO;
import dtos.EmployerDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import services.interfaces.InterfaceEmployerService;
import transformers.interfaces.InterfaceEmployerTransformer;
import java.util.List;
public class EmployerService implements InterfaceEmployerService {
@Autowired
private InterfaceEmployerDAO employerDAO;
@Autowired
private InterfaceEmployerTransformer employerTransformer;
@Override
@Transactional
public List<EmployerDTO> getAllEmployers() {
return employerTransformer.listToDTO(employerDAO.getAllEmployers());
}
(methods irrevelant at this moment)
}
그리고 <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
이 applicationContext.xml에 추가되었습니다 : 나는 게으른 로딩 @OneToMany
관계를 추가 할 때, 나는이 오류 등 어떤 세션과 관련된했다 그래서 난 내 EmployerService 방법에 @Transactional
주석을 사용하기로 결정 파일 (나는 봄 주석의 초보자 그리고 난이 문제를 해결하는 것이 중요 할 수 있는지 모르기 때문에 나는 혼란을 드려 죄송합니다, 모든 붙여 넣기) :
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Data Source Declaration -->
<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/postgres" />
<property name="user" value="postgres" />
<property name="password" value="postgres" />
<property name="maxPoolSize" value="10" />
<property name="maxStatements" value="0" />
<property name="minPoolSize" value="5" />
</bean>
<!-- Session Factory Declaration -->
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="annotatedClasses">
<list>
<value>models.Employee</value>
<value>models.Employer</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.connection.driver_class">org.postgresql.Driver</prop>
<prop key="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</prop>
<prop key="hibernate.connection.username">postgres</prop>
<prop key="hibernate.connection.password">postgres</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.search.default.directory_provider">filesystem</prop>
<prop key="hibernate.search.default.indexBase">target/luceneIndex</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory"></property>
</bean>
<bean class="services.EmployeeService"></bean>
<bean class="services.EmployerService"></bean>
<bean class="daos.EmployeeDAO"></bean>
<bean class="daos.EmployerDAO"></bean>
<bean class="transformers.EmployeeTransformer"></bean>
<bean class="transformers.EmployerTransformer"></bean>
<context:annotation-config/>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<context:component-scan base-package="controllers" />
<mvc:annotation-driven/>
</beans>
가 나는 것을 생각 HibernateTransactionManager
및 tx:annotation-driven
이 작동했지만 잘못되었습니다 - **createCriteria is not valid without active transaction**
을 daos.EmployerDAO.getAllEmployers
방법으로 받았습니다. transactionManager
을 제대로 구성 할 수 없다고 확신합니다. 아무도 나를 도우 려하지 않으면 매우 기쁠 것입니다. 미리 감사드립니다.
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
다시 한 번 감사드립니다 어떤 도움 : 가 나는 또한 MVC-디스패처-servlet.xml에 제시, 어쩌면 뭔가 문제가있다.
해결책은입니다. 내 대답은 아래를 참조하십시오.