-->
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.  [ 6 posts ] 
Author Message
 Post subject: Many-to-Many association --> Table1 - Table 2 - Table 1 ?
PostPosted: Fri Jan 20, 2006 5:24 pm 
Newbie

Joined: Tue Jan 17, 2006 12:51 pm
Posts: 8
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1

Name and version of the database you are using: SQL Server

Hello,
We have the following tables in our database.

Table 1
Part_Id - int identity (10) - Primary key
Part_Code - Char (10)
Part_Name - Varchar (50)
Deleted - int(1) - value zero or one


Table 2
Part_Id - int(10) - Foreign key -->Table1.Part_Id
Replacing_Part_Id - int(10) - Foreign key-->Table1.Part_Id
Deleted - int(1) - value zero or one
Part_Id & Replacing_Part_Id is a composite primary key in table2.


Requirements: Rows should not be deleted physically in the database. They should be marked as deleted. Value 0 in Deleted column indicates row is not deleted. Py key value in Deleted column indicates row is deleted.

Please let me know how to create hbm mappings and java class for above schema?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 5:59 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Considering your requirement on the deleted item, also considering you have a property to manage in your many to many mapping table, you should do with 2 associations (*...1 and 1..*)

class for table 1 which has a one to many association on table2 (part_id -> part_id)
class for table 2 which has a many to one association on table 1(replacing_part_id -> part_id)

The composite key is documented in hibernate docs, as are the 'one to many' and the 'many to one' associations.

Also, don't activate the 'on delete cascade' in you hbm :) (Well according to your requirements, you never delete anyway)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 3:11 pm 
Newbie

Joined: Tue Jan 17, 2006 12:51 pm
Posts: 8
Many thanks tchize!!

Sorry to be so naive. I am very new to hibernate. What is the basic difference between 'one to many' and 'many to one' association in hibernate? Can I map my classes like i have defined below?

Class for table 1
has a one to many association on table2 (part_id -> part_id)
has a one to many association on table 2(part_id -> replacing_part_id)

a) inverse atttibute is set to "true" for both these mappings.
b) Class has two HashSet instance variables.

class for table 2
has Part as the instance variable.

Is this correct? Please let me know.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 4:59 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
hiber123 wrote:
Many thanks tchize!!

Sorry to be so naive. I am very new to hibernate. What is the basic difference between 'one to many' and 'many to one' association in hibernate?


the type of association rules how hibernate will manage update / insert in database.

one to many and many to one is just a matter of the side you look the association from. (at SQL level there is no difference as SQL queries allows you to explore association both ways). However at objects level, you can only explore an association in a given direction only if there are appropriate getters. If you want to explore a given association both way, you have to put appropriate getters on both classes. Hibernate requires you to create a descriptor for each class. This mean you have to describe the association, in the good direction, for each descriptor. But you seem to have already understood it considering your suggested code.

Quote:
Can I map my classes like i have defined below?

Class for table 1
has a one to many association on table2 (part_id -> part_id)
has a one to many association on table 2(part_id -> replacing_part_id)

a) inverse atttibute is set to "true" for both these mappings.
b) Class has two HashSet instance variables.

class for table 2
has Part as the instance variable.

Is this correct? Please let me know.

i'll say, table 2 has two parts and not one (one part representing the part_id field and another part representing the replacing_part_id field :)
i suppose you plan to have the one to many association represented by a <set ...> tag in .hbm overall it seems to correspond to you request :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 6:46 pm 
Newbie

Joined: Tue Jan 17, 2006 12:51 pm
Posts: 8
Many thanks tchize!! One last question!!! What is the need/advantage of having two parts instance variable in class2?

I have defined my mappings for class2 & class 1 as

CLASS2

Code:
   

  /* Two mappings defined for same instance variable*/

   * @hibernate.many-to-one column="PART_ID"  class="CLASS1"  cascade="save-update"
   * @hibernate.many-to-one column="REPLACING_PART_ID"  class="CLASS1"  cascade="save-update"

      public Part getPart() {
      return this.part;
     }




CLASS1

Code:
   
   * @hibernate.set inverse="true" cascade="save-update" lazy = "false"
   * @hibernate.collection-key column="PART_ID"
   * @hibernate.collection-one-to-many class="CLASS2" 

     public Set getPartsThatReplacesThisPart() {
      return this.partsThatReplacesThisPart;;
      }



   * @hibernate.set inverse="true" cascade="save-update" lazy = "false"
   * @hibernate.collection-key column="REPLACING_PART_ID"
   * @hibernate.collection-one-to-many class="CLASS2" 
   
    public Set getPartsReplacedByThisPart() () {
    return this.partsReplacedByThisPart;
  }





Please let me know!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 10:19 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
hiber123 wrote:
Many thanks tchize!! One last question!!! What is the need/advantage of having two parts instance variable in class2?

It just comes from your sql definition. table2 has 2 foreign keys, (PART_ID and REPLACING_PART_ID), so obviously you need two properties in CLASS2 to reprensent this (one which correspond to REPLACING_PART_ID and one which correspond to PART_ID)


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