[英]Spring CrudRepository findOne(id) method throwing org.hibernate.LazyInitializationException: could not initialize proxy - no Session
I have an Employee entity where i am able to retrieve description and name correctly but its failing when it tries to retrieve a collection. 我有一个Employee实体,可以在其中正确检索描述和名称,但是在尝试检索集合时失败。 I have set fetch type as eager.
我已将获取类型设置为渴望。
I have following code in my controller: 我的控制器中有以下代码:
Employee emp = employeeRepository.findOne(id);
emp.getName()
emp.getDescription();
emp.getProjects() // throws exception on this line
Here is my Employee entity 这是我的员工实体
@Entity
public class Employee {
....
/** The name. */
@NotNull
@Size(max = 30)
private String name;
/** The description. */
@NotNull
@Size(max = 250)
private String description;
....
@ElementCollection
@CollectionTable(name = "Projects", joinColumns = @JoinColumn(name = "emp_ID"))
@Basic(fetch = FetchType.EAGER)
private Set<Project> projects = new HashSet<Project>();
I am not sure why the session ceases to exist. 我不确定为什么该届会议不复存在。
As far as I know, ElementCollection only works for basic types. 据我所知,ElementCollection仅适用于基本类型。
The documentation says: 该文件说:
In some situations you don't need to associate two entities but simply create a collection of basic types or embeddable objects.
在某些情况下,您不需要关联两个实体,而只需创建基本类型或可嵌入对象的集合。 Use the @ElementCollection for this case.
在这种情况下,请使用@ElementCollection。
In this case you need to use a traditional OneToMany relationship. 在这种情况下,您需要使用传统的OneToMany关系。 Here is a good tutorial: http://viralpatel.net/blogs/hibernate-one-to-one-mapping-tutorial-using-annotation/
这是一个很好的教程: http : //viralpatel.net/blogs/hibernate-one-to-one-mapping-tutorial-using-annotation/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.