Hibernate version:
3.0 beta4b
Name and version of the database you are using:
MySQL 4.1.10
Imagine the following situation:
Classes
Code:
public abstract class User {
...
}
public class Student extends User {
...
}
public class Lecturer extends User {
private Course course;
...
}
public class Course {
private Set students = new HashSet();
...
}
Hibernate mappingsCode:
<hibernate-mapping>
<class name="persistence.User" table="users" abstract="true">
<id name="id" column="id" type="long" unsaved-value="null">
<generator class="native"/>
</id>
<discriminator column="discriminator" type="string"/>
<subclass name="persistence.Student" discriminator-value="student">
<many-to-one name="course" class="persistence.Course" column="course_id" not-null="true"/>
</subclass>
<subclass name="persistence.Lecturer" discriminator-value="lecturer">
</subclass>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="persistence.Course" table="courses">
<id name="id" column="id" type="long" unsaved-value="null">
<generator class="native"/>
</id>
<set name="students" cascade="all" inverse="true">
<key column="course_id"/>
<one-to-many class="persistence.Student"/>
</set>
</class>
</hibernate-mapping>
This won't work because of the constraint
not-null="true" in the many-to-one association (lecturers and students are in
one table).
Well, it will work, but you won't be able to insert lecturers which should not have any association to a specific course.
So, you have to change the
not-null="true" to
not-null="false".
Now, I wonder, if this behaviour is intended?
IMHO, for a student the
not-null="true" seems to be 100% correct.
Any feedback is appreciated!
Best regards
Sven