hes*_*ati 5 hibernate hierarchical-data hibernate-annotations
我知道有一些关于这个的帖子,但它们大约一年没有回复.实际上我们在PostgreSQL 8.4上使用Hibernate 4.2.1.Final.我们有两个像这样的实体
实体A(顶级层次结构类)
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Polymorphism(type = PolymorphismType.EXPLICIT)
public class A {
@Id
@GeneratedValue
private Long id;
public A() {
}
public A(Long id) {
super();
this.id = id;
}
// Setters, getteres, hashCode and equals
}
Run Code Online (Sandbox Code Playgroud)
实体B(子类)
@Entity
public class B extends A{
String value;
public B(){
}
public B(String value) {
super();
this.value = value;
}
public B(Long id, String value) {
super(id);
this.value = value;
}
// Setters, getteres, hashCode and equals
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,实体使用注释PolymorphismType.EXPLICIT但在使用时获取顶级类
ArrayList<A> lista = (ArrayList<A>) session.createCriteria(A.class).list();
Run Code Online (Sandbox Code Playgroud)
我们也使用String value属性获取B子类.实际上,SQL语句包含left outer join B.这仍然是第四版Hibernate中的一个错误,或者我做错了什么?
最好的祝福
在我看来,相关文档和功能本身非常混乱.从第5章开始:
显式多态性意味着只有显式命名该类的查询才会返回类实例.
这将告诉我你的查询应该工作.但是你想要做的事情似乎并不是他们的意图,你可以在后面的同一段中看到:
当两个不同的类映射到同一个表时,显式多态性很有用.这允许包含表列子集的"轻量级"类.
他们在这里谈论的是拥有B并A映射到同一个表,但没有实际的类关系.您可以在旧的JIRA票证中重复此观点.我想这意味着如果他们没有类关系,但是在同一个表中,那么你可以使用IMPLICIT多态来同时获得相同的查询,但这看起来完全奇怪,因为他们不共享Java子类.
所以,总结是PolymorphismType.EXPLICIT你不会做你认为它做的事情.在我看来,根据上面的第一个引用,它应该做你期望的事情.