Hibernate Polymorphism.EXPLICIT annotation doesn't work?

懵懂的女人 提交于 2019-12-21 17:22:36

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/17277500/hibernate-polymorphism-explicit-annotation-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!