【问题标题】:Hibernate annotation mapping for java extended classesjava扩展类的Hibernate注解映射
【发布时间】:2014-01-22 00:10:41
【问题描述】:

我正在开发一个 Spring Hibernate 项目,其中我有三个类 BaseEntity, Person & Owner.

Person 扩展BaseEntityOwner 扩展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

现在我想将三个类的所有属性映射到一个表中 是Owner。谁能帮我映射一下?

我有一个基于 xml 的映射 & 我想在注释中做它

<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>

我想过使用每个类继承映射的表,但在 xml 中我看不到 正在使用鉴别器列。

【问题讨论】:

    标签: java hibernate jakarta-ee jpa hibernate-mapping


    【解决方案1】:

    作为映射超类的具体类

    使用@MappedSuperclass注解。

    JPA (...) 定义了通过 @MappedSuperclass 注释或元素定义的映射超类概念。映射的超类不是持久类,但允许为其子类定义通用映射。

    更多:Java Persistence/Inheritance

    示例

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

    也看看这个问题:Hibernate : How override an attribute from mapped super class

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多