Hi,
Should the find method of entity manager return null if a subclass is not found (although the instance exists in the base class)?
Let me explain better. I have the following entities:
Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class BaseClass implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Basic
private String text;
public BaseClass() {
}
public BaseClass(String text) {
this.text = text;
}
public int getId() {
return id;
}
public String getText() {
return text;
}
}
@Entity
public class B extends A {
@Basic
private String extraText;
public B() {
}
public B(String baseText, String extraText) {
super(baseText);
this.extraText = extraText;
}
public String getExtraText() {
return extraText;
}
}
Next I create to records.
Code:
A a = new A("some text");
manager.persist(a);
int id1 = a.getId();
B b = new B("some text", "another extra text");
int id2 = b.getId();
// Try to locate a B instance with id1
// Should return null but instead it returns ClassCastException
b = manager.find(B.class, id1);
My question is: shouldn't the last line returns
null instead of ClassCastException. The specification states that the find method returns null if no instance of type B exists in the persistence store. But in the above code it returns an A instance instead of null (and the consequent ClassCastException)...
I checked the SQL generated by Hibernate and it seems that for the Joined inheritance type it uses a
Left Outer Join. Shouldn't be a
Inner Join instead?
Best regards,
Vítor Carreira
PS. Using hibernate 3.2.0.cr4 / hibernate EM 3.2.0CR2 / postgresql 8