2

I'm working with Hibernate Annotations and the issue that I'm trying to solve goes as follows:

I need to have 2 different @Entity classes with the same columns mapping but with a different Identifier.

The first one should use id as identifier.

The second should use name as identifier.

So, I have an abstract class, annotated with @MappedSuperclass that have all of the columns including id and name, and in addition 2 @Entity classes that extends the super class and overriding the getters of the id and name.

@MappedSuperclass public class MappingBase { protected Integer id; protected String name; @Column (name = "ID") public void getId() { return this.id; } @Column (name = "NAME") public void getName() { return this.name; } } @Entity @Table (name = "TABLE") public class Entity1 extends MappingBase { @Id @Column (name = "ID") public void getId() { return this.id; } } @Entity @Table (name = "TABLE") public class Entity2 extends MappingBase { @Id @Column (name = "NAME") public void getName() { return this.name; } } 

Note: I must have the members (id,name) in the super class. I know that i can add @Transient to the id and name getters but this means that i must add both of them in each class and it's not a good design :( In addition, the following insertable="false, updateable=false can help but i don't understand what is the meaning of this...

Please help me!

2
  • Did you got a way to do this, I too have same situation where I need to override the @Id field in the subclass Commented Nov 25, 2015 at 9:18
  • You can't override the id. You can if you put the @Id annotation on the getter, but then you can't have your column annotations on your fields, they all must be on the getters/setters as well, since jpa doesn't support mixed annotations on entities. Build 2 hierarchies with 2 mappedsupperclasses Commented Jan 24, 2020 at 8:51

3 Answers 3

3

Hibernate/JPA allows us to annotate either properties or accessors. If we have @Id annotation on a property, JPA will lookup all the properties of the class. Similarly, if we have @id annotation on a getter method, JPA will lookup all the getters.

We can solve the above problem by annotating properties instead. The superclass and the two subclasses will be as follows-

@MappedSuperclass public abstract class AbstractMappingBase { //properties other than id and name public abstract Integer getId(); public abstract String getName(); //other getters and setters } @Entity public class Entity1 extends AbstractMappingBase { @Id private Integer id; private String name; @Override public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Override public String getName() { return name; } public void setName(String name) { this.name = name; } } @Entity public class Entity2 extends AbstractMappingBase { private Integer id; @Id private String name; @Override public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Override public String getName() { return name; } public void setName(String name) { this.name = name; } } 

Here JPA will look for properties instead of getters. There are no duplicate properties between superclass and its subclasses. So it will work fine.

Sign up to request clarification or add additional context in comments.

Comments

1

You are much better off defining your base class as @Embeddable and using @Embedded in your implementation classes with the use of @AttributeOverride.

2 Comments

I thought that this is the way to create a composite identifier, in addition i can't wrap those fields, i need them as class members and not a part from another object. Can you please advise ?
@Kobim , have you find a way to make the overide ?
0

If i remember correctly, I simply defined 2 @Entity classes with the same table that inherits from one abstract @MappedSuperclass class. The super class contains the id member and each Entity class define it's own @Id @Column definition. It should work!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.