Issue
This is the scenario:
I have 2 classes mapped with hibernate: "Foo" and "EnhancedFoo"
both classes are mapped to the same table "foo"
EnhancedFoo extends Foo
Foo.hbm.xml contains a named query "find active foos" which goes like this:
from Foo foo where foo.active = true
Now, if I try to load the configuration I get this exception:
could not resolve property: active of: EnhancedFoo [
from EnhancedFoo foo where foo.active = true
]
which is kind of correct since in EnhancedFoo.hbm.xml there isn't a mapped property "active", but then why is hibernate replacing "Foo" with "EnhancedFoo"?
This is what I've tried:
tried to add a "enity-name" attribute on the mapping files like so:
<class name="myproject.data.entity.Foo" table="foo" entity-name="Foo">
.......
<class name="myproject.data.entity.EnhancedFoo" table="foo" entity-name="EnhancedFoo">
and then configuration loads fine, but whenever I try to insert a Foo I get this:
org.hibernate.MappingException: Unknown entity: myproject.data.entity.Foo
These are the relevant parts of code:
//Foo.java
public class Foo
{
private long id;
private boolean active;
// getters and setters
}
//EnhancedFoo.java
public class EnhancedFoo extends Foo
{
private String extraProperty
// getter and setter
}
//Foo.hbm.xml
//.....
<class name="myproject.data.entity.Foo" table="foo" entity-name="Foo">
<id column="id" name="id">
<generator class="assigned"/>
</id>
<property name="active" column="active" />
</class>
<query name="find_active_foos">
<![CDATA[
from Foo foo where foo.active = true
]]>
</query>
//EnhancedFoo.hbm.xml
//.....
<class name="myproject.data.entity.EnhancedFoo" table="foo" entity-name="EnhancedFoo">
<id column="id" name="id">
<generator class="assigned"/>
</id>
</class>
Solution
I see two problems:
1) You never should both map a superclass and a subclass. The problem is, the subclass instance also is an instance of the superclass, in your example an EnhancedFoo instance also is an instance of Foo. This confuses hibernate when looking in the session cache. (I didn't have your problem, but I had the effect, when I loaded the superclass instance with the primary key value as the condition then uniqueResult() failed, because it found in the cache one instance of the superclass and one of the subclass.) Your replaced Foo also might be caused be a similar effect.
If you want to continue with the two mappings, you can do like this
//AbstractFoo.java
public abstract class AbstractFoo
{
private long id;
private boolean active;
// getters and setters
}
//Foo.java
public class Foo extends AbstractFoo
{ // empty body
}
//EnhancedFoo.java
public class EnhancedFoo extends AbstractFoo
{
private String extraProperty
// getter and setter
}
and you let the mapping files as they are.
(An other possibility is to map only one class and handle the differences in Java.)
2) For your insert problem:
You got the error message Unknown entity: myproyect.data.entity.Foo
with 'y' instead of 'j' in project. Do you have a typo anywhere there?
Answered By - Johanna