File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Primary Key Autogeneration for InheritenceType. TABLE_PER_CLASS

 
Muralidhar Adhikarla
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;

import javax.persistence.*;

@Entity
@Inheritance
(strategy = InheritanceType.
TABLE_PER_CLASS)

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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what if I am using a database like MySql that does not support sequence or table strategy for identifiers?
 
Artur Nowak
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic