Primary Key Autogeneration for InheritenceType. TABLE_PER_CLASS
Muralidhar Adhikarla
Greenhorn
Joined: Apr 01, 2007
Posts: 14
posted
0
Hi All, I am trying Exercise no 8.2 from the book Enterprise Javabeans 3.0(Oreilly). I encountered a weird behaviour . I cannot assign @Generated Value to the primary key of my Super Class while implementing the InheritenceType.TABLE_PER_CLASS mappings. package com.titan.domain;
public class Person implements java.io.Serializable { private int id; private String firstName; private String lastName;
@Id @GeneratedValue //this is giving an error.
public int getId() { return id; } public void setId(int id) { this.id = id; }
package com.titan.domain;
import javax.persistence.*;
@Entity public class Customer extends Person {......}
package com.titan.domain;
import javax.persistence.*;
@Entity public class Employee extends Customer {......}
While the Primary key Autogeneration works for InheritanceType. JOINED and InhertitanceType. SINGLE_CLASS, its giving me an error for the same in InheritenceType. TABLE_PER_CLASS.
Any Idea?
Muralidhar Adhikarla
Greenhorn
Joined: Apr 01, 2007
Posts: 14
posted
0
I am getting this error:
: Cannot use identity column key generation with <union-subclass> mapping for: om.titan.domain.Employee I Depend On: jboss.jca:service=DataSourceBinding,name=DefaultDS Depends On Me: jboss.j2ee:jar=titan.jar,name=DataAccessBean,service=EJB3
ObjectName: jboss.j2ee:jar=titan.jar,name=DataAccessBean,service=EJB3 State: NOTYETINSTALLED I Depend On: persistence.units:jar=titan.jar,unitName=titan
--- MBEANS THAT ARE THE ROOT CAUSE OF THE PROBLEM --- ObjectName: persistence.units:jar=titan.jar,unitName=titan State: FAILED Reason: javax.persistence.PersistenceException: org.hibernate.MappingExceptio : Cannot use identity column key generation with <union-subclass> mapping for: om.titan.domain.Employee I Depend On: jboss.jca:service=DataSourceBinding,name=DefaultDS Depends On Me: jboss.j2ee:jar=titan.jar,name=DataAccessBean,service=EJB3
Benjie Buenafe
Greenhorn
Joined: Apr 24, 2008
Posts: 1
posted
0
I also encounter the same error. JOINED and SINGLE_TABLE works fine but when I switch to TABLE_PER_CLASS I receive the same error.
Artur Nowak
Greenhorn
Joined: Dec 13, 2009
Posts: 4
posted
0
I know I'm a little late, but maybe this answer will help somebody anyways.
One cannot use IDENTITY for primary key generation strategy with conjunction with "table per concrete class" inheritance, because in such a case the identity column must be present in each table and its values must be mutually exclusive: in a case of auto-generated value we say that they must share a seed. For example, for the hierarchy proposed in the original post:
So it is clearly not possible to use IDENTITY generation strategy, because it cannot guarantee that e.g. value 3 won't appear in any other table but EMPLOYEE.
If the generation strategy isn't specified in @GeneratedValue annotation, GenerationType.AUTO is assumed which means that the persistence provider is free to choose whichever strategy it likes, IDENTITY included. So only specifying SEQUENCE or TABLE strategies will ensure portable behaviour.
EDIT: shortened horizontal bars that looked bad due to line wrapping
Deepak R Singh
Greenhorn
Joined: Feb 16, 2010
Posts: 2
posted
0
But what if I am using a database like MySql that does not support sequence or table strategy for identifiers?
Artur Nowak
Greenhorn
Joined: Dec 13, 2009
Posts: 4
posted
0
TABLE strategy for key generation is always available, because it can be performed by the persistence provider on its own (i.e. by performing SELECTs and INSERTs on a designated table). It is by far the most portable and flexible choice, but for some databases it's suboptimal performance-wise.
ragavendran krishnamoothy
Greenhorn
Joined: Feb 13, 2007
Posts: 17
posted
0
Artur,
I read in Mastering EJB 4th edition, that in EJB3.0 spec, its mentioned as single table per concrete class is not required to be supported.
do we still need to work this strategy out ? can you please clarify me this ?
- Ragav
Aj Deschanel
Ranch Hand
Joined: Oct 20, 2009
Posts: 40
posted
0
Is it possible to use TABLE_PER_CLASS strategy where each subclass has its own native generator ?
Maybe I can supply the same SEQ for both subclasses to make sure I will have mutually exclusive IDs.
My super class it's abstract and I do not really need a table for it.