4

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.

1 Answer 1

12

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

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

Comments

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.