最近遇到这样的问题 failed to lazily initialize a collection of role
代码如下
//多对多定义 @ManyToMany @JoinTable( name = "acct_user_group", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "group_id") }) //Fecth策略定义 @Fetch(FetchMode.SUBSELECT) //集合按id排序. @OrderBy("id") //集合中对象id的缓存. @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public List<Group> getGroupList() { return groupList; }
网上查了很多文章,都说是因为懒加载的问题。所以改成
//多对多定义 @ManyToMany(fetch=FetchType.EAGER) @JoinTable( name = "acct_user_group", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "group_id") }) //Fecth策略定义 @Fetch(FetchMode.SUBSELECT) //集合按id排序. @OrderBy("id") //集合中对象id的缓存. @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public List<Group> getGroupList() { return groupList; }
问题还是没有解决,最后吧@Fetch(FetchMode.SUBSELECT)注释掉就解决了问题
//多对多定义 @ManyToMany(fetch=FetchType.EAGER) @JoinTable( name = "acct_user_group", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "group_id") }) //Fecth策略定义 //@Fetch(FetchMode.SUBSELECT) //集合按id排序. @OrderBy("id") //集合中对象id的缓存. @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public List<Group> getGroupList() { return groupList; }