-->
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.  [ 4 posts ] 
Author Message
 Post subject: Parent childs problem
PostPosted: Thu May 15, 2008 10:04 am 
Newbie

Joined: Wed May 09, 2007 7:06 am
Posts: 12
Hi,

in my application I need a data structure to make it possible save parent child relations. My special case here is that each child can also be a parent and so on.

Therefore there is a abstract class used as superclass for all entities

Code:
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class BaseCategory<T extends StandardEntity> extends StandardEntity {

   private Long id;
   private List<T> childs;
   private T parent;
   
   
   @Id @GeneratedValue(strategy=GenerationType.TABLE)
   public Long getId(){return id;}
   public void setId(Long id){this.id = id;}

   @OneToMany(cascade=CascadeType.ALL, targetEntity=BaseCategory.class, mappedBy="parent")
   public List<T> getChilds() {return childs;}
   public void setChilds(List<T> childs) {this.childs = childs;}
   
   @ManyToOne()
   public T getParent() {return parent;}
   public void setParent(T parent) {this.parent = parent;}
}


And I have many entity classes using the BaseCategory above. Here one example, the others created in the same way.
Code:
@Entity
@Name("package")
public class Package extends BaseCategory<BaseCategory> implements Serializable {

   
   private static final long serialVersionUID = 5330799040159648995L;
   private Long package_id;
   private String package_name;
   private Long package_totalCount = new Long(0);
   
   
   public Long getPackage_id() {return package_id;}
   public void setPackage_id(Long package_id) {this.ackage_id = ackage_id;}
   
   public String getPackage_name() {return package_name;}
   public void setPackage_name(String package_name) {this.package_name = package_name;}
   
   public Long getPackage_totalCount() {return package_totalCount;}
   public void setPackage_totalCount(Long package_totalCount) {this.package_totalCount = package_totalCount;}

}


in my business code i use:
Code:
records = new ArrayList<BaseCategory>();

package= new Package();
package.setPackage_id(Long.valueOf(w94aufnpTemp));
package.setPackage_name(w91bezei);
package.setChilds(new ArrayList<BaseCategory>());
records.add(package);

service = new Service();
service.setService_id(Long.valueOf(w94teilnTemp));
service.setService_name(w94bezei);
package.getChilds().add(service);
service.setChilds(new ArrayList<BaseCategory>());
service.setParent(package);

cardType = new KardType();
cardType.setKardType_name(w40klassTemp);
service.getChilds().add(cardType);
cardType.setChilds(new ArrayList<BaseCategory>());
cardType.setParent(service);

Package package;
for (BaseCategory pge : records) {
   if (pge.getClass().getSimpleName().equals("Package")) {
      package = (Package) pge;
      entityManager.persist(package);
   }
}
entityManager.flush();


All working fine, but if I try to remove a data set it takes a long time. My database is MySQL, and MySQL Administrator displays always "freeing items" and "removing tmp table"
Code:
entityManager.remove(package);



Any ideas?

_________________
Feivel Frey


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 3:00 am 
Newbie

Joined: Wed May 09, 2007 7:06 am
Posts: 12
Ok, is this maybe a desgin problem?

Can tell me someone a better design to build these parent/child/parent... data structure?


Thanks

_________________
Feivel Frey


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 17, 2008 9:22 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Wouldn't it be possible to use aggregation instead of inheritance. Then a parent can have a child, and a parent can also have an ancestor (other parent)?

_________________
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  
 
 Post subject:
PostPosted: Sun May 18, 2008 6:47 am 
Newbie

Joined: Wed May 09, 2007 7:06 am
Posts: 12
hi,

my problem is that a parent can have several childs and each of this childs can also be a parent with childs in addition (children-childs).

An example would be helpfull.

Thanks

_________________
Feivel Frey


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