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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With