-->
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.  [ 7 posts ] 
Author Message
 Post subject: Many To One Mapping on the Same Table/Object
PostPosted: Fri Jan 16, 2009 8:10 am 
Newbie

Joined: Fri Jan 16, 2009 7:58 am
Posts: 4
Hi All,

New to the forums....been trying to solve this for some time and it's starting to grind me down.

I need to Create a Many-to-One relationship on the Same Table.

The context is I have a Trade Object that can be both a parent or a child in a relationship.
Code:
public class Trade{

     private Integer Id; //Primary Key
     private Integer parentId ; //PK of Parent Trade Object
     private Collection<Trade> childTrades ; //Collection Of Child Trades belonging to this parent
}


A snippet of my hibernate mapping file looks as follows :

Code:
   <class name="com.jpmorgan.ecredit.salesbookingtool.dao.bean.Trade" table="trades">

      <id name="id" unsaved-value="null">
         <generator class="native"/>
      </id>

      <many-to-one name="parentId" class="com.jpmorgan.ecredit.salesbookingtool.dao.bean.Trade" column="parent_id"/>

      <set
         name="childTrades"
         cascade="all"
         inverse="true"
         lazy="false">
         <key column="parent_id" />
         <one-to-many class="com.jpmorgan.ecredit.salesbookingtool.dao.bean.Trade"/>
      </set>
   </class


I have been able to establish the one-to-many side of the relationship with no problems, however, I cannot figure out how to establish the many-to-one side of the relationship where both parent and child are of the same object.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 8:23 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You'll need to have a Trade object as the parent not just the ID. Something like:

Code:
private Trade parent;

and

Code:
<many-to-one name="parent" class="com.jpmorgan.ecredit.salesbookingtool.dao.bean.Trade" column="parent_id"/>


Top
 Profile  
 
 Post subject: join problem within composite keys and oneToMany
PostPosted: Fri Jan 16, 2009 8:32 am 
Regular
Regular

Joined: Tue Jun 03, 2008 1:12 pm
Posts: 84
Location: germany
I have a similar problem:

I want to map these two tables:

WOOD (Table)
---------------
ID_WOOD (Primary Key)
NAME

TREE (Table)
-------------
ID_WOOD (Primary Key AND Foreign Key)
ID_TREE (only Primary Key)
NAME

The Relationship: OneToMany (one Wood can consist of at least one Tree or more Trees)

My Table WOOD:

Code:
@Entity
@Table(name = "WOOD)
public class Wood implements Serializable {

private Integer idWood;
@Id
@Column(name = "ID_WOOD")
// getter/setter

private String name;
@Column(name = "NAME")
// getter/setter

//hashCode/equals
..

// relationship to TREES
private Set<Tree> trees = new HashSet<Tree>();
@OneToMany(mappedBy = "wood", targetEntity = Tree.class)
@JoinColumn(name = "ID_WOOD", referencedColumnName = "ID_WOOD", nullable = false, insertable = false, updatable = false)
// getter/setter

}


My Table TREE :
Code:
@Entity
@Table(name = "TREE )
public class Tree implements Serializable {

// Tree consists of a composite key (ID_TREE, ID_WOOD)
public static class TreeID implements Serializable
{   

@Basic(optional = false)
@Column(name = "ID_TREE", nullable = false, columnDefinition = "integer")
private Integer idApplication;
      
@Basic(optional = false)
@Column(name = "ID_WOOD", nullable = false, columnDefinition = "integer")
private Integer idDocument;

//hashCode/equals
..
}

@EmbeddedId
private TreeID treeID = new TreeID();
// getter/setter


// relationship to Wood (this does not work!!)
private Wood wood;
@ManyToOne(optional = true, targetEntity = Wood.class)
@JoinColumn(name = "ID_WOOD", referencedColumnName = "ID_WOOD", nullable = false, insertable = false, updatable = false)
// getter/setter


}



My Relationship does NOT work properly.
(I have tried it also with PrimaryKeyJoinColumn, without success..I guess this is only for OneToOne-Relationships)

This HQL

"from Wood w"

returns a correct SQL:

Code:
select
  wood0_.ID_WOOD as IDWOOD _829_,
  wood0_.NAME as name_829_,
from
  MYSCEME.WOOD wood0_



But this HQL

"from Tree t"

returns a corrupted SQL:


Code:
select
  tree0_.ID_WOOD as IDWOOD _829_,
  tree0_.ID_TREE as IDTREE _829_,
  tree0_.wood as wood3_829_,
  tree0_.NAME as name829_,
from


  MYSCEME.TREE tree0_


This:

tree0_.wood as wood3_829_, should be a relationship (!) and has not to be in this sql-statement. So I guess, my Mapping is false! But Hibernate does not complain anything when validating the schema.

I do not know, what is wrong?

Is it possible to mapp a bidirectional mapping?

How can I improve this mapping?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 9:37 am 
Newbie

Joined: Fri Jan 16, 2009 7:58 am
Posts: 4
nordborg wrote:
You'll need to have a Trade object as the parent not just the ID. Something like:

Code:
private Trade parent;

and

Code:
<many-to-one name="parent" class="com.jpmorgan.ecredit.salesbookingtool.dao.bean.Trade" column="parent_id"/>


Hi nordborg - I followed your advice, however, the children still do not get populated with the parent trade id in the parent_id column.

Back to the drawing board....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 9:48 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Have you called childTrade.setParent(parentTrade)? As the mapping is written now this is what controls the relation. It will not be enough to just call parentTrade.getChildTrades().add(childTrade). This information may be useful:
http://www.hibernate.org/hib_docs/v3/re ... bidir.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 10:35 am 
Newbie

Joined: Fri Jan 16, 2009 7:58 am
Posts: 4
nordborg wrote:
Have you called childTrade.setParent(parentTrade)? As the mapping is written now this is what controls the relation. It will not be enough to just call parentTrade.getChildTrades().add(childTrade). This information may be useful:
http://www.hibernate.org/hib_docs/v3/re ... bidir.html


yeah, i have ensured that I set the parent, as below :

Code:
Trade parentTrade = TradeTest.newTrade();
Trade childTrade = TradeTest.newTrade();

childTrade.setParent(parentTrade);
parentTrade.addChildTrade(childTrade);


Thanks for your assistance and time so far - I think it may be time for me to get start debugging the Hibernate code to find out exactly why it will update the trade on the one-to-many side - i.e. create the new trade but fail to update the parent_id on the many-to-one side.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 11:16 am 
Newbie

Joined: Fri Jan 16, 2009 7:58 am
Posts: 4
Nordborg - Many thanks for your advice, between your advice and the Hibernate documents I was able to understand and configure.

Thanks again.


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