欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

failed to lazily initialize a collection of role 的问题

程序员文章站 2022-04-11 16:51:16
...

最近遇到这样的问题 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;
	}