-->
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: Overriding the @Id annotation
PostPosted: Tue Jan 12, 2010 6:44 pm 
Beginner
Beginner

Joined: Thu Jan 29, 2009 1:24 am
Posts: 20
I have two classes, A and B. Class A is a mapped superclass that contains a particular property marked with the @Id annotation. In Class B I want to specify a different property as the primary key. From what I have seen of the JPA API the @AttributeOverride only applies to the @Column annotation. So is it possible to override primary keys in a subclass?

I have also been trying to learn about @IdClass. If I were to specify an @IdClass in my base class that utiltized the standard id and then I specify a different @IdClass in Class B will that override all keys and give me what I want?

Thanks in advance


Top
 Profile  
 
 Post subject: Re: Overriding the @Id annotation
PostPosted: Wed Jan 13, 2010 3:46 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi caskater4,

why would you want to override the ID? I cannot see any need for that. The subclass will inherit the ID field and its property of being the primary key. This way Hibernate can support polymorphic queries.

If you have another candidate for a primary key in your subclass then you should be able to enforce data integrity with a UNIQUE constraint on that candidate, I suppose.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Overriding the @Id annotation
PostPosted: Wed Jan 13, 2010 4:03 pm 
Beginner
Beginner

Joined: Thu Jan 29, 2009 1:24 am
Posts: 20
Hi Froestel,

Thanks for the reply. On the one hand I completely agree that the ID should never be overridden. However I have the scenario where a property that is not the ID is the most commonly used reference to the object which unfortunately skips ehcache whenever a search is performed for that object.

To better explain my scenario imagine having an entity that stores application settings. Since it's bad practice (and unstable) to code against a database generated ID the settings are referenced by it's unique name property instead which is always gauranteed (and simpler for the programmer). Since the name property is not an ID any searches for the object goes past ehcache and right to the database causing a big performance issue. While I could make these objects their own entity class our system is designed to be generic requiring every entity class to have a common base class.

I'm pretty sure this must be a common phenomena for design and there's got to be an appropriate solution for such scenarios.


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.