-->
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.  [ 1 post ] 
Author Message
 Post subject: Collections of entities containing collections of entities
PostPosted: Wed Jan 27, 2010 11:12 am 
Newbie

Joined: Tue May 26, 2009 6:15 am
Posts: 4
Hi all,
I have a class Order which contains Products, which in turns contains Units (Unit is an abstract class, Copy and Folder are subclasses) :
Code:
@Entity
@Table(name = "t_order")
public class Order implements Serializable {

   private static final long serialVersionUID = 8986089659809970850L;

   @Id
   @GeneratedValue
   @Column(name = "order_id")
   private Integer id;

   @Column(unique = true, nullable = false)
   private String orderNumber;

   @OneToMany(cascade = CascadeType.ALL, mappedBy = "order", fetch = FetchType.EAGER)
   private Set<Product> products = new HashSet<Product>();

   // setters, getters and so on (...)
}

Note that the collection products is eagerly fetched.

Code:
@Entity
@Table(name="t_product")
public class Product implements Serializable {

   private static final long serialVersionUID = 6823234587691684869L;

   @Id
   @GeneratedValue
   @Column(name = "product_id")
   private Integer id;

   @ManyToOne
   @JoinColumn(name = "order_fk")
   private Order order;

   @OneToMany(cascade = CascadeType.ALL, mappedBy = "product")
   private Set<Unit> units = new HashSet<Unit>();
}

In this class, units is a lazy collection.

Code:
@Entity
@Table(name="t_unit")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Unit {

   @Id
   @GeneratedValue
   @Column(name = "unit_id")
   private Integer id;

   private String unitNumber;
}

@Entity
@PrimaryKeyJoinColumn(name = "folder_id")
public class Folder extends Unit implements Serializable {
        // (...)
}

@Entity
@PrimaryKeyJoinColumn(name = "copy_id")
public class Copy extends Unit implements Serializable {
        // (...)
}



Well. Now, I would like to find the order which number is, say "74152", and I want to fetch the units along with the products :

Code:
HQL:
from Order o inner join fetch o.products p inner join fetch p.units where o.orderNumber = ?


I don't know if this is the right query. But I have another problem :
Quote:
org.hibernate.InstantiationException: Cannot instantiate abstract class or interface: com.xx.domain.Unit.

I don't understand why Hibernate tries to instanciate this class instead of using the concrete ones. Can anyone help me (it is not desirable to eargerly load units).

Thanks in advance !


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.