-->
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: How to define a SELF REFERENCE using Annotations
PostPosted: Wed Apr 19, 2006 10:40 am 
Newbie

Joined: Wed Apr 19, 2006 10:31 am
Posts: 3
Location: Brazil
Hello guys!

I'm a beginner with Hibernate Annotations and I would like to know how to create a self reference in an entity. For example: I have a class called CATEGORY that can contains SUB CATEGORIES. I would like to obtain these SUB CATEGORIES and, through a category obtain the SUPER CATEGORY. Something like:

Code:
class Category
{

  // Category Attributes

  public ArrayList<Category> getSubCategories()
  {

  }

  public Category getSuperCategory()
  {

  }

}

_________________
Best Regards,

Marcelo Juventino
marcelo.juventino at ejbeer.com.br


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 3:17 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Code:
class Category
{

  // Category Attributes

  @OneToMany
  @JoinColumn(name = "category_id", unique = false, nullable = true, insertable = true, updatable = true)
  public [b]List[/b]<Category> getSubCategories()
  {

  }


  @ManyToOne
  @JoinColumn(name = "category_id", unique = false, nullable = true, insertable = true, updatable = true)
  public Category getSuperCategory()
  {

  }

}


Be aware of List interface!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 8:27 am 
Newbie

Joined: Wed Apr 19, 2006 10:31 am
Posts: 3
Location: Brazil
Hello Alesj!!!

Thanks for your reply!!!

How I will define my ID and other attributes? There's some difference in declaration to support this kind of relationship?

_________________
Best Regards,

Marcelo Juventino
marcelo.juventino at ejbeer.com.br


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 8:46 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
http://hibernate.org/5.html


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 9:09 am 
Newbie

Joined: Wed Apr 19, 2006 10:31 am
Posts: 3
Location: Brazil
I already read the annotations documentation but it doesn't explain how to define this kind of relationship and there aren't so much sources showing how to do it using EJB 3.

_________________
Best Regards,

Marcelo Juventino
marcelo.juventino at ejbeer.com.br


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 9:28 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
marcelojuventino wrote:
How I will define my ID and other attributes?


Quote:
but it doesn't explain how to define this kind of relationship


Sure ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 9:40 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Actually more something like that
Code:
class Category
{

  // Category Attributes

  @OneToMany(mappedBy="superCategory")
  public [b]Set[/b]<Category> getSubCategories()
  {

  }


  @ManyToOne
  @JoinColumn(name = "category_id")
  public Category getSuperCategory()
  {

  }

}

_________________
Emmanuel


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.