-->
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.  [ 10 posts ] 
Author Message
 Post subject: Overriding @Id in a derived class
PostPosted: Wed Jan 03, 2007 8:33 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
I have two Pojos, Person and Employee.
Employee inherits from Person.
I wish to use the autogeneration facility for the primary keys where the primary key is generated from a sequence table.

For Person I do this:
@Id
@SequenceGenerator(name="SPERSON", sequenceName="SPERSON")
@GeneratedValue(generator="SPERSON")
public int getI() {
return i;
}

Which is fine. However, for Employee (which inherits from Person) I wish for it's primary key to be generated from the a different sequence table, I attempt to do:

@Id
@SequenceGenerator(name="SEMPLOYEE", sequenceName="SEMPLOYEE")
@GeneratedValue(generator="SEMPLOYEE")
public int getI(){
return i;
}

However throws the following exception:

javax.persistence.PersistenceException: org.hibernate.AnnotationException: Unable to define/override @Id(s) on a subclass: Employee

Is it possible to arrange the Employee pojo so that it can generate it's primary key from a unique sequence table?

The reason I want to do this is to fit in with a legacy system.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 4:13 pm 
Beginner
Beginner

Joined: Sun Nov 16, 2003 3:04 pm
Posts: 24
How are the two tables related? If they both have different Ids, then there is no real relationship, is there? Although they share the same pieces of data, if they aren't sharing the same Ids, then you have two tables with similar data.

If you are just starting your application, I'd suggest making the ids the same for both (just a serialized number), then have a 'person_id' and 'employee_id' that are generated, but not part of the primary key.

If you are slipping hibernate into a legacy system, then we'll need more information about both tables and how they relate...

Hope that helps...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 04, 2007 7:47 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
fortknox wrote:
Hope that helps...

Thanks for your reply. I am trying to use hibernate to integrate with an existing system.
The tables form an inheritance hierarchy i.e. Employee extends Person.
However they each have their own sequence table SEmployee and SPerson from which they generate their own Id.
So I need to point the @Id in the Employee pojo to a different sequence table to Person, but how can I do this if I cannot override the annotation?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 04, 2007 11:14 am 
Beginner
Beginner

Joined: Sun Nov 16, 2003 3:04 pm
Posts: 24
Unfortunately, you are at the end of my knowledge of inheritence within hibernate with annotations. The obvious work-around would to take out the inheritence on the code side and put in code to handle the inheritence properties. For this particular situation, I do not know of any real 'quick' or 'graceful' fix. Hopefully a hibernate guy will pop in and be able to answer this better than I...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 06, 2007 12:46 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Some people have successfully done something like that
Code:
@MappedSuperclass
public class Person {
  @Id @generatedValue(strategy="myStrategy")...


@Entity
@GenericGenerator(name="myStrategy", sequenceName="myspecialname")
public class Employee {


...

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 7:02 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Hi,
Thanks for your replies.
Unfortunately, I need to persist both Person and Employee entities. If I use @MappedSuperclass for Person, I cannot persist Person entites.

It looks like if Employee inherits from Person I cannot override the @Id annotation which I need to do, if both are to use their own sequence tables - this appears a limitation with hibernate / JPA?

Your comments are welcomed.


Top
 Profile  
 
 Post subject: update
PostPosted: Mon Jan 08, 2007 10:52 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Hi,
I found the place in the hibernate code where an @ID annotation is not allowed to be overriden in aninherited class.

It is in org.hibernate.cfg.AnnotationBinder.bindId

if ( ! ( persistentClass instanceof RootClass ) ) {
throw new AnnotationException(
"Unable to define/override @Id(s) on a subclass: "
+ propertyHolder.getEntityName()
);
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 1:35 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
no there are deeper constraints to prevent @id on subclasses

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 6:22 am 
Newbie

Joined: Wed Oct 31, 2007 5:15 am
Posts: 6
Hello all,

this is maybe late, but I encountered the same problem: I wanted to use different sequences to get the primary key from a sequence generator. And each entity has to get its Id from a different sequence.

I did this by writing a MultiSequenceGenerator extending a SeqHiLoGenerator and taking into param a map fullyQualifiedClassName=sequenceName .

I maintain a map of SeqHiLoGenerators in my class, and a default generator.
If I get a generate request for an objet, I return the generator.generate(session, object) otherwise, the super.generate(session, object).


I can post the code if someone is interested.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 3:05 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you can post that in the community area section of the wiki

_________________
Emmanuel


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