Question

I am working on a Spring Hibernate project where I have three classes BaseEntity, Person & Owner.

Person extends BaseEntity, Owner extends Person.

BaseEntity.java

public class BaseEntity {

    private Integer id;

        Getters & Setters 

Person.java

public class Person extends BaseEntity {

    private String firstName;

    private String lastName;

       Getters & Setters 

Owner.java

@Entity
@Table(name="OWNERS")
public class Owner extends Person {

    @Column(name="ADDRESS")
    private String address;

    @Column(name="CITY")
    private String city;

    @Column(name="TELEPHONE")
    private String telephone;

    Getter and Setters

Now I want to map all the properties of the three classes in a single table which is Owner. Can anyone please help me how do I map?

I have a xml based mapping for this & I want to do it in annotation

<class name="org.springframework.samples.petclinic.Owner" table="owners">
        <id name="id" column="id">
            <generator class="identity"/>
        </id>
        <property name="firstName" column="first_name"/>
        <property name="lastName" column="last_name"/>
        <property name="address" column="address"/>
        <property name="city" column="city"/>
        <property name="telephone" column="telephone"/>
    </class>

I thought of using table per class inheritance mapping but here in xml I see no discriminator column being used.

Was it helpful?

Solution

Concrete class as a mapped superclass

Use @MappedSuperclass annotation.

JPA (...) defines a mapped superclass concept defined though the @MappedSuperclass annotation or the element. A mapped superclass is not a persistent class, but allow common mappings to be define for its subclasses.

More: Java Persistence/Inheritance

Example

@MappedSuperclass
public class BaseEntity {
    ...
}

@MappedSuperclass
public class Person extends BaseEntity {
   ...
}

@Entity
@Table(name="OWNERS")
public class Owner extends Person {
    ...
}

Look also at this question: Hibernate : How override an attribute from mapped super class

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top