【发布时间】:2016-02-17 17:06:50
【问题描述】:
我正在使用带有事务的 Spring Data JPA 开发应用程序。虽然我使用的 Spring 版本(4.0.0)可以使用 JavaConfig,但我更喜欢坚持使用 XML。
我有这个配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<jpa:repositories base-package="repo"/>
<context:component-scan base-package="service" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/staffing?transformedBitIsBoolean=true"/>
<property name="username" value="root"/>
<property name="password" value="vivupdip1`"/>
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="database" value="MYSQL"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="packagesToScan" value="model"/>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.hbm2ddl.auto" value="validate"/>
<entry key="hibernate.format_sql" value="true" />
</map>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
我的单个(到目前为止)存储库是这样的:
package repo;
import org.springframework.data.jpa.repository.JpaRepository;
import model.Volunteer;
public interface VolunteerRepo extends JpaRepository<Volunteer, Integer> {
}
service 包中还有一个 Service 接口:
public interface VolunteerService {
public List<Volunteer> findAll();
}
和实施:
@Service
@Transactional
public class VolunteerServiceImpl implements VolunteerService {
@Autowired VolunteerRepo repo;
public List<Volunteer> findAll() {
return repo.findAll();
}
}
从controller 包中的Controller 调用:
@RestController
public class VolunteerController {
@Autowired VolunteerService vs;
@RequestMapping(value = "/volunteers")
List<Volunteer> getVolunteers() {
return vs.findAll();
}
}
Volunteer 域对象非常复杂,并且与其他各种域对象相关。
当我在浏览器中发出正确的请求时,我得到以下异常:
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: model.Volunteer.volunteerSessions, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->model.Volunteer["sessions"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: model.Volunteer.volunteerSessions, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->model.Volunteer["sessions"])
据我所知,它正在抱怨没有会话,(我认为)可以通过正确配置事务来解决。但是,我配置事务的尝试似乎不正确,但我不知道为什么。
【问题讨论】:
-
您是否将
volunteerSessions实例字段映射为oneToMany,并在Volunteer类中延迟加载。如果是这样,请启用 EAGER 加载并尝试。
标签: java spring hibernate jpa spring-transactions