gpt4 book ai didi

java - Java 扩展类的 Hibernate 注解映射

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:39:24 25 4
gpt4 key购买 nike

我正在做一个 Spring Hibernate 项目,我有三个类BaseEntityPersonOwner

Person 扩展 BaseEntityOwner 扩展 Person

基础实体.java

public class BaseEntity {

private Integer id;

Getters & Setters

人.java

public class Person extends BaseEntity {

private String firstName;

private String lastName;

Getters & Setters

所有者.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

现在我想将三个类的所有属性映射到一个表中是 所有者。任何人都可以帮助我如何映射?

我有一个基于 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 中我看不到正在使用鉴别器列。

最佳答案

作为映射父类(super class)的具体类

使用@MappedSuperclass注释。

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.

更多: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

关于java - Java 扩展类的 Hibernate 注解映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20918634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com