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: VERY URGENT - PLS HELP!!! Py key mapping to two FN keys?
PostPosted: Tue Jan 17, 2006 6:07 pm 
Newbie

Joined: Tue Jan 17, 2006 12:51 pm
Posts: 8
Hibernate version: 3.1

Name and version of the database you are using: MS SQL 2000

Following tables are present in our database.

Table 1
Part_Id - int identity (10) - Primary key
Part_Code - Char (10)
Part_Name - Varchar (50)



Table 2
Sequence_Id - int identity (10) - Primary key
Part_Id - int(10) - Foreign key -->Table1.Part_Id
Replacing_Part_Id - int(10) - Foreign key-->Table1.Part_Id


Is this many-to-many association or many-to-one association? Please let me know how to create hibernate mapping file for the above tables?

Many Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 6:19 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
judging by your table layout, I'd guess that one part can only have one replacement at a time, but one part could be a replacement for many others.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 9:16 am 
Newbie

Joined: Sat Dec 24, 2005 8:39 am
Posts: 15
<hibernate-mapping>
<class name="Table1ClassName" table="Table1">
<id name="partId" >
<column name="Part_Id">
<generator class="increment" />
</column>
</id>
<property name="partCode" type="string">
<column name="Part_Code" length="10" />
</property>
<property name="partName" type="string">
<column name="Part_Name" length="50" />
</property>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="Table2ClassName" table="Table2">
<id name="sequenceId" >
<column name="Sequence_Id">
<generator class="increment"/>
</column>
</id>
<many-to-one name="table1PartId" class="Table1ClassName">
<column name="Part_Id">
</many-to-one>
<many-to-one name="replacingPartId" class="Table1ClassName">
<column name="Replacing_Part_Id">
</many-to-one>
</class>
</hibernate-mapping>

Hope this gives you an initial insight.....


Regards
Ahmad


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 5:53 pm 
Newbie

Joined: Tue Jan 17, 2006 12:51 pm
Posts: 8
Many thanks for answering basha. The mapping needs to be bidrectional though.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 20, 2006 12:08 am 
Newbie

Joined: Sat Dec 24, 2005 8:39 am
Posts: 15
Let me know the functionality, i mean functionally how do you want to relate the two tables and what is the table / object that you would wish to use to drive the queries..

Regards
Ahmad


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 20, 2006 4:20 pm 
Newbie

Joined: Tue Jan 17, 2006 12:51 pm
Posts: 8
I forgot to mention that we use another column in the tables. The column
is called "deleted" and it indicates whether that row is deleted or not. This is done as a standard in order to avoid physical deletion of rows.


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
Sequence_Id - int identity (10) - Primary key
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


And I created my mappings like this.


Code:
<hibernate-mapping>
<class name="Table1ClassName" table="Table1">
<id name="partId" >
<column name="Part_Id">
<generator class="increment" />
</column>
</id>
<property name="partCode" type="string">
<column name="Part_Code" length="10" />
</property>
<property name="partName" type="string">
<column name="Part_Name" length="50" />
</property>


<set    name="replacingParts" lazy="false"   inverse="true"   cascade="save-update"   sort="unsorted">
<key column="Part_Id"></key>
<one-to-many class="Table2ClassName"/>
</set>

<property   name="deleted" column="DELETED"  />
       
</class>
</hibernate-mapping>



<hibernate-mapping>
<class name="Table2ClassName" table="Table2">
<id name="sequenceId" >
<column name="Sequence_Id">
<generator class="increment"/>
</column>
</id>
<many-to-one name="table1PartId" class="Table1ClassName" cascade="save-update">
<column name="Part_Id">
</many-to-one>


</class>
</hibernate-mapping>



Note that I didn't use the second many-to-one mapping in Table2ClassName that you had given (which might be the solution to the problem that i am facing right now).


This one-to-many mapping of parent-child relationship worked fine except for one condition.

If I want to delete a part in the parent table, I actually update the delete column value of that part to one. Under this condition, I should update(mark as delete) child rows in table 2 as well. I cud do that for those rows that have the part_id as foriegn key in table 2 (since they are of collections in Table1ClassName). However there could be reference to this part id in the Replacing_Part_Id columns of Table2.

Code:
Part_Id        Part_Code         Delete
1           AE                0
2           AF               0
3           AS               0 
4            FS               0
5          KS               0



seq_Id    Part_Id     replacing_part_id      Delete
1001      1          2                     0     
1002      1          3                   0
1003      3          5                  0
1004      4          1                  0     



Now If I mark part_id 1 as deleted in table 1 I can easily mark the 1001 and 1002 sequence rows of table2 as deleted. How do I find the fourth row (sequnce 1004) which has reference to part_id 1?

Please provide java class definition for correct mappings. Many thanks for yoor help!!


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.