I am working on a Spring Hibernate project where I have three classes
BaseEntity
, Person
& Owner
.
Person
extends BaseEntity
, Owner
extends Person
.
public class BaseEntity {
private Integer id;
Getters & Setters
public class Person extends BaseEntity {
private String firstName;
private String lastName;
Getters & Setters
@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.
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
@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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With