-->
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: Mapping a tree within one table
PostPosted: Tue Apr 08, 2008 5:34 am 
Newbie

Joined: Tue Apr 08, 2008 4:50 am
Posts: 2
Hello everyone,

I would like to ask how to do the following thing. Let's have an entity object:

Code:
@Entity
public class Category {

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  @Column(name = "category_id")
  private Long categoryId; //primary key
 
  @Column(name = "name")
  private String categoryName;

  @Column(name = "parent_category_id")
  private Long parentCategoryId;

  @????
  private Category parentCategory;

  @????
  private List<Category> childCategories;
}


and a corresponding underlying DB table:

Code:
CREATE TABLE categories(
  category_id BIGINT not null primary key auto_increment,
  name VARCHAR(64),
  parent_category_id BIGINT);


How to do the mapping for the parentCategory and childCategories fileds?

Many thanks in advance for any clues you might come up with! :)

Ibisek


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 08, 2008 3:48 pm 
Newbie

Joined: Mon Jul 23, 2007 11:58 am
Posts: 15
Location: New Jersey
You have a one to many "self-association". That is a category can have many categories as children...

So the properties of your code look ok, you will need to map the categories set using a "one-to-many" construct.

The code to manipulate the relationships might look something like this...


Code:
Category parent = new Category();
Category child = new Category();

child.setparentCategory(parent);
parent.getChildCategories().add(child);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 09, 2008 3:13 am 
Newbie

Joined: Tue Apr 08, 2008 4:50 am
Posts: 2
So if I understand you well, I have to do it outside the entity, like for example in the DAO object, there is no way how to do it by Hibernate mapping....?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 09, 2008 5:01 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Code:
<class name="Category" table="category">
<id name="categoryId" column="category_id">
<generator class="sequence"/>
</id>
<property name="name" column="name" />
<property name="parentLocationId" column="parent_location_id" />
<set name="childCategories">
<key>
<column name="parent_category_id" />
</key>
<one-to-many class="Category" />
</set>
</class>


This is an example of self association. And I think your java bean class will change.

Code:
@Entity
public class Category {

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  @Column(name = "category_id")
  private Long categoryId; //primary key
 
  @Column(name = "name")
  private String categoryName;

  @Column(name = "parent_category_id")
  private Long parentCategoryId;

  @????
  private Set<Category> childCategories;


This is how I do to represent a tree structure. Maybe you can use it too.

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 09, 2008 6:54 am 
Newbie

Joined: Wed Apr 09, 2008 3:37 am
Posts: 4
Location: Tel Aviv, Israel
I'm using this type of mapping; It works using the initial insert, but when making modifications (e.g. deleting children) and updating, I get an exception thrown.

I've just opened a thread regarding this issue here. Again, this still doesn't work completely for me, but initial insert and selects DO work, so I must be in the right direction.

Fairly straightforward, actually, just notice that you have to implement Comparable for the SortedSet to work (I'm guessing that you don't REALLY need a list).

Hope this helps.

Code:
@Entity
public class Category implements Comparable<Category>{

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  @Column(name = "category_id")
  private Long categoryId; //primary key

  @Column(name = "name")
  private String categoryName;

   @ManyToOne(fetch = FetchType.EAGER, cascade=CascadeType.ALL, optional = true)
   @JoinColumn(name = "PARENT_CATEGORY_ID", nullable = true)
   @Fetch(FetchMode.SELECT)
   @Cascade({   org.hibernate.annotations.CascadeType.ALL,
            org.hibernate.annotations.CascadeType.SAVE_UPDATE,
            org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
  private Category parentCategory;

   @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="parentCategory")
   @Sort(type = SortType.NATURAL)
   @Fetch(FetchMode.SUBSELECT)
   @Cascade({   org.hibernate.annotations.CascadeType.ALL,
            org.hibernate.annotations.CascadeType.SAVE_UPDATE,
            org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
  private SortedSet<Category> childCategories;
}


Top
 Profile  
 
 Post subject: Re: Mapping a tree within one table
PostPosted: Wed Oct 02, 2013 5:02 pm 
Newbie

Joined: Wed Oct 02, 2013 4:53 pm
Posts: 1
Hello!

I have used the same mapping as proposed in the previous e-mail in order to depict the parent-child relationship. However, when I try to run
Code:
parent.getChildren()

hibernate gets in an endless loop (I waited for about 10 minutes with no result)! I also noticed that when I do the same thing for a simple parent-children hierarchy (where the parent has only one child which might have no children), there are no such issues.

Any idea what might be the problem? I checked if there are circular references, but didn't find any :)


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.