-->
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.  [ 1 post ] 
Author Message
 Post subject: PropertyValueException with one-to-many relationship
PostPosted: Tue Jul 06, 2004 11:23 pm 
Newbie

Joined: Tue Jul 06, 2004 10:57 pm
Posts: 4
I am getting the following exception when trying to create a one-to-many Parent/Child relationship based on the Parent/Child example in the Hibernate Reference Manual:

Code:
net.sf.hibernate.PropertyValueException: not-null property references a null or transient value: mdl.Child.parent


I am using XDoclet to create the mapping files (listed below), but they look the same as the one in the Hibernate Reference Manual.

What is wrong mith my mapping files? You can see from the Java code that I am explicitly setting the
Code:
parent
field of the Child to the actual reference to its Parent. Why does theexception say not-null property references a null or transient value?

Thank you for your help.

Warmest regards, Matt


Hibernate version: 2.1.3


Mapping documents: There are two, Parent.hbm.xml and Child.hbm.xml

Parent.hbm.xml
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>
    <class
        name="mdl.Parent"
        table="PARENT"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="PARENT_PK"
            type="java.lang.Long"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
            column="PARENTNAME"
            length="20"
            not-null="true"
        />

        <set
            name="children"
            lazy="true"
            inverse="true"
            cascade="all-delete-orphan"
            sort="unsorted"
        >

              <key
                  column="PARENT_FK"
              >
              </key>

              <one-to-many
                  class="mdl.Child"
              />
        </set>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Parent.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


Child.hbm.xml
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>
    <class
        name="mdl.Child"
        table="CHILD"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="CHILD_PK"
            type="java.lang.Long"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
            column="CHILDNAME"
            length="20"
            not-null="true"
        />

        <many-to-one
            name="parent"
            class="mdl.Parent"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            access="property"
            column="PARENT_FK"
            not-null="true"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Child.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>



the Java code between sessionFactory.openSession() and session.close()
Code:
        Parent parent = new Parent("Kerry");

        Child child = new Child("Guzzi");
        parent.addChild(child);

        pm.save(parent);
        pm.endSession();

        // Keep id but lose reference to object.
        Long id = parent.getId();
        parent = null;

        // Look it up again.
        Parent new_parent = (Parent) pm.getById(Parent.class, id);
        assertEquals("Kerry", new_parent.getName());



the full stack trace of any exception that occurs
Code:
net.sf.hibernate.PropertyValueException: not-null property references a null or transient value: mdl.Child.parent
   at net.sf.hibernate.impl.SessionImpl.checkNullability(SessionImpl.java:1269)
   at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:921)
   at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:850)
   at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:768)
   at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:731)
   at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1380)
   at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
   at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
   at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
   at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
   at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
   at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)
   at net.sf.hibernate.impl.SessionImpl.preFlushEntities(SessionImpl.java:2664)
   at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2239)
   at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2228)
   at persistence.hibernate.DefaultHibernateSessionFactory.endSession(Unknown Source)
   at persistence.hibernate.HibernatePersistenceManager.endSession(Unknown Source)
   at tests.persistence.hibernate.TestHibernatePersistenceManager.tearDown(Unknown Source)
   at junit.framework.TestCase.runBare(TestCase.java:130)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)



the name and version of the database
Code:
hsqldb-1.7.2.RC-6b.jar



a debug level Hibernate log
Code:
18:59:51,501  INFO DriverManagerConnectionProvider:143 - cleaning up connection pool: jdbc:hsqldb:.
18:59:51,541  INFO Configuration:872 - configuring from resource: /hibernate.cfg.xml
18:59:51,551  INFO Configuration:844 - Configuration resource: /hibernate.cfg.xml
18:59:51,571  INFO Configuration:328 - Mapping resource: mdl/Parent.hbm.xml
18:59:51,611  INFO Binder:229 - Mapping class: mdl.Parent -> PARENT
18:59:51,631  INFO Configuration:328 - Mapping resource: mdl/Child.hbm.xml
18:59:51,681  INFO Binder:229 - Mapping class: mdl.Child -> CHILD
18:59:51,691  INFO Configuration:1030 - Configured SessionFactory: null
18:59:51,691  INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.HSQLDialect
18:59:51,701  INFO DriverManagerConnectionProvider:42 - Using Hibernate built-in connection pool (not for production use!)
18:59:51,701  INFO DriverManagerConnectionProvider:43 - Hibernate connection pool size: 1
18:59:51,701  INFO DriverManagerConnectionProvider:77 - using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:.
18:59:51,711  INFO DriverManagerConnectionProvider:78 - connection properties: {user=sa}
18:59:51,711  INFO SchemaUpdate:102 - Running hbm2ddl schema update
18:59:51,711  INFO SchemaUpdate:110 - fetching database metadata
18:59:51,711  INFO SchemaUpdate:124 - updating schema
18:59:51,711  INFO Configuration:613 - processing one-to-many association mappings
18:59:51,711  INFO Binder:1168 - Mapping collection: mdl.Parent.children -> CHILD
18:59:51,711  INFO Configuration:622 - processing one-to-one association property references
18:59:51,721  INFO Configuration:647 - processing foreign key constraints
18:59:51,811 DEBUG SchemaUpdate:133 - create table PARENT (PARENT_PK BIGINT NOT NULL IDENTITY, PARENTNAME VARCHAR(20) not null)
18:59:51,821 ERROR SchemaUpdate:138 - Unsuccessful: create table PARENT (PARENT_PK BIGINT NOT NULL IDENTITY, PARENTNAME VARCHAR(20) not null)
18:59:51,821 ERROR SchemaUpdate:139 - Table already exists: PARENT in statement [create table PARENT]
18:59:51,821 DEBUG SchemaUpdate:133 - create table CHILD (CHILD_PK BIGINT NOT NULL IDENTITY, CHILDNAME VARCHAR(20) not null, PARENT_FK BIGINT not null)
18:59:51,821 ERROR SchemaUpdate:138 - Unsuccessful: create table CHILD (CHILD_PK BIGINT NOT NULL IDENTITY, CHILDNAME VARCHAR(20) not null, PARENT_FK BIGINT not null)
18:59:51,821 ERROR SchemaUpdate:139 - Table already exists: CHILD in statement [create table CHILD]
18:59:51,821 DEBUG SchemaUpdate:133 - alter table CHILD add constraint FK3D1FCFC217F387A foreign key (PARENT_FK) references PARENT
18:59:51,871 ERROR SchemaUpdate:138 - Unsuccessful: alter table CHILD add constraint FK3D1FCFC217F387A foreign key (PARENT_FK) references PARENT
18:59:51,881 ERROR SchemaUpdate:139 - Constraint already exists: FK3D1FCFC217F387A in statement [alter table CHILD add constraint FK3D1FCFC217F387A foreign key (PARENT_FK) references PARENT]
18:59:51,881  INFO SchemaUpdate:143 - schema update complete
18:59:51,881  INFO DriverManagerConnectionProvider:143 - cleaning up connection pool: jdbc:hsqldb:.
18:59:51,881  INFO Configuration:613 - processing one-to-many association mappings
18:59:51,881  INFO Configuration:622 - processing one-to-one association property references
18:59:51,881  INFO Configuration:647 - processing foreign key constraints
18:59:51,881  INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.HSQLDialect
18:59:51,881  INFO SettingsFactory:62 - Use outer join fetching: false
18:59:51,881  INFO DriverManagerConnectionProvider:42 - Using Hibernate built-in connection pool (not for production use!)
18:59:51,891  INFO DriverManagerConnectionProvider:43 - Hibernate connection pool size: 1
18:59:51,891  INFO DriverManagerConnectionProvider:77 - using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:.
18:59:51,891  INFO DriverManagerConnectionProvider:78 - connection properties: {user=sa}
18:59:51,891  INFO DriverManagerConnectionProvider:143 - cleaning up connection pool: jdbc:hsqldb:.
18:59:51,891  INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommend
[/u]


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

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.