aspose file tools*
The moose likes Object Relational Mapping and the fly likes Persisting role object pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Persisting role object pattern" Watch "Persisting role object pattern" New topic
Author

Persisting role object pattern

Santiago Seifert
Greenhorn

Joined: Aug 03, 2011
Posts: 2
The title is quite self explanatory. I have a User class(abstract) which has 2 sublcasses: UserCore(concrete) and UserRole(abstract). UserRole has itself a few more subclasses. The problem lies in User which has no attributes and I want It to stay that way. I've been having a large number of excepcions but, if I'm right, everything comes to User's mapping.
The problem is that if I annotate it as Entity, it has no id. I wont be moving UserCore's and UserRole's id to their superclass since that would not be right. Otherwise, any reference to User in another mapped Entity in my model will cause an exception since User is an unknown entity. I have been stuck with this for long enough but did not find any suiting answer.
Any hints will be appreciated. Thanks in advance!
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Hi and welcome to the JavaRanch.

First some questions:
Santiago Seifert wrote:The problem lies in User which has no attributes and I want It to stay that way.
Why?
Santiago Seifert wrote:I wont be moving UserCore's and UserRole's id to their superclass since that would not be right.
Why?

And something like this works fine:




James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
Don't map User as an Entity, either don't map it at all, or use a @MappedSuperclass.

Otherwise move the id to User.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Santiago Seifert
Greenhorn

Joined: Aug 03, 2011
Posts: 2
Thanks for the replies. Well, i knew that moving the id up to the superclass was an option to make it work. I had a reason for not moving the ids up in the hierarchy, which I cant remember now. Neither the MappedSuperclass or leaving the abstract class unmapped wont work with a problem of Unknown entity wherever another entity holds reference of a User.
Do you think that there is no other choice when mapping a hierarchy like this with no attributes at the top of it?
Thanks, once again.
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
An option outside of JPA, using EclipseLink could be to use define an interface for User to use in the references and map to the User interface using a VariableOneToOne mapping.

Normal inheritance is probably still best.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Persisting role object pattern