1

Given this class:

@MappedSuperclass
public abstract class AbstractEntity {

    int id;
    public void setId(int id) { this.id = id; }
    public int getId() { return id; }

    // other mappings
}

I want to define an entity:

@Entity
public class SomeEntity extends AbstractEntity {

    @Override
    @Id // or @OneToOne etc.
    public int getId() { return id; }
}

But this fails with a "No identifier specified" (or a "Could not determine type for") error on SomeEntity. If I remove the getter from the superclass it works. Can't I do this override strategy? Why not, or if yes - how?

Adding

@AttributeOverride(name = "id", column = @Column(name = "ID"))

to the subclass does not change the error.

Mark
  • 2,167
  • 4
  • 32
  • 64
  • Possible duplicate of [Hibernate : How override an attribute from mapped super class](https://stackoverflow.com/questions/5257921/hibernate-how-override-an-attribute-from-mapped-super-class) – crizzis Jun 07 '17 at 13:52
  • @crizzis That answer doesn't solve the problem. – Mark Jun 07 '17 at 14:08
  • JPA inheritance follows java inheritance, so you can override the column the used within the database, but not the mapping itself. So there is no way to change the ID column used once set, nor mark an existing field as the ID in a subclass. You can trick things by having JPA pick up fields in your abstract class (name them slightly different and mark them transient) and then define the mappings on the properties in your subclasses, see https://stackoverflow.com/questions/13874528/what-is-the-purpose-of-accesstype-field-accesstype-property-and-access to change access types – Chris Jun 07 '17 at 16:21
  • @Chris Thank you. `So there is no way to... mark an existing field as the ID in a subclass.` might tell me that i'm trying to do something the wrong way. Which of the answers should I be looking at in the link since there are contradictory opinions there. – Mark Jun 07 '17 at 16:30
  • What's the aim, why you want to do this? And show please other mappings in both classes. – Popandopolos Jun 07 '17 at 18:15
  • There was only https://stackoverflow.com/a/13874900/496099 that I'm aware of, which shows how you might use access type differences to avoid mapping the 'id' and other fields in the base so that subclasses can override the mappings. Ie use id_, mark it as transient, then specify property access on the subclass that getId has the ID annotation. As you say though, there is probably a better way to get what you want. – Chris Jun 07 '17 at 19:09
  • @Popandopolos This is what I want to do: https://stackoverflow.com/questions/44362748/what-is-a-good-persistence-design-for-this-entity-hierarchy – Mark Jun 08 '17 at 04:05
  • @Chris Then can you have a look at the link I gave in the previous comment maybe you can recommend something? – Mark Jun 08 '17 at 04:06

1 Answers1

-1

For you to create an entity class there are requirements that the class must meet. Ex. must have a public/private constructor.

Here is the list of requirements:

http://docs.oracle.com/javaee/5/tutorial/doc/bnbqa.html

Hope this helps.

RostSunshine
  • 216
  • 4
  • 14