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.  [ 4 posts ] 
Author Message
 Post subject: Cascade insert pb :
PostPosted: Tue Nov 30, 2004 4:25 pm 
Newbie

Joined: Tue Nov 30, 2004 3:45 pm
Posts: 2
I've some problems with cascade insertion.
Thanks a lot.

Code:
Here is the stackTrace :
net.sf.hibernate.PropertyValueException: not-null property references a null or transient value: persistent.Child.parent
   at net.sf.hibernate.impl.SessionImpl.checkNullability(SessionImpl.java:1277)
   at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:928)
   at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857)


Hibernate version:2.0

Main
Code:
            Transaction tx= session.beginTransaction();
            Parent parent = new Parent();
            parent.setName("parent1");
            parent.setChild(new TreeSet());
            Child child = new Child();
            child.setName("child1");
            parent.addChild(child);
            session.save(parent);
            tx.commit();


Mapping documents:
Parent.hml.xml :
Code:
<hibernate-mapping>
    <class name="com.flashannonces.website.persistent.Parent" table="PARENTS">
        <id name="id" type="java.lang.String" unsaved-value="null">
                <column name="PARENT_ID" not-null="true" sql-type="char(32)"/>
            <generator class="uuid.string"/>
        </id>
        <set name="child" table="CHILDS" lazy="true" inverse="true" cascade="all">
              <key><column name="PARENT_ID" not-null="true"/></key>
              <one-to-many class="com.flashannonces.website.persistent.Child"/>
        </set>
        <property name="name" type="string" update="true" insert="true" access="property" column="NAME" length="64" not-null="true"/>
    </class>
</hibernate-mapping>


Child.hbm.xml :
Code:
<hibernate-mapping>
    <class name="persistent.Child" table="CHILDS">
        <id name="id" type="java.lang.String" unsaved-value="null">
            <column name="CHILD_ID" not-null="true" sql-type="char(32)"/>
            <generator class="uuid.string"/>
        </id>
        <property name="name" type="string" update="true" insert="true" access="property" column="NAME" length="64" not-null="true"/>
        <many-to-one name="parent" class="persistent.Parent" column="PARENT_ID" not-null="true"/>
    </class>
</hibernate-mapping>


Persistent class
Code:
public class Parent {
    private String id;
    private String name;
    private Set child;
    public Set getChild() {return child;}
    public void setChild(Set child) {this.child = child;}
    public void addChild(Child child) {this.child.add(child);}
    public String getId() {return id;}
    public void setId(String id) {this.id = id;}
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
}
public class Child {
    private String id;
    private String name;
    private Parent parent;
    public String getId() {return id;}
    public void setId(String id) {this.id = id;}
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
    public Parent getParent() {return parent;}
    public void setParent(Parent parent) {this.parent = parent;}
}


Table creation scripts
Code:
create table PARENTS (
   PARENT_ID char(32) not null,
   NAME varchar(64) not null,
   primary key (PARENT_ID)
);
create table CHILDS (
   CHILD_ID char(32) not null,
   NAME varchar(64) not null,
   PARENT_ID char(32) not null,
   primary key (CHILD_ID)
);


The PARENT_ID field of CHILDS table is not filled


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 30, 2004 4:30 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
Solution in the documentation (16.2. Bidirectional one-to-many).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 30, 2004 10:26 pm 
Newbie

Joined: Tue Nov 30, 2004 10:07 am
Posts: 3
We have the same problem :(


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 01, 2004 4:07 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
You need to add:

child.setParent(parent);


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