-->
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: Help, Insert in association table problem
PostPosted: Wed Aug 09, 2006 11:59 pm 
Newbie

Joined: Wed Aug 09, 2006 10:52 am
Posts: 1
Hibernate version: 3.0

Name and version of the database you are using: MySQL 5.0

I can't seem to add an entry onto my association table.

Here's the setup:
"Supplier" can have many "Book"
"Book" can have many "Supplier"

I have a join table called BookSupplier that takes both the PK's of Book and Supplier; and sets it as its own PK

here is my Supplier.hbm
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
       
<hibernate-mapping>
   <class name="Supplier" table="suppliers">
      <id name="supplierId" column="supplierId" type="int">
         <generator class="native"/>
      </id>
      <property name="name"/>
      <property name="address"/>
      
      <set name="books" table="BookSupplier" inverse="true">
         <key column="supplierId" />
         <many-to-many column="bookId" class="Book"/>
      </set>
   </class>
</hibernate-mapping>


Book.hbm
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
       
<hibernate-mapping>
   <class name="Book" table="Book">
      <id name="bookId" column="bookId" type="int">
         <generator class="native"/>
      </id>
      <property name="bookTypeId" type="int"/>
      <property name="title"/>
      <property name="author"/>
      <property name="publisher"/>
      <property name="language"/>
      <property name="pageCount" type="int"/>
      <property name="quantity" type="int"/>
      <property name="margin" type="int"/>
      <property name="status"/>
      
      <set name="bookSuppliers" table="BookSupplier">
         <key column="bookId" />
         <many-to-many column="supplierId" class="SupplierBean"/>
      </set>
   </class>
</hibernate-mapping>


I already have Book.java and Supplier.java that have the necessary setters and getters.

excerpt from Supplier.java (to show the code for adding sets)
Code:
   protected Set getBooks()   {
      return books;
   }
   protected void setBooks(Set newbook)   {
      books = newbook;
   }
   public void addBooks(Book book)   {
      this.getBooks().add(book);
      book.getBookSuppliers().add(this);
   }
   public void removeBooks(Book book)   {
      this.getBooks().remove(book);
      book.getBookSuppliers().remove(this);
   }


this is the code that hopefully saves the object
Code:
public boolean addBookSupplier(Book b, Supplier s)
   {

      boolean check = false;
      try
      {
          Book tempB;
          Supplier tempS;
         tempB = (Book)((session.createQuery("from Book as book where book.title = ?")).setString(0,b.getTitle())).list().get(0);
          tempS = (Supplier)((session.createQuery("from Supplier as s where s.name = ?")).setString(0,s.getName())).list().get(0);

          tempS.addBooks(tempB);
          session.save(tempS);
         
          tx.commit();

          check = true;
      }catch(HibernateException e)   {
         System.out.println(e.getMessage());
      }
      return check;
   }


After I execute the code above, there would be no errors. However, the object was not saved onto the database. Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 10, 2006 3:10 pm 
Newbie

Joined: Wed Nov 23, 2005 11:37 am
Posts: 9
I am running into the same issue, outlined in my thread here:

http://forum.hibernate.org/viewtopic.php?t=963204

No help yet though.......


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 10, 2006 4:09 pm 
Newbie

Joined: Wed Nov 23, 2005 11:37 am
Posts: 9
I finally got this working by applying the following mappings, after re-reading section "6.3.2. Bidirectional associations" in the documentation.

Concert.hbb.xml
Code:
<hibernate-mapping>

    <class name="Concert" table="concert_tbl">
       <id name="concertID" column="concert_id">
          <generator class="identity"/>
       </id>
       
       <property name="concertDate" column="concert_date"/>
       <property name="notes"/>
       <property name="flyerImage" column="flyer_img"/>
       
       <!-- The associated Venue -->
       <many-to-one name="venue" column="venue_id" unique="true" not-null="true" lazy="false"/>

       <!-- The associated Artists -->
       <set name="artists" table="concert_artists_tbl" lazy="false" cascade="save-update,delete-orphan">
         <key column="concert_id" not-null="true"/>
         <many-to-many
            column="artist_id"
            class="Artist"
            outer-join="auto"/>
       </set>
    </class>
</hibernate-mapping>



Artist.hbm.xml
Code:
<hibernate-mapping>

    <class name="Artist" table="artist_tbl">
       <id name="artistID" column="artist_id">
          <generator class="identity"/>
       </id>
       
       <property name="name"/>
       <property name="websiteURL" column="website_url"/>
       <property name="emailAddress" column="email_address"/>

       <!-- The associated Concerts -->
       <set name="concerts" table="concert_artists_tbl" lazy="false" cascade="save-update" inverse="true">
         <key column="artist_id" not-null="true"/>
         <many-to-many
            column="concert_id"
            class="Concert"
            outer-join="auto"/>
       </set>

    </class>   
</hibernate-mapping>


Note the inverse="true" on Artist, and the cascade settings. Hope this helps!


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.