Hello
I think I'm facing the same problem. I summarized my problem in the following very basic application. It looks like a very standard situation to me.
I got 2 model classes: Person and Doctor. Doctor inherits from person. I tried to save an Doctor in a Console application.
this are the 2 model classes:
The Person Class:
/** *@hibernate.class * discriminator-value="person" *@hibernate.discriminator * column="discrimator" * type="string" */ public class Person { Long id; String firstName; String lastName; public Person(){} public Person(String firstName,String lastName) { this.firstName = firstName; this.lastName = lastName; } /** * @hibernate.id * generator-class="native" */ public Long getId() { return id; } public void setId(Long id) { this.id=id; } /** *@hibernate.property */ public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName=firstName; }
/** *@hibernate.property */ public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName=lastName; } }
The Doctor Class:
/** *@hibernate.subclass * discriminator-value="doctor" */ public class Doctor extends Person { String specialisation; public Doctor(){} public Doctor(String firstName,String lastName,String specialisation) { super(firstName,lastName); this.specialisation=specialisation; } /** *@hibernate.property */ public String getSpecialisation() { return specialisation; } public void setSpecialisation(String specialisation) { this.specialisation=specialisation; } }
This is the mapping generated by Xdoclet:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping> <class name="generalClasses.Person" dynamic-update="false" dynamic-insert="false" discriminator-value="person" >
<id name="id" column="id" type="java.lang.Long" > <generator class="native"> </generator> </id>
<discriminator column="discrimator" type="string" />
<property name="firstName" type="java.lang.String" update="true" insert="true" column="firstName" />
<property name="lastName" type="java.lang.String" update="true" insert="true" column="lastName" />
<!-- To add non XDoclet property mappings, create a file named hibernate-properties-Person.xml containing the additional properties and place it in your merge dir. --> <subclass name="generalClasses.Doctor" dynamic-update="false" dynamic-insert="false" discriminator-value="doctor" > <property name="specialisation" type="java.lang.String" update="true" insert="true" column="specialisation" />
<!-- To add non XDoclet property mappings, create a file named hibernate-properties-Doctor.xml containing the additional properties and place it in your merge dir. -->
</subclass>
</class>
</hibernate-mapping>
Finally I tried to save the Doctor subclass with the following console application:
class TestHib { public static void main(String args[]) throws Exception { Doctor doc = new Doctor("doctor","Evil","Evil things"); SessionFactory sesFac = new Configuration().configure().buildSessionFactory(); Session hibSession= sesFac.openSession(); Transaction transaction =hibSession.beginTransaction(); hibSession.save(doc); transaction.commit(); hibSession.close();
} }
This unfortunately resulted in the following errors:
Exception in thread "main" net.sf.hibernate.MappingException: No persister for: Doctor
at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryImpl.java:344)
at net.sf.hibernate.impl.SessionImpl.getClassPersister(SessionImpl.java:2574)
at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2581)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:725)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:717)
at TestHib.main(TestHib.java:24)
This looks like a very standard problem to me. I must be overlooking something here. Could anybody please help me out.
Thank you
|