您的位置:首页 > 文旅 > 美景 > failed to lazily initialize a collection of role,解决Hibernate查询报错

failed to lazily initialize a collection of role,解决Hibernate查询报错

2024/10/7 2:19:58 来源:https://blog.csdn.net/weixin_44853310/article/details/140186160  浏览:    关键词:failed to lazily initialize a collection of role,解决Hibernate查询报错

Hibernate报错:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.jiuqi.gov.common.attatchment.entity.AttachmentEntity.properties, could not initialize proxy - no Session
        at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:614) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
        at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:218) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
        at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:162) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
        at org.hibernate.collection.internal.PersistentMap.size(PersistentMap.java:148) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
        at java.util.HashMap.putMapEntries(HashMap.java:502) ~[?:1.8.0_381]
        at java.util.HashMap.<init>(HashMap.java:491) ~[?:1.8.0_381]
        at com.jiuqi.gov.common.attatchment.entity.AttachmentEntity.toAttachmentInfo(AttachmentEntity.java:293) ~[common.attachment-1.5.1.jar:?]

原代码:

public List<AttachmentInfo> getAttachmentById(UUID id){String table = "com.fdw.AttachmentEntity";List<AttachmentInfo> result = new ArrayList<>();List<AttachmentEntity> resultList = template.execute(new HibernateCallback<List<AttachmentEntity>>() {@Overridepublic List<AttachmentEntity> doInHibernate(Session session) throws HibernateException {String sql = "from " + table + " where Id = :id";Query<AttachmentEntity> query = session.createQuery(sql, AttachmentEntity.class);query.setParameter("id", id);return query.getResultList();}});for (AttachmentEntity attachmentEntity : resultList){result.add(attachmentEntity.toAttachmentInfo());}return result;}

问题原因:

 AttachmentEntity 类有个懒加载的字段 properties,关联了其他表。

关于懒加载

所谓懒加载(lazy)就是延时加载,延迟加载。即不是不加载,而是在需要的时候才加载。

什么时候用懒加载呢,我只能回答要用懒加载的时候就用懒加载。

至于为什么要用懒加载呢,就是当我们要访问的数据量过大时,明显用缓存不太合适,

因为内存容量有限 ,为了减少并发量,减少系统资源的消耗,

我们让数据在需要的时候才进行加载,这时我们就用到了懒加载。

比如部门ENTITY和员工ENTITY,部门与员工1对多,

如果lazy设置为 false,那么只要加载了一个部门的po,就会根据一对多配置的关系把所有员工的po也加载出来。

但是实际上有时候只是需要用到部门的信息,不需要用到 员工的信息,这时员工po的加载就等于浪费资源。如果lazy设置为true,那么只有当你访问部门po的员工信息时候才回去加载员工的po的信息。

hibernate3.0 中 lazy有三个值:

lazy有三个属性:true、false、extra

【true】:默认取值,它的意思是只有在调用这个集合获取里面的元素对象时,才发出查询语句,加载其集合元素的数据 。即只有在使用的时候才发出查询命令。

【false】:取消懒加载特性,即在加载对象的同时,就发出第二条查询语句加载其关联集合的数据即加载对象的时候就发出查询语句,加载关联的子类数据。


【extra】:一种比较聪明的懒加载策略,即调用集合的size/contains等方法的时候,hibernate并不会去加载整个集合的数据,而是发出一条聪明的SQL语句,以便获得需要的值,只有在真正需要用到这些集合元素对象数据的时候,才去发出查询语句加载所有对象的数据

 

 

解决后代码:

public List<AttachmentInfo> getAttachmentById(UUID id){String table = "com.fdw.AttachmentEntity";List<AttachmentInfo> result = new ArrayList<>();template.execute(new HibernateCallback<List<AttachmentEntity>>() {@Overridepublic List<AttachmentEntity> doInHibernate(Session session) throws HibernateException {String sql = "from " + table + " where Id = :id";Query<AttachmentEntity> query = session.createQuery(sql, AttachmentEntity.class);query.setParameter("id", id);List<AttachmentEntity> resultList = query.getResultList();for (AttachmentEntity attachmentEntity : resultList){result.add(attachmentEntity.toAttachmentInfo());}return null;}});return result;}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com