-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Not-Null constraint problems - Is this intended?
PostPosted: Thu Feb 17, 2005 7:12 pm 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
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 mappings
Code:
<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


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 19, 2005 7:56 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
not-null refers to the DDL schema. It's not possible to set not-null properties in <subclass> (at least most of the time).

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.