Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hi
I am trying to build a simple order application. I am able to save and view Item and customer details. When trying to place an order I am having trouble. My Order_Line table has line_id and order_id as PK.
Hibernate version:
Hibernate-2.1
Mapping documents:
Item.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.example1.Item" table="item" >
<id name="id" type="int" unsaved-value="0">
<column name="id" sql-type="number(11)" not-null="true"/>
<generator class="sequence">
<param name="sequence">item_seq</param>
</generator>
</id>
<property name="name" column="name" type="java.lang.String" />
<property name="description" column="description" type="java.lang.String" />
<property name="quantity" column="available_qty" type="int" />
<property name="unitprice" column="unit_price" type="double" />
</class>
</hibernate-mapping>
Customer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.example1.Customer" table="customer" >
<id name="id" type="int" unsaved-value="0">
<column name="id" sql-type="number(11)" not-null="true"/>
<generator class="sequence">
<param name="sequence">cust_seq</param>
</generator>
</id>
<property name="lname" column="lname" type="java.lang.String" />
<property name="fname" column="fname" type="java.lang.String" />
<property name="street" column="street" type="java.lang.String" />
<property name="city" column="city" type="java.lang.String" />
<property name="state" column="state" type="java.lang.String" />
<property name="phone" column="phone" type="int" />
</class>
</hibernate-mapping>
Order.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.example1.Order" table="order_table" >
<id name="id" type="int" unsaved-value="0">
<column name="id" sql-type="number(11)" not-null="true"/>
<generator class="sequence">
<param name="sequence">order_seq</param>
</generator>
</id>
<property name="custid" column="custid" type="int" />
<property name="status" column="status" type="java.lang.String" />
<property name="total" column="total" type="double" />
<property name="orderdate" column="order_date" type="java.util.Date" />
<set name="orderItems" table="Order_Lines" inverse="true" cascade="all">
<key column="orderid"/>
<one-to-many class="com.example1.OrderLine" />
</set>
</class>
</hibernate-mapping>
OrderLine.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.example1.OrderLine" table="order_lines" >
<composite-id name="id" class="com.example1.OrderLine$Id" >
<key-property name="lineId" column="id" type="int"/>
<key-property name="orderid" column="orderid" type="int" />
</composite-id>
<property name="productid" column="prd_id" type="int" insert="false" update="false"/>
<property name="price" column="price" type="double" />
<property name="quantity" column="quantity" type="int" />
<many-to-one name="order"
class="com.example1.Order"
column="orderid" insert="false" update="false"/>
<many-to-one name="product"
class="com.example1.Item"
column="prd_id" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
//Session session = ConnectionFactory.getInstance().getSession();
Session session = HibernateUtilSimple.getSession();
try
{
Transaction tx=session.beginTransaction();
session.save(order);
tx.commit();
}
catch (HibernateException e)
{
//tx.rollback();
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
finally
{
if (session != null)
{
try
{
session.close();
}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
}
}
Full stack trace of any exception that occurs:
javax.servlet.ServletException: net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: com.example1.OrderLine$Id@1, of class: com.example1.OrderLine
at org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:594)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
at java.lang.Thread.run(Thread.java:534)
root cause
java.lang.RuntimeException: net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: com.example1.OrderLine$Id@1, of class: com.example1.OrderLine
at com.example1.OrderService.addOrder(Unknown Source)
at com.example1.OrderAction.execute(Unknown Source)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:594)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
at java.lang.Thread.run(Thread.java:534)
Caused by: net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: com.example1.OrderLine$Id@1, of class: com.example1.OrderLine
at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl.java:1687)
at net.sf.hibernate.impl.SessionImpl.doUpdateMutable(SessionImpl.java:1453)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1480)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1403)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:962)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:868)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:790)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:749)
... 37 more
Name and version of the database you are using:
Oracle 9i
The generated SQL (show_sql=true):
Hibernate: select customer0_.id as id0_, customer0_.lname as lname0_, customer0_.fname as fname0_, customer0_.street as street0_, customer0_.city as city0_, customer0_.state as state0_, ustomer0_.phone as phone0_ from customer customer0_ where customer0_.id=?
Hibernate: select item0_.id as id, item0_.name as name, item0_.description as descript3_, item0_.available_qty as availabl4_, item0_.unit_price as unit_price from item item0_ order by item0_.name
Product id :5 Number:5
Hibernate: select item0_.id as id0_, item0_.name as name0_, item0_.description as descript3_0_, item0_.available_qty as availabl4_0_, item0_.unit_price as unit_price0_ from item item0_ where item0_.id=?
Hibernate: select order_seq.nextval from dual
Hibernate Exceptiona different object with the same identifier value was already associated with the session: com.example1.OrderLine$Id@1, of class: com.example1.OrderLine
Debug level Hibernate log excerpt: