我已经收到此错误的多对多关系使用懒惰的评价时,所有的时间:
LazyInitializationException异常:未能初始化懒洋洋角色的集合,无法初始化代理 - 没有会话
我知道,使用预先抓取类型将正常工作,但是这将是一个头痛和表现滞后于应用程序,因此,我不得不继续考虑懒获取类型。
我读了使用Spring JPA的数据时, @Transactional
将初始化懒惰的关系,但它似乎并没有采取与我的影响。
这里是我的应用程序看起来怎么样:
实体
当然,实体
@Entity
@Table(name = "course")
@Transactional
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "course_name")
private String courseName;
@Column(name = "course_description")
private String courseDescription;
@ManyToMany(cascade = {CascadeType.MERGE,CascadeType.DETACH,CascadeType.REFRESH,CascadeType.PERSIST})
@JoinTable(
name = "course_student",
joinColumns = @JoinColumn(name = "course_id"),
inverseJoinColumns = @JoinColumn(name = "student_id") )
private List<Student> students;
学生实体
@Entity
@Table(name = "students")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String grade;
@ManyToMany(cascade = {CascadeType.MERGE,CascadeType.DETACH,CascadeType.REFRESH,CascadeType.PERSIST},fetch = FetchType.EAGER)
@JoinTable(
name = "course_student",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "course_id") )
List<Course> courses;
调节器
@Controller
@RequestMapping("/courses")
@Transactional
public class CourseController {
....
@GetMapping("get-joined-students")
ModelAndView getJoined() {
Course course = courseRepo.findById(6).get();
for (Student student :
course.getStudents()) {
System.out.println("Student: " + student);
}
ModelAndView modelAndView = new ModelAndView("redirect:list");
return modelAndView;
}
}
库
课程资源库
@Repository
public interface CourseRepo extends JpaRepository<Course,Integer> {}
学生信息库
@Repository
public interface StudentRepo extends JpaRepository<Student,Integer> { }
Servlet的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" .... >
<context:annotation-config/>
<context:component-scan base-package="com.company.springdemo"/>
<mvc:annotation-driven/>
....
<jpa:repositories transaction-manager-ref="transactionManagerJPA" base-package="com.company.springdemo" entity-manager-factory-ref="EMF" />
<bean id="transactionManagerJPA" class="org.springframework.orm.jpa.JpaTransactionManager" >
<property name="dataSource" ref="myDataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManagerJPA" />
<bean id="EMF"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="com.company.springdemo.entity" />
<property name="dataSource" ref="myDataSource" />
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
</property>
</bean>
什么可能会被我做错了?