-->
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.  [ 9 posts ] 
Author Message
 Post subject: many-to-one with at least one parent
PostPosted: Mon Feb 09, 2004 4:52 am 
Newbie

Joined: Tue Feb 03, 2004 1:49 pm
Posts: 16
Code:
<?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 schema="test">
   <class name="com.Parent" table="parents">
      <id name="id" type="java.lang.Long" unsaved-value="null">
         <generator class="native"/>
      </id>
      <version name="version" type="java.lang.Long" unsaved-value="null"/>
      <bag name="children" inverse="true" lazy="true" cascade="all-delete-orphan">
         <key>
            <column name="parent_fk"/>
         </key>
         <one-to-many class="com.Child"/>
      </bag>
      <property name="name" type="java.lang.String"/>
   </class>
   <class name="com.Child" table="childs">
      <id name="id" type="java.lang.Long" unsaved-value="null">
         <generator class="native"/>
      </id>
      <version name="version" type="java.lang.Long" unsaved-value="null"/>
      <many-to-one name="parent" not-null="true" cascade="save-update" class="com.Parent">
         <column name="parent_fk"/>
      </many-to-one>
      <property name="name" type="java.lang.String"/>
   </class>   
</hibernate-mapping>


-- my code --

txn = session.beginTransaction();
Child child = new Child();
child.setName("this child should not in db");
//
session.save(child);
session.flush();
txn.commit();

The problem is the child without parent can still be inserted into database. How to do in the mapping file so that -- a child without parent will be rejected?

_________________
mzoom@homealone


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 6:07 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
not-null="true"

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 6:16 am 
Newbie

Joined: Tue Feb 03, 2004 1:49 pm
Posts: 16
emmanuel wrote:
not-null="true"


Where else should I put the not-null="true"?

(I already have one in the mapping file.)

_________________
mzoom@homealone


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 6:58 am 
Newbie

Joined: Tue Feb 03, 2004 1:49 pm
Posts: 16
tested here --

Code:
      <many-to-one name="parent" cascade="save-update" class="com.Parent">
         <column name="parent_fk"/>
      </many-to-one>


It works.

But, not for this composite-id mapping

Code:
<?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 schema="test">
   <class name="com.Composite" table="composites">
      <composite-id name="comp_id" class="com.CompositePK">
         <key-property name="id" column="id" type="java.lang.Long"/>
         <key-property name="name" column="name"   type="java.lang.String"/>
      </composite-id>
      <version name="version" type="java.lang.Long" unsaved-value="null"/>
      <bag name="children" inverse="true" lazy="true" cascade="all-delete-orphan">
         <key>
            <column name="id_fk"/>
            <column name="name_fk"/>
         </key>
         <one-to-many class="com.Composite"/>
      </bag>
      <many-to-one name="parent" cascade="save-update" class="com.Composite">
         <column name="id_fk" not-null="true"/>
         <column name="name_fk" not-null="true"/>
      </many-to-one>
      <property name="description" type="java.lang.String"/>
   </class>
</hibernate-mapping>


result-errors
Code:
[junit] Testcase: testAddComposite(com.CompositeTest):      Caused an ERROR
[junit] not-null property references a null or transient value: com.Composite.parent
[junit] net.sf.hibernate.PropertyValueException: not-null property references a null or transient value: com.Composite.parent
[junit]     at net.sf.hibernate.impl.SessionImpl.checkNullability(SessionImpl.java:1248)
[junit]     at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:902)
[junit]     at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:839)
[junit]     at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:761)
[junit]     at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:720)
[junit]     at com.CompositeTest.testAddComposite(CompositeTest.java:39)

_________________
mzoom@homealone


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 8:35 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
To summarize
Code:
<many-to-one name="parent" cascade="save-update" class="com.Composite">
         <column name="id_fk" not-null="true"/>
         <column name="name_fk" not-null="true"/>
      </many-to-one>

don"t raise 'not-null property references a null or transient value'

but
Code:
<many-to-one name="parent" not-null="true" cascade="save-update" class="com.Parent">
         <column name="parent_fk"/>
      </many-to-one>

Does.

Is that correct ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 9:06 am 
Newbie

Joined: Tue Feb 03, 2004 1:49 pm
Posts: 16
Sorry.

Quote:
<many-to-one name="parent" cascade="save-update" class="com.Composite">
<column name="id_fk" not-null="true"/>
<column name="name_fk" not-null="true"/>
</many-to-one>

The above mapping cause errors. raise 'not-null property references a null or transient value' when it tried to add data into database. I guess, it is due to only one table being used. Haven't tested on two tables with composite-id.

and,

Quote:
<many-to-one name="parent" not-null="true" cascade="save-update" class="com.Parent">
<column name="parent_fk"/>
</many-to-one>

The above code -- don"t raise 'not-null property references a null or transient value', but a child without parent will still get inserted into database.

and,

Code:
<many-to-one name="parent" cascade="save-update" class="com.Parent">
         <column name="parent_fk" not-null="true"/>
      </many-to-one>


The above code works, and a child without parent will NOT get inserted into database.

_________________
mzoom@homealone


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 9:20 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The last one should have raised an exception.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 9:25 am 
Newbie

Joined: Tue Feb 03, 2004 1:49 pm
Posts: 16
u r right.

_________________
mzoom@homealone


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 9:28 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
So 1st and 3rd do the same thing as expected.

the second cannot work 'cause you're mixing <column> usage with <many-to-one column> usage (using some attribute as if you where using the second syntax).

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.