-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem in Hibernate mapping
PostPosted: Wed Feb 10, 2010 2:44 am 
Newbie

Joined: Wed Jun 17, 2009 2:04 am
Posts: 6
Hi all

My domain is like this :
Code:
class lines{ 
     String comment; 
     Collection parent; (of type lines) 
     Collection children; (of type lines) 


I have two tables to which this is to be saved...
Code:
lines 

    id serial NOT NULL, 
   comments character varying(500), 

   
lines_hierarchy 

   id serial NOT NULL, 
   parent_id integer NOT NULL, 
   child_id integer NOT NULL 




Please help me to write an hibernate mapping for this


Top
 Profile  
 
 Post subject: Re: Problem in Hibernate mapping
PostPosted: Wed Feb 10, 2010 3:56 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I would try with following:

@Entity
@org.hibernate.annotations.Table( appliesTo = "lines")
@SecondaryTable(name = "lines_hierarchy")
class Lines {
@Id @GeneratedValue
private long id;

@Column
String comment;

@javax.persistence.ManyToMany
protected Set<Lines> parent = new java.util.HashSet<Lines>();

@javax.persistence.ManyToMany(mappedBy = "parent")
protected Set<Lines> children = new java.util.HashSet<Lines>();
}

N.B.: I not understand for what purpose lines_hierarchy.id stands for.


Top
 Profile  
 
 Post subject: Re: Problem in Hibernate mapping
PostPosted: Wed Feb 10, 2010 5:57 am 
Newbie

Joined: Wed Jun 17, 2009 2:04 am
Posts: 6
Sorry to say that I don't understand annotations...

and you specified about using HashSet..??? why?? cant i use Collection..

thanks....


Top
 Profile  
 
 Post subject: Re: Problem in Hibernate mapping
PostPosted: Wed Feb 10, 2010 6:10 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
Sorry to say that I don't understand annotations...


Since ever I work only with annotations, and I have not the time to translate it for you into hbm syntax.

Collection is just an interface. For concrete use you need to specify a concrete implementation of collection and
HashSet is an implementation of collection (overmore Set is the suggested first-choice by hiberante)


Top
 Profile  
 
 Post subject: Re: Problem in Hibernate mapping
PostPosted: Wed Feb 10, 2010 8:45 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
The mappings provided look good. You should get comfortable with them.

Here's a little tutorial on using Hibernate Many to Many mappings. Give it a look, along with the section on setting up Hiberante with annotations support:

http://www.hiberbook.com/HiberBookWeb/l ... ationships

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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