我有一个集合的实体,我想在懒惰中加载实体但我得到错误:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: lu.entities.folders.Folders.requests, could not initialize proxy - no Session (through reference chain: lu.entities.folders.Folders.requests)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:210)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:177)
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:190)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:674)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:129)
at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:851)
at org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider.writeTo(ResteasyJackson2Provider.java:207)
at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.writeTo(AbstractWriterInterceptorContext.java:131)
at org.jboss.resteasy.core.interception.ServerWriterInterceptorContext.writeTo(ServerWriterInterceptorContext.java:60)
at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:120)
at org.jboss.resteasy.security.doseta.DigitalSigningInterceptor.aroundWriteTo(DigitalSigningInterceptor.java:145)
at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:124)
at org.jboss.resteasy.plugins.interceptors.encoding.GZIPEncodingInterceptor.aroundWriteTo(GZIPEncodingInterceptor.java:100)
at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:124)
at org.jboss.resteasy.core.ServerResponseWriter.writeNomapResponse(ServerResponseWriter.java:98)
at org.jboss.resteasy.core.SynchronousDispatcher.writeResponse(SynchronousDispatcher.java:466)
... 38 more
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: lu.pgd.ccpd.entities.folders.Folders.requests, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:587)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:204)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:148)
at org.hibernate.collection.internal.PersistentBag.size(PersistentBag.java:261)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:88)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:24)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:666)
... 51 more
我的实体喜欢:
@Entity
@Table(name = "FOLDER_FR")
@JsonIdentityInfo(scope=Folders.class, generator = ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Folders implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "FR_ID")
private Long id;
@LazyCollection(LazyCollectionOption.TRUE)
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinTable(name = "M_REQUESTFOLDER_RR", joinColumns = {
@JoinColumn(name = "RR_FOLDER", referencedColumnName = "FR_ID") }, inverseJoinColumns = {
@JoinColumn(name = "RR_REQUEST", referencedColumnName = "RS_ID") })
private List<Requests> requests = new ArrayList<Requests>();
}
在我的DAO中:
@Override
public Folders retrieveFolder(Long id) throws PersistenceException {
Folders folders = entityManager.find(Folders.class, id);
return folders;
}
那么如何在JPA中解决它?当我调用我的方法retrieveFolder时出了什么问题?
非常感谢
您必须初始化收集请求。
您可以调用folders.getRequests()。iterator()来触发请求的加载。
但最好的方法是将带有JOIN FETCH的JPQL查询写入将急切加载该集合的请求:
SELECT f FROM Folder f JOIN FETCH f.requests r
或者使用将执行相同操作的EntityGraph。
查看Hibernate文档并搜索JOIN FETCH和EntityGraph。
http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html