-->
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.  [ 3 posts ] 
Author Message
 Post subject: Question about POJO class
PostPosted: Sat Dec 15, 2007 4:42 am 
Newbie

Joined: Sat Dec 15, 2007 3:33 am
Posts: 2
Hi, everybody!

I have some questions about the flowing code

Code:
@MappedSuperclass 
public abstract class Work {   
}   
 

@Entity 
public class CodeWork extends Work{   
  @OneToMany(mappedBy="codeWork")   
  private List<Employee> employees;   
}   
 
 
@Entity 
public class UIWork extends Work{   
  @OneToMany(mappedBy="uiWork")   
  private List<Employee> employees;   
}   
 
 
@Entity 
public class Employee{   
  @ManyToOne 
  @JoinColumn(name="codeWork_id", nullable=false, updatable= false)   
  private CodeWork codeWork;   
 
  @ManyToOne 
  @JoinColumn(name="uiWork_id", nullable=false, updatable= false)   
  private UIWork uiWork;   
}   


I think the code above can be optimized to this:

Code:
@MappedSuperclass 
public abstract class Work {   
  @OneToMany(mappedBy="work")   
  private List<Employee> employees;   
}   
 

@Entity 
public class CodeWork extends Work{   
}   
 
 
@Entity 
public class UIWork extends Work{   

}   
 
 
@Entity 
public class Employee{   
  @ManyToOne
  @JoinColumn(name="work_id", nullable=false, updatable= false)   
  private Work work;   
}


But after that, it will throw the org.hibernate.AnnotationException: @OneToOne or @ManyToOne on Employee.work references an unknown entity: Work.

I know the reason is that Work is not a Entity class. Could anyone explain me how to do this?
Thank you in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 15, 2007 9:58 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
You would need to model this as an entity hierarchy and not by using a @MappedSuperclass. The details depends on which strategy you want to use for mapping entity hierarchies to tables. Your options are:

Single table per hierarchy: CodeWork and UIWork would be stored in one table together with the persistent properties defined on Work. It requires the presence of a discriminator column whose value decides to which subclass a row in the table belongs.

Table per concrete class: Separate tables for CodeWork and UIWork each containing all persistent properties defined on Work. For polymorphic queries, e.g. queries against Work, an SQL UNION is required.

Joined subclass: Separate tables for CodeWork, UIWork and Work. Here SQL JOINS are required to instantiate the subclasses. It may also require the presence of a discriminator column in the Work table whose value decides to which subclass a row in the table belongs.

This is described in more detail in both the JPA specification and the Java Persistence with Hibernate book.

Without knowing more about your table and/or data model it is hard to say which strategy will fit best your needs. The 'Single table per hierarchy' is often the simplest to deal with.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 16, 2007 1:58 pm 
Newbie

Joined: Sat Dec 15, 2007 3:33 am
Posts: 2
I'm a newbie at this. Thanks a lot!


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