希望您能帮助我停止打在墙上我的头。
这里是我的问题,毕竟对很多论坛的研究很多天,我没有发现我现在遇到的问题,任何兼容的答案。
我有这似乎是一个已知的异常的org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.supinfo.suppicture.entity.User.pictures, no session or session was closed
,但还是让我发疯...
我试图迫使预先加载,而不是延迟加载,但仍然有同样的问题。
这里是我的类:
category.Java
@Entity
@Table(name = "category")
@XmlRootElement
public class Category{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String Name;
@OneToMany(mappedBy = "category")
private Collection<Picture> pictures;
(getters and setters)
user.Java
@Entity
@Table(name = "user")
@XmlRootElement
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String username;
private String password;
private String firstName;
private String lastName;
private String phoneNumber;
private String postalAddress;
private String email;
@OneToMany(mappedBy = "author")
private Collection<Picture> pictures;
(getters and setters)
picture.Java
@Entity
@Table(name = "picture")
@XmlRootElement
public class Picture {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
private String description;
@Column(columnDefinition="longblob")
private byte[] image;
private Date dateOfPublish;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "category_id")
private Category category;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private User author;
(getters and setters)
这里是我如何努力找回我的数据库中的图片(至极的是,有外键的唯一一个)的例子
@Override
public List<Picture> getAllPictures() {
EntityManager em = emf.createEntityManager();
try {
CriteriaQuery<Picture> criteriaQuery = em.getCriteriaBuilder()
.createQuery(Picture.class);
criteriaQuery.from(Picture.class);
return em.createQuery(criteriaQuery).getResultList();
} finally {
em.close();
}
}
最后叫这样的:
@Path("/picture")
public class PictureController {
@GET
@Path("/all") @Produces(MediaType.APPLICATION_JSON)
public List<Picture> getAllPictures(){
return DaoFactory.getPictureDao().getAllPictures();
}
例外波纹管的全堆栈跟踪:
FEVR。 03,2019下午4点38分48秒org.hibernate.LazyInitializationException坟墓未能懒洋洋地初始化角色的集合:com.supinfo.suppicture.entity.User.pictures,没有会议或会议是封闭org.hibernate.LazyInitializationException:失败懒洋洋地初始化角色的集合:com.supinfo.suppicture.entity.User.pictures,没有会议或会议于org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)在org.hibernate.collection关闭。 AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)在org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)在org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)在org.hibernate.collection .PersistentBag.iterator(PersistentBag.java:272)在com.sun.xml.internal.bind.v2.runtime.reflect.Lister $ CollectionLister.itera TOR(Lister.java:266)在com.sun.xml.internal.bind.v2.runtime.reflect.Lister $ CollectionLister.iterator(Lister.java:253)在com.sun.xml.internal.bind.v2。 runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:118)在com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:144)在com.sun.xml.internal。 bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:345)在com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:681)在com.sun.xml.internal。 bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:143)在com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:345)在了com.sun.xml。 internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:578)在com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:326)在了com.sun.xml。 internal.bind.v2.runtime.XM LSerializer.childAsRoot(XMLSerializer.java:479)在com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308)在com.sun.xml.internal.bind.v2.runtime。 MarshallerImpl.marshal在com.sun.jersey.core.provider.jaxb.AbstractListElementProvider(MarshallerImpl.java:163)在com.sun.jersey.json.impl.provider.entity.JSONListElementProvider.writeList(JSONListElementProvider.java:145)。的writeTo(AbstractListElementProvider.java:264)在com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)在com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java: 1448)在com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1360)在com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1350)在COM .sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)在com.sun.jersey.spi.container.servlet.ServletContainer.service(Servle tContainer.java:538)在com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)在javax.servlet.http.HttpServlet.service(HttpServlet.java:725)在org.apache .catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter的.java:52)在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)在org.apache.catalina。 core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)在org.apache.ca talina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)在org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java: 88)在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)在org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler .process(AbstractProtocol.java:658)在org.apache.coyote.http11.Http11NioProtocol $在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun Http11ConnectionHandler.process(Http11NioProtocol.java:222)(NioEndpoint.java :1566)在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1523)在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)在java.util.concurrent中。的ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)在org.apache.tomcat.util.threads.TaskThre广告$ WrappingRunnable.run(TaskThread.java:61)在java.lang.Thread.run(Thread.java:748)
FEVR。 03,2019下午4点38分48秒org.apache.catalina.core.StandardWrapperValve调用坟墓“Servlet.service()”倒拉的servlet球衣,servlet的一个généréUNE例外org.hibernate.LazyInitializationException:无法初始化懒洋洋地收集作用:com.supinfo.suppicture.entity.User.pictures,没有会话或会话在org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)在org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection被关闭的.java:375)在org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)在org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)在org.hibernate.collection.PersistentBag.iterator( PersistentBag.java:272)在com.sun.xml.internal.bind.v2.runtime.reflect.Lister $ CollectionLister.iterator(Lister.java:266)在com.sun.xml.inte rnal.bind.v2.runtime.reflect.Lister $ CollectionLister.iterator(Lister.java:253)在com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:118)在com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:144)在com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:345 )在com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:681)在com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java :143)在com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:345)在com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java :578)在com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:326)在com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.ja VA:479)在com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308)在com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl。的java:163)在com.sun.jersey.json.impl.provider.entity.JSONListElementProvider.writeList(JSONListElementProvider.java:145)在com.sun.jersey.core.provider.jaxb.AbstractListElementProvider.writeTo(AbstractListElementProvider.java: 264)在com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)在com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1448)在的com.sun .jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1360)在com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1350)在com.sun.jersey.spi .container.servlet.WebComponent.service(WebComponent.java:416)在com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)在com.sun.jersey.s pi.container.servlet.ServletContainer.service(ServletContainer.java:716)在javax.servlet.http.HttpServlet.service(HttpServlet.java:725)在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java: 291)在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)在org.apache.catalina.core .ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)在org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:142)在org.apache.catalina.valves.ErrorReportValve.invoke(二rorReportValve.java:79)在org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)在org.apache.catalina .connector.CoyoteAdapter.service(CoyoteAdapter.java:537)在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)在org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:658 )在org.apache.coyote.http11.Http11NioProtocol $ Http11ConnectionHandler.process(Http11NioProtocol.java:222)在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1566)在org.apache。 tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1523)在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(的ThreadPoolExecutor。 Java的:624)在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java: 61)在java.lang.Thread.run(Thread.java:748)
如果你需要更多的我的代码,以帮助我解决问题请告诉我。感谢您在advance.`
如果遗失了开发商。
让分这一步一步
1)你的球衣网络控制器调用你的DAO方法来获取图片的列表(没有错误)
2)现在你DAO层正从数据库(无错误数据)
3)值返回给Web服务控制器的方法(没有错误)
4)但是,你正在使用自动JSON转换到目标数据转换成JSON。您的图片与用户相关的数据。当JSON转换器试图获取的用户数据,因为您的交易已经结束,其产生的异常值是零。尝试如下所示的JSON转换期间忽略类别和用户字段。
@JsonIgnore
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "category_id")
private Category category;
@JsonIgnore
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private User author;
5)不应该直接使用图片对象JSON的转换,因为它会导致在循环冗余(无限循环)。尝试创建另一个包装类图片的处理这种情况
照片 - >用户 - >图片 - >用户(循环冗余)
因此,对于解决这个创建三个包装类
public class PictureWrapper
{
private int id;
private String name;
private String description;
private byte[] image;
private Date dateOfPublish;
private CategoryWrapper category;
private UserWrapper author;
}
public class UserWrapper
{
private int id;
private String username;
private String password;
private String firstName;
private String lastName;
private String phoneNumber;
private String postalAddress;
private String email;
}
public class CategoryWrapper
{
private int id;
private String Name;
}
然后改变getAllPictures方法以下
public List<PictureWrapper> getAllPictures()
{
List<PictureWrapper> resultList = new ArrayList<PictureWrapper>();
EntityManager em = emf.createEntityManager();
try
{
CriteriaQuery<Picture> criteriaQuery = em.getCriteriaBuilder().createQuery(Picture.class);
criteriaQuery.from(Picture.class);
List<Picture> list = em.createQuery(criteriaQuery).getResultList();
if(list != null && list.size() != 0)
{
for(Picture picture : list)
{
PictureWrapper pictureWrapper = new PictureWrapper();
//Load data from picture to picture wrapper with user and category
resultList.add(pictureWrapper);
}
}
}
finally
{
em.close();
}
return resultList;
}
您需要装饰OneToMany
性质user.pictures
并与获取策略category.pictures
EAGER
。
class Category {
@OneToMany(mappedBy = "category", fetch = FetchType.EAGER)
Collection<Picture> pictures;
}
class User {
@OneToMany(mappedBy = "author", fetch = FetchType.EAGER)
Collection<Picture> pictures;
}
作为一个侧面说明,你其实并不需要与装饰ManyToOne
属性,这被认为是默认的行为EAGER
获取策略。
由于桑托斯巴拉吉用于特技;)