Java 在Hibernate中从数据库加载属性?
我有三个类具有类层次结构Java 在Hibernate中从数据库加载属性?,java,hibernate,Java,Hibernate,我有三个类具有类层次结构 ParentClass.java having commons properties used for both ChildClass1 and ChildClass2. ChildClass1扩展了ParentClass 将属性用于这个ChildClass1+它还使用父类中的一些公共属性 ChildClass2扩展了ParentClass 将属性用于这个ChildClass2+时,它还使用父类中的一些公共属性 所有属性都可用于具有两列的表中 **Key
ParentClass.java
having commons properties used for both ChildClass1 and ChildClass2.
将属性用于这个ChildClass1+它还使用父类中的一些公共属性ChildClass1扩展了ParentClass
将属性用于这个ChildClass2+时,它还使用父类中的一些公共属性ChildClass2扩展了ParentClass
**Key value** Type
---------------------------------------
propertyKey1 propertyValue1 Child1
propertyKey2 propertyValue2 Child1
propertyKey3 propertyValue3 Child2
propertyKey4 propertyValue4 Child2
propertyKey5 propertyValue5 CommonPorperty
.. .. ..
propertyKeyn propertyValuen ..
现在我不确定如何从hibernate继承加载它们
为愚蠢的问题道歉
提前感谢您需要添加一个列“Discriminator”,以通知Hibernate在使用多个类型处理同一个表时应该加载哪个实例。 见示例:
@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Table(name = "PARENT_TABLE")
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.INTEGER)
public abstract class ParentEntity implements java.io.Serializable {
private long id;
private String commonValue1;
@Id
@Column(name = "ID", nullable = false)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "Common_Value1")
public String getCommonValue1(){
return commonValue1;
}
public void setCommonValue1(String commonValue1){
this.commonValue1 = commonValue1;
}
}
@Entity
@DiscriminatorValue("1")
public class ChildEntity1 extends ParentEntity {
private String child1Value;
@Column(name = "Child1_Value")
public String getChild1Value(){
return child1Value;
}
public void setChild1Value(String child1Value){
this.child1Value = child1Value;
}
}
@Entity
@DiscriminatorValue("2")
public class ChildEntity2 extends ParentEntity {
private String child2Value;
@Column(name = "Child2_Value")
public String getChild2Value(){
return child2Value;
}
public void setChild2Value(String child2Value){
this.child2Value = child2Value;
}
}
你说的是两种属性吗?hibernate实体的属性是一个具有特定名称的java字段,但看起来您的属性名称是表中的一个值。。。。请重新编写。正在检查描述符…可能重复感谢@hsnkhrmn..但没有名为“Child1_Value”的列。和Child2_值,那么我们如何使用它呢?这里我的问题是没有关于子类的特定列。我使用的值只是示例,您需要根据您的数据模型进行更改。基本上,您将在父实体上放置公共列(并进行映射),类的特定列是特定表(Child1或Child2)是的,您是对的。。。。hsnkhrmn。。但是在这里我没有任何关于chidl1或child2的专栏。只有propertykye特定于child1或child2。我明白了,这些字段在DB上没有映射。它们只是你身体上的瞬变磁场,对吗?如果是这样,您需要用@Transient标记它们的getter,以避免混淆hibernate。子类可能有也可能没有附加列,这不是必需的。Hibernate只是通过Discriminator列来理解它(如果没有,您应该添加)