java Hibernate Annotations - @MappedSuperclass 如何覆盖列标识符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14229060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate Annotations - @MappedSuperclass How to override column identifier
提问by kmualem
I'm working with Hibernate Annotations and the issue that I'm trying to solve goes as follows:
我正在使用 Hibernate Annotations,我试图解决的问题如下:
I need to have 2 different @Entity classes with the same columns mapping but with a different Identifier.
我需要有 2 个不同的 @Entity 类,它们具有相同的列映射但具有不同的标识符。
The first one should use id as identifier.
第一个应该使用 id 作为标识符。
The second should use name as identifier.
第二个应该使用名称作为标识符。
So, I have an abstract class, annotated with @MappedSuperclass that have all of the columns including id and name, and in addition 2 @Entity classes that extends the super class and overriding the getters of the id and name.
所以,我有一个抽象类,用@MappedSuperclass 注释,其中包含所有列,包括 id 和 name,另外还有 2 个 @Entity 类,它们扩展了超类并覆盖了 id 和 name 的 getter。
@MappedSuperclass
public class MappingBase {
protected Integer id;
protected String name;
@Column (name = "ID")
public void getId() {
return this.id;
}
@Column (name = "NAME")
public void getName() {
return this.name;
}
}
@Entity
@Table (name = "TABLE")
public class Entity1 extends MappingBase {
@Id
@Column (name = "ID")
public void getId() {
return this.id;
}
}
@Entity
@Table (name = "TABLE")
public class Entity2 extends MappingBase {
@Id
@Column (name = "NAME")
public void getName() {
return this.name;
}
}
Note: I must have the members (id,name) in the super class. I know that i can add @Transient to the id and name getters but this means that i must add both of them in each class and it's not a good design :( In addition, the following insertable="false, updateable=false can help but i don't understand what is the meaning of this...
注意:我必须在超类中有成员 (id,name)。我知道我可以将 @Transient 添加到 id 和 name getter 但这意味着我必须在每个类中都添加它们,这不是一个好的设计:( 此外,以下 insertable="false, updateable=false 可以提供帮助但我不明白这是什么意思...
Please help me!
请帮我!
回答by Puneet Aggarwsal
回答by misbah
Hibernate/JPA allows us to annotate either properties or accessors. If we have @Id
annotation on a property, JPA will lookup all the properties of the class. Similarly, if we have @id
annotation on a getter method, JPA will lookup all the getters.
Hibernate/JPA 允许我们注释属性或访问器。如果我们@Id
对某个属性进行了注释,JPA 将查找该类的所有属性。同样,如果我们@id
在 getter 方法上有注解,JPA 将查找所有的 getter。
We can solve the above problem by annotating properties instead. The superclass and the two subclasses will be as follows-
我们可以通过注释属性来解决上述问题。超类和两个子类如下:
@MappedSuperclass
public abstract class AbstractMappingBase {
//properties other than id and name
public abstract Integer getId();
public abstract String getName();
//other getters and setters
}
@Entity
public class Entity1 extends AbstractMappingBase {
@Id
private Integer id;
private String name;
@Override
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity
public class Entity2 extends AbstractMappingBase {
private Integer id;
@Id
private String name;
@Override
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Here JPA will look for properties instead of getters. There are no duplicate properties between superclass and its subclasses. So it will work fine.
这里 JPA 将寻找属性而不是 getter。超类与其子类之间没有重复的属性。所以它会正常工作。
回答by dinukadev
You are much better off defining your base class as @Embeddable and using @Embedded in your implementation classes with the use of @AttributeOverride.
最好将基类定义为@Embeddable 并在实现类中使用@Embedded 并使用@AttributeOverride。
回答by kmualem
If i remember correctly, I simply defined 2 @Entity classes with the same table that inherits from one abstract @MappedSuperclass class. The super class contains the id member and each Entity class define it's own @Id @Column definition. It should work!
如果我没记错的话,我只是简单地定义了 2 个 @Entity 类,该类具有从一个抽象的 @MappedSuperclass 类继承的同一个表。超类包含 id 成员,每个实体类定义它自己的 @Id @Column 定义。它应该工作!