gpt4 book ai didi

java - Hibernate 和 @AttributeOverride

转载 作者:行者123 更新时间:2023-12-01 11:15:35 24 4
gpt4 key购买 nike

我有 3 节课:

@MappedSuperclass
public class BaseEntity {
@Id
@GeneratedValue
private Long id;
...
}

@Entity
public class Person extends BaseEntity {
private String name;
...
}

@Entity
@AttributeOverride(name = "id", @Column = (name = "idStudent"))
public class Student extends Person {
private float avgGrades;
...
}

我想覆盖 ID 属性,以便在 Student 表中会有一个 idStudent 列,而不是 id。不幸的是,上面的代码不起作用。看起来 @AttributeOverride 只适用于您扩展的类(并且没有进一步的一步)。在我描述的情况下有什么方法可以覆盖属性名称(覆盖类中扩展我们正在扩展的类的属性)吗?

最佳答案

如果您知道默认继承类型是什么:SINGLE_TABLE,那么您的问题很容易理解。

这意味着扩展 Person 的所有实体都在同一个表中。因此 Person 已经定义了 ID 列。因为否则您将违反 Person 表的主键列的约定。

我还引用了@AttributeOverride的JavaDoc:

May be applied to an entity that extends a mapped superclass or to an embedded field or property to override a basic mapping or id mapping defined by the mapped superclass or embeddable class (or embeddable class of one of its attributes).

在此处提问之前,先阅读 JavaDoc 总是有帮助的。

你能做些什么呢?让您的 Person 成为 @MappedSuperclass(或创建一个 BasePerson)。

关于java - Hibernate 和 @AttributeOverride,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31865191/

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