我有2个类,有多对一映射:
购物车类:
@Entity
@Table(name = "Cart")
public class Cart {
@Id
@Column(name = "ID", nullable = false)
private String id;
@ManyToOne(fetch = FetchType.LAZY, targetEntity = User.class)
@JoinColumn(name = "UserID", referencedColumnName = "ID")
private User user;
}
用户类:
@Entity
@Table(name = "User")
public class User {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
private Set<Cart> carts = new HashSet<Cart>();
}
当我从购物车对象获取外键时出现问题,它总是返回错误。如下代码:
<c:set var="cart" value='<%=CartDAO.getCart("CT00001")%>' />
<c:out value="${cart.user.id}" />
我的问题:
SEVERE: Servlet.service() for servlet [jsp] in context with path [/mavenHibernate] threw exception [javax.el.ELException: Error reading 'id' on type model.User_$$_jvst39a_3] with root cause
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:147)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:260)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:73)
at model.User_$$_jvst39a_3.getId(User_$$_jvst39a_3.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:94)
at org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:110)
at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:944)
at org.apache.jsp.test_jsp._jspx_meth_c_005fout_005f0(test_jsp.java:209)
at org.apache.jsp.test_jsp._jspService(test_jsp.java:176)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1452)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
当我使用hibernate xml映射时没有发生。请帮我解决这个问题。我找不到任何解决方案。
编辑: 这是xml映射版本:
Cart.hbm.xml:
<hibernate-mapping>
<class name="model.Cart" table="CART">
<id name="id" type="java.lang.String">
<column name="ID" />
<generator class="assigned" />
</id>
<many-to-one name="user" class="model.User" fetch="select">
<column name="USERID" />
</many-to-one>
<set name="cartItems" table="CARTITEM" inverse="true" lazy="true" fetch="select">
<key>
<column name="CARTID" />
</key>
<one-to-many class="model.CartItem" />
</set>
</class>
</hibernate-mapping>
User.hbm.xml:
<hibernate-mapping>
<class name="model.User" table="USER">
<id name="id" type="java.lang.String">
<column name="ID" />
<generator class="assigned" />
</id>
<set name="carts" table="CART" inverse="true" lazy="true" fetch="select">
<key>
<column name="USERID" />
</key>
<one-to-many class="model.Cart" />
</set>
</class>
</hibernate-mapping>
他们创建相同的查询:
xml查询:
Hibernate: select cart0_.ID as ID1_2_, cart0_.USERID as USERID2_2_, cart0_.DATEADD as DATEADD3_2_, cart0_.ACTIVE as ACTIVE4_2_ from CART cart0_ where cart0_.ID=?
注释查询:
Hibernate: select cart0_.ID as ID1_2_, cart0_.Active as Active2_2_, cart0_.DateAdd as DateAdd3_2_, cart0_.UserID as UserID4_2_ from Cart cart0_ where cart0_.ID=?
为什么注释查询抛出异常和xml查询不?我仍然不知道是什么导致了这个问题。
编辑2:只想找到解决方案。
编辑3:仍在寻找解决方案。
答案 0 :(得分:1)
您已将@ManyToOne
关系设为FetchType.LAZY。将其更改为EAGER
,或者更好的是,完全删除fetch属性,因为默认情况下ManyToOne是EAGER。