-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem with Many-to-Many association
PostPosted: Mon Feb 18, 2008 6:19 pm 
Beginner
Beginner

Joined: Thu Dec 13, 2007 2:32 pm
Posts: 26
Hi,

I have tried to structure the post so as to make everything as clear as possible ... can someone figure out where am i wrong with this ?

Hibernate version:
Using 3.x

Mapping documents:

Product_Parts Mapping file :
-------------------------------------------
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="om.shiva">
   
   <class name="ProductParts" table="PRODUCT_PARTS">
      <composite-id name="id" class="ProductParts$Id">
         <key-property name="productId" column="product_id" access="field" />
         <key-property name="partId" column="part_id" access="field" />
      </composite-id>

      <!--  Relationships Begin here  -->
      <many-to-one name="product" class="Product" insert="false" update="false" />
      <many-to-one name="part" class="Part"  insert="false" update="false" />
      <!--  Relationships End here -->
   </class>
</hibernate-mapping>



Product Mapping File
--------------------------------------------------------
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="om.shiva">
   <class name="Product" table="PRODUCT">
      <id name="id" column="product_id">
         <generator class="assigned" />
      </id>
      
      <property name="name" column="name"/>
      
      <set name="productParts" inverse="true" cascade="save-update">
         <key column="product_id" />
         <one-to-many class="ProductParts"  />
      </set>
   </class>
</hibernate-mapping>


Part mapping is also the same as product mapping has a many to many association with productParts.

ProductParts class is as follows :
-------------------------------------------------
Code:
@SuppressWarnings("serial")
public class ProductParts implements Serializable{

   
   
   public static class Id implements Serializable{
      public Long productId;
      public Long partId;
      
   }
   
   private Id id;

   private Product product;
   private Part part;
   
   public Product getProduct() {
      return product;
   }

   public void setProduct(Product product) {
      this.product = product;
   }

   public Part getPart() {
      return part;
   }

   public void setPart(Part part) {
      this.part = part;
   }

   public Id getId() {
      return id;
   }

   public void setId(Id id) {
      this.id = id;
   }

}






Code between sessionFactory.openSession() and session.close():

Code:
   // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      // Products here
      // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      Product product = new Product();
      product.setId(1008L);
      product.setName("Anirudh");
      
      
      
      // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      // Parts here
      // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      Part part = new Part();
      part.setId(11L);
      part.setName("Shiv");
      
      // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      // ProductParts  here
      // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ProductParts productParts = new ProductParts();
      productParts.setPart(part);
      productParts.setProduct(product);
      ProductParts.Id id = new ProductParts.Id();
      // associate and establish foreign key relation ships
      id.partId = part.getId();
      id.productId = product.getId();
      productParts.setId(id);
      product.addProductParts(productParts);
      part.addProductParts(productParts);

      session.save(productParts);




Full stack trace of any exception that occurs:

Code:
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   at om.shiva.Test.main(Test.java:84)
Caused by: java.sql.BatchUpdateException: ORA-02291: integrity constraint (WAPERMAN.FK_PRODUCT_ID) violated - parent key not found

   at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
   at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10698)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
   ... 8 more




Name and version of the database you are using:

Oracle 10g.

table structure
PRODUCT_PARTS TABLE (PART_ID REFERENCES PART_ID IN PARTS TABLE && PRODUCT_ID REFERENCES PRODUCT_ID IN PRODUCT TABLE )

PART_ID AND PRODUCT_ID are part of primary key i.e. Primary key(part_id, product_id)

The generated SQL (show_sql=true):

Code:
Hibernate:
    select
        product_.product_id,
        product_.name as name0_
    from
        WAPERMAN.PRODUCT product_
    where
        product_.product_id=?
Hibernate:
    select
        part_.part_id,
        part_.name as name1_
    from
        WAPERMAN.PART part_
    where
        part_.part_id=?
Hibernate:
    insert
    into
        WAPERMAN.PRODUCT_PARTS
        (product_id, part_id)
    values
        (?, ?)



Basically when i do a <br/>
Code:
session.save(Product);
session.save(Parts);
session.save(ProductParts);



All is fine ( i remove cascade="save-update" from Product and Parts Mapping files), but that would defeat the purpose of many-to-many cascades .... Am I missing something ?!

Regards
Vyas, Anirudh[/code]

_________________
Regards,
Vyas, Anirudh


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 9:37 pm 
Beginner
Beginner

Joined: Thu Dec 13, 2007 2:32 pm
Posts: 26
hullo ... testing testing ... anyone ? ;)


Regards
Vyas, Anirudh

_________________
Regards,
Vyas, Anirudh


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 6:16 am 
Regular
Regular

Joined: Wed Jun 20, 2007 1:53 am
Posts: 75
Reason for this error is you are tring to insert product_parts record without inserting products or parts record, that means inserting child record with out inserting parent record.

To solve this,

Use cascade="save-update" in both the many-to-one of your ProductParts mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 5:05 pm 
Beginner
Beginner

Joined: Thu Dec 13, 2007 2:32 pm
Posts: 26
Muahahahaha! thanks a zillion dude, gosh i missed it, duh! it is working fine now ...

Regards
Vyas, Anirudh

_________________
Regards,
Vyas, Anirudh


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.