6

I have been looking into JPA/Hibernate @Entity inheritance for a while now and can't seem to find anything that addresses what I am trying to achieve.

Basically I want to be able to define an @Entity with all of the column and table mappings as required. Then I want to be able to extend the @Entity in a number of different locations with different sets of @Transient methods defined in the body of each "sub-Entity". This is a basic example of what I am trying to achieve but with no success thus far:

@Entity
@Table(name = "mountain")
public class MountainEntityBase implements Serializable {
    public Integer mountainId = 0;
    public Integer height = 0;

    public List<ExplorerEntityBase> explorers = new ArrayList<ExplorerEntityBase>();

    @Id
    @GeneratedValue
    @Column(name = "mountain_id")
    public Integer getMountainId() { return mountainId; }
    public void setMountainId(Integer mountainId) { this.mountainId = mountainId; }

    @Column(name="height")
    public String getHeight() { return height; }
    public void setHeight(String height) { this.height = height; }

    @OneToMany(mappedBy="mountainId")
    public List<ExplorerEntityBase> getExplorers() { return this.explorers; }
    public void setExplorers(List<ExplorerEntityBase> explorers) { this.explorers = explorers; }

}    

.

@Entity
public class MountainEntity extends MountainEntityBase implements Serializable {

    public List<MountainEntity> allMountainsExploredBy = new ArrayList<MountainEntity>();

    @Transient
    public List<MountianEntity> getAllMountainsExploredBy(String explorerName){
        // Implementation 
    }
}

So any extended class will define only @Transients in its body. But also I want to allow for situations where the child class is empty:

@Entity
public class MountainEntity extends MountainEntityBase implements Serializable {
}

Thanks in advance for any help with this.

2
  • 2
    I don't understand what you want to do. In JPA, only fields are serialized, not methods. Only fields can be declared transcient: this keyword has no effect on methods. Commented May 29, 2011 at 21:39
  • Hi Benoit thanks for your reply and sorry for the confusion :P I have tried to clarify my original post. So basically it is the inheritance aspect of the entity that I want to get sorted before I get the actual transients implemented. So even if the child class is empty I can't seem to use it. I keep getting entity not mapped errors. Thanks again Commented May 29, 2011 at 22:06

1 Answer 1

9

Inheritance in JPA is specified on the root entity using the @Inheritance annotation. There you can specify the database representation of the hierarchy. Check the documentation for more details.

If your child classes define only transient fields (not methods) (i.e. not saved in the db), then perhaps a discriminator column is the best option. But it may be the case that you don't actually need inheritance - the main entity can have all the methods (because it has all the fields the methods operate on)

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

6 Comments

Hi, thanks for your reply. I am more interested in the inheritance aspect than the use of transients at the first pass. Basically I want to also cater for situations where the child class is also empty. (See original post [edited]). Is it possible to do this using the inheritance annotation? Thanks again
yes, it is possible. You will just have to specify a discriminator value. I linked the documentation - it's a must-read ;)
Is there a way to do this without adding a new column to every table in the database?
I don't think you should have many tables for inheritance that doesn't add any fields. It can be all in one table (with an additional discriminator column)
hibernate needs to have a criteria based on which to instantiate one subclass or another. If there is no discriminator field, hibernate cannot know which of many subclasses you want (or even - do you want to base class or the child)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.