Hibernate Polymorphism.EXPLICIT注解不起作用? [英] Hibernate Polymorphism.EXPLICIT annotation doesn't work?

查看:829
本文介绍了Hibernate Polymorphism.EXPLICIT注解不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有关于此的一些帖子,但他们大概一年没有回应。其实我们在PostgreSQL 8.4上使用Hibernate 4.2.1.Final。我们有两个这样的实体



实体A(顶级层次类)

<$ p
@Inheritance(strategy = InheritanceType.JOINED)
@Polymorphism(type = PolymorphismType.EXPLICIT)
public class A {
@ Id
@GeneratedValue
私人长ID;
$ b $ public A(){

}

public A(Long id){
super();
this.id = id;
}

// Setter,getteres,hashCode和等于
}

实体B(子类)

  @Entity 
public class B扩展A {
字符串值;
$ b $ public B(){

}

public B(字符串值){
super();
this.value = value;
}

public B(Long id,String value){
super(id);
this.value = value;
}

// Setter,getteres,hashCode和等于
}

正如你所看到的,一个实体被注解为 PolymorphismType.EXPLICIT ,但是当用

获取顶级类时

  ArrayList< A> lista =(ArrayList< A>)session.createCriteria(A.class).list(); 

我们也得到了B子类以及字符串值属性。实际上,SQL语句包含 left left outer join B 。这仍然是第四版Hibernate的错误,或者我做错了什么?



最好的祝福

解决方案

在我看来,相关文档和功能本身非常混乱。从第5章


明确的多态性意味着类实例将仅由显式命名该类的查询返回。


这会向我表明您的查询工作。但你想做的事似乎并不是他们的意图,你可以在同一段后面看到:


显式多态性当两个不同的类映射到同一个表时,它非常有用这允许包含表列的子集的轻量级类。 >他们在这里讨论的是将 B A 映射到同一个表,但没有实际的类关系。你可以在旧的 JIRA中看到这种观点票。我想这意味着如果他们没有类关系,但是在同一个表中,那么你可以使用 IMPLICIT 多态来获得两个相同的查询,但是这看起来很奇怪,因为它们不共享Java子类。



因此,总结是 PolymorphismType.EXPLICIT 不会做你认为它做的事。在我看来,它应该根据上面的第一个引用来做你期望的。

I know there are some post about this, but they are about a year and no response. Actually we are using Hibernate 4.2.1.Final over PostgreSQL 8.4. We have two entitys like this

Entity A (Top Hierarchy Class)

@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
}

Entity B (Subclass)

@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
}

As you can see, A entity is annotated with PolymorphismType.EXPLICIT but when fetching the top class with

ArrayList<A> lista = (ArrayList<A>) session.createCriteria(A.class).list();

we are getting the B subclasses too with the String value property. In fact, the SQL statement contains left outer join B. Is this still a bug in the fourth version of Hibernate or I'm doing something wrong?

Best regards

解决方案

In my opinion, the relevant documentation and the feature itself is very confusing. From Chapter 5:

Explicit polymorphisms means that class instances will be returned only by queries that explicitly name that class.

which would indicate to me that your query should work. But what you're trying to do does not appear to be their intention, as you can see later in the same paragraph:

Explicit polymorphisms is useful when two different classes are mapped to the same table This allows a "lightweight" class that contains a subset of the table columns.

What they're talking about here is having B and A map to the same table, but have no actual class relationship. You can see this opinion repeated in an old JIRA ticket. I guess that means that if they didn't have a class relationship, but are in the same table, then you could use IMPLICIT polymorphism to get both with the same query, but that seems totally bizarre given they don't share a Java subclass.

So, the summary is that PolymorphismType.EXPLICIT doesn't do what you think it does. In my opinion, it should do what you're expecting, based on the first above quote.

这篇关于Hibernate Polymorphism.EXPLICIT注解不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆