-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Exception on many-to-many relation
PostPosted: Mon Nov 07, 2005 5:53 am 
Newbie

Joined: Mon Nov 07, 2005 5:15 am
Posts: 2
Hi, I'm facing a problem with many-to-many relation, especially in the generation of the primary key for the association table.
I'm quite new to hibernate and I'm experimenting with its many features...

In my db I have an Order, a Product and an OrderLine tables. The OrderLine table has a composite key (orderId, productId) and two more fields, amount and price.
I developed an application that inserts new products without any problem.
But when I try to create a new Order I get an exception with this message:
ids for this class must be manually assigned before calling save(): OrderLine.
I can understand what hibernate is trying to tell me, in fact I don't explicitly assign the ids... I guess hibernate can handle this work for me, but as I said, I'm a newbie!

Thanks!

Hibernate version: 3.0.5

Mapping documents:
------ Mapping for class Order -------
<hibernate-mapping>
<class name="Order" table="Order">
<id name="id" type="integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="orderDate" type="date">
<column name="orderDate" length="10" not-null="true" />
</property>
<property name="totalPrice" type="double">
<column name="totalPrice" precision="22" scale="0" not-null="true" />
</property>
<set name="orderLines" table="OrdineLine" inverse="true" cascade="save-update" >
<key column="orderId"/>
<one-to-many class="OrderLine"/>
</set>
</class>
</hibernate-mapping>

------ Mapping for class Product -------
<hibernate-mapping>
<class name="Product" table="Product">
<id name="id" type="integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="nome" type="string">
<column name="name" length="50" not-null="true" />
</property>
<property name="price" type="double">
<column name="prce" precision="22" not-null="true" />
</property>
</class>
</hibernate-mapping>

------ Mapping for class OrderLine -------
<hibernate-mapping>
<class name="OrderLine" table="OrderLine">
<composite-id name="id" class="OrderLineId">
<key-property name="orderId" type="integer">
<column name="orderId" />
</key-property>
<key-property name="productId" type="integer">
<column name="productId" />
</key-property>
</composite-id>
<property name="amount" type="integer">
<column name="amount" not-null="true" />
</property>
<property name="price" type="double">
<column name="price" not-null="true" />
</property>
<many-to-one name="product" class="Product" insert="false" update="false">
<column name="productId" />
</many-to-one>
<many-to-one name="order" class="Order" insert="false" update="false">
<column name="orderId" />
</many-to-one>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Product p = null;

Query q = sess.createQuery("from Product p where p.name = 'Milk'");
List<Product> ql = q.list();
if(ql.size() > 0)
p = ql.get(0);

Transaction t = sess.beginTransaction();

Order o = new Order();
o.setOrderDate(new Date());
o.addProduct(p, 5); //Adds an OrderLine with 5 products
sess.persist(o);
t.commit();

Full stack trace of any exception that occurs:
Exception in thread "main" org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): OrderLine
at org.hibernate.id.Assigned.generate(Assigned.java:32)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:85)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:96)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468)
at org.hibernate.engine.Cascades$5.cascade(Cascades.java:154)
at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:771)
at org.hibernate.engine.Cascades.cascade(Cascades.java:720)
at org.hibernate.engine.Cascades.cascadeCollection(Cascades.java:895)
at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:792)
at org.hibernate.engine.Cascades.cascade(Cascades.java:720)
at org.hibernate.engine.Cascades.cascade(Cascades.java:847)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:121)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:112)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:59)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at insertOrder.main(insertOrder.java:44)

Name and version of the database you are using: Mysql 4.1.9


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 9:33 am 
Beginner
Beginner

Joined: Tue Jun 07, 2005 11:36 pm
Posts: 22
I belive you want to remove the insert=false and update=false attributes from your Product and Order mappings:

Code:
<many-to-one name="product" class="Product">
<column name="productId" />
</many-to-one>
<many-to-one name="order" class="Order">
<column name="orderId" />
</many-to-one>


You are telling Hibernate to not set those values when you add an OrderLine, so it has nothing from which to make the composite-id.

I am no expert on composite-ids, though, so could be totally off base.

-----
Eric

Helpful? Hit me with a credit!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 9:44 am 
Newbie

Joined: Mon Nov 07, 2005 5:15 am
Posts: 2
Thank you, but if I remove the insert="false" and/or update="false" attributes I get this exception:
Code:
Repeated column in mapping for entity: OrderLine column: productId (should be mapped with insert="false" update="false")


donnie


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.