【问题标题】:Spring CrudRepository findOne(id) method throwing org.hibernate.LazyInitializationException: could not initialize proxy - no SessionSpring CrudRepository findOne(id) 方法抛出 org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话
【发布时间】:2013-08-14 22:21:15
【问题描述】:

我有一个 Employee 实体,我可以在其中正确检索描述和名称,但它在尝试检索集合时失败。我已将获取类型设置为渴望。

我的控制器中有以下代码:

Employee emp = employeeRepository.findOne(id);
emp.getName()
emp.getDescription();
emp.getProjects() // throws exception on this line

这是我的员工实体

@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>();

我不确定会话为什么不存在。

【问题讨论】:

    标签: spring hibernate spring-data spring-data-jpa spring-orm


    【解决方案1】:

    据我所知,ElementCollection 仅适用于基本类型。

    文档说:

    在某些情况下,您不需要关联两个实体,只需 创建基本类型或可嵌入对象的集合。使用 @ElementCollection 用于这种情况。

    在这种情况下,您需要使用传统的 OneToMany 关系。这是一个很好的教程:http://viralpatel.net/blogs/hibernate-one-to-one-mapping-tutorial-using-annotation/

    【讨论】:

      猜你喜欢
      • 2016-07-29
      • 2011-12-01
      • 2014-04-21
      • 2019-05-10
      • 1970-01-01
      • 2015-11-09
      • 2021-08-10
      • 1970-01-01
      • 2013-10-22
      相关资源
      最近更新 更多