-->
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.  [ 7 posts ] 
Author Message
 Post subject: one-to-many associations with assigned PK
PostPosted: Mon Dec 01, 2003 2:19 pm 
Newbie

Joined: Mon Dec 01, 2003 2:03 pm
Posts: 6
I am trying to implement a one-to-many association with a assigned String identifier. I have a simple parent child relationship like :

/*** Parent.hbm.xml***/
Code:
<hibernate-mapping>
<class name="org.opcapl.profession.model.Parent" table="parent">
<id column="parent_id" name="id" type="java.lang.String">
<generator class="assigned"/>
</id>

<property column="name" length="255" name="name" type="java.lang.String"/>

<set name="children" inverse="true" cascade="all" lazy="true" >
     <key column="parent_id"/>
     <one-to-many class="org.opcapl.profession.model.Child"/>
</set>

</class>
</hibernate-mapping>


/*** Child.hbm.xml***/
Code:
<hibernate-mapping>
<class name="org.opcapl.profession.model.Child" table="child">
<id column="id" name="id" type="java.lang.Long">
<generator class="identity"/>
</id>
<property column="name" length="255" name="name" type="java.lang.String"/>

<many-to-one name="parent" class="org.opcapl.profession.model.Parent" column="parent_id" not-null="true"/>
</class>
</hibernate-mapping>


This is exactly the implementation from the hibernate doc except that I have changed the primary key type of the parent table to assigned.
this is not working.
I am trying to do like this :

Code:
            Parent parent = new Parent();
            parent.setName("parent");
            parent.setId("XXXXX");
            Child child = new Child();
            child.setName("child");
            parent.setChildren(new HashSet());
            parent.addChild(child);
            session.save(parent);
            session.flush();
            session.connection().commit();
            session.close();


I got the following exception :

Code:
Hibernate: insert into child (name, parent_id) values (?, ?)
19:17:48,000  WARN JDBCExceptionReporter:38 - SQL Error: 515, SQLState: 23000
19:17:48,000 ERROR JDBCExceptionReporter:46 - Could not insert NULL value into 'parent_id' column, table 'Dev.dbo.child'. this column does not accpet NULL value. INSERT failed.
19:17:48,000 ERROR JDBCExceptionReporter:37 - Could not insert


this code is working perfectly with an 'indentity' sequence identifier, why it is not working with assigned?

thanks for your help.
S.Shehzad


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 2:54 pm 
Regular
Regular

Joined: Thu Nov 20, 2003 10:44 am
Posts: 58
Location: Paris, France
You should put a parent attribute in your child class


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 3:12 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
child.setParent(parent);

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 7:44 pm 
Newbie

Joined: Mon Dec 01, 2003 2:03 pm
Posts: 6
Hi,

Actually in the method addChild of parent I am setting the parent property to the child;

Code:
parent.addChild(child c) {
  c.setParent(this)
  this.children.add(c);

}


which is the same : child.setParent(parent);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 7:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Try not to mix and match "identity" key generation with other id generation strategies. Or, if you must, you might need to handle things a bit more "manually" by turning off cascade and calling flush() at the right time, etc.

Classes with "identity" id generation are inserted immediately, instead of using write-behind.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 6:03 am 
Newbie

Joined: Mon Dec 01, 2003 2:03 pm
Posts: 6
Hi,
I have turned off the cascade..
the parent is saved but not the child.

thanks for your help.

S.Shehzad


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 6:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
obviously, if you turn off cascade, you need to save them both manually....


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