Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Persisting role object pattern

 
Santiago Seifert
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't map User as an Entity, either don't map it at all, or use a @MappedSuperclass.

Otherwise move the id to User.
 
Santiago Seifert
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic