Hibernate one to many relationship - not-null property references a null or transient value error

dkim18
dkim18 used Ask the Experts™
on
Hi,

This is one to many relastionship in hibernate and I think I figured out correctly, but I don't understand why I am having this error.
+++++++++
[2007-03-17 16:10:53,450] DEBUG org.hibernate.type.IntegerType binding '0' to parameter: 1
Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value: edu.jhu.jhu786.hib.Address.person
      at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
      at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:265)
      at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
      at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:114)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:98)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
      at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:502)
      at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:494)
      at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:490)
      at edu.jhu.jhu786.hib.AddressDAOImpl.insert(AddressDAOImpl.java:32)
      at edu.jhu.jhu786.hib.TestDriver.insertAddress(TestDriver.java:138)
      at edu.jhu.jhu786.hib.TestDriver.main(TestDriver.java:69)

...
+++++++++++++++++
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="edu.jhu.jhu786.hib.Person" table="person" catalog="jhu786_1">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <property name="versionNum" type="java.lang.Integer">
            <column name="version_num" not-null="true" />
        </property>
        <property name="familyName" type="java.lang.String">
            <column name="family_name" length="20" />
        </property>
        <property name="givenName" type="java.lang.String">
            <column name="given_name" length="20" />
        </property>
        <property name="relocate" type="java.lang.String">
            <column name="relocate" length="20" />
        </property>
        <set name="addresses" inverse="true">
            <key>
                <column name="person_id" not-null="true" />
            </key>
            <one-to-many class="edu.jhu.jhu786.hib.Address" />
        </set>
    </class>
</hibernate-mapping>
++++++++++++++
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="edu.jhu.jhu786.hib.Address" table="address" catalog="jhu786_1">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <many-to-one name="person" class="edu.jhu.jhu786.hib.Person" fetch="select">
            <column name="person_id" not-null="true" />
        </many-to-one>
        <property name="versionNum" type="java.lang.Integer">
            <column name="version_num" not-null="true" />
        </property>
        <property name="street1" type="java.lang.String">
            <column name="street1" length="50" />
        </property>
        <property name="street2" type="java.lang.String">
            <column name="street2" length="50" />
        </property>
        <property name="city" type="java.lang.String">
            <column name="city" length="50" />
        </property>
        <property name="state" type="java.lang.String">
            <column name="state" length="2" />
        </property>
        <property name="zip" type="java.lang.String">
            <column name="zip" length="10" />
        </property>
        <property name="latitude" type="java.lang.Float">
            <column name="latitude" precision="4" />
        </property>
        <property name="longitude" type="java.lang.Float">
            <column name="longitude" precision="4" />
        </property>
    </class>
</hibernate-mapping>
+++++++++++++++


 Person person = new Person();
        Set addresses = new HashSet();
       
        Address address1 = new Address( 1, "1234 Main St", "APT 111", "Baltimore", "MD", "22222"/*, person*/);
        address1.setPerson(person);
       
        addresses.add(address1);
        //person.setAddresses(addresses);
        person = new Person(1, "Smith", "John",  null /*relocate*/ , addresses);
       
        person.setAddresses(addresses);

        testDriver.insertPerson(person, personDAO);
        testDriver.insertAddress(address1, addressDAO);



++++++++++++++++++

public class Address  extends BaseModel implements java.io.Serializable {


    // Fields    

    // private Integer id;
     private Person person;
    // private Integer versionNum;
     private String street1;
     private String street2;
     private String city;
     private String state;
     private String zip;
     private Float latitude;
     private Float longitude;


    // Constructors

    /** default constructor */
    public Address() {
    }

      /** minimal constructor */
    public Address(Integer id, Person person, Integer versionNum) {
        this.id = id;
        this.person = person;
        this.versionNum = versionNum;
    }
   
    /** full constructor */
    public Address(Integer versionNum,  String street1, String street2, String city, String state, String zip/*, Person person*/) {
        this.id = id;
        this.person = person;
        this.versionNum = versionNum;
        this.street1 = street1;
        this.street2 = street2;
        this.city = city;
        this.state = state;
        this.zip = zip;
        this.latitude = latitude;
        this.longitude = longitude;
    }

   
...

+++++++++++++++


public class Person  extends BaseModel implements java.io.Serializable {


    // Fields    

    // private Integer id;
    // private Integer versionNum;
     private String familyName;
     private String givenName;
     private String relocate;
     private Set addresses = new HashSet(0);


    // Constructors

    /** default constructor */
    public Person() {
    }

      /** minimal constructor */
    public Person(Integer id, Integer versionNum) {
        this.id = id;
        this.versionNum = versionNum;
    }
   
    /** full constructor */
    public Person( Integer versionNum, String familyName, String givenName, String relocate, Set addresses) {
        this.id = id;
        this.versionNum = versionNum;
        this.familyName = familyName;
        this.givenName = givenName;
        this.relocate = relocate;
        this.addresses = addresses;
    }

   
    // Property accessors

    public Integer getId() {
        return this.id;
    }
   
    public void setId(Integer id) {
        this.id = id;
    }


...
+++++++++++++++++++++

Thanks in advance
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mayank SPrincipal Technologist

Commented:
Is this a duplicate of http://www.experts-exchange.com/Programming/Languages/Java/Q_22455089.html? Pls wait till somebody posts the solution there....
Java Developer
Top Expert 2010
Commented:
try first saving the the person you are assigning to address1.
you also need to set the id etc for that person.

alternnatively instead assign the 2nd person you create (and save) to address1.
Not sure what you're intent is.

Author

Commented:
mayankeagle,

I know this is similiar question. The other question is about many to many to relastionship and some experts mentioned that it might be mysql and hibernate issue. That was why I tried one to many relastionship and I still have problem. I searched all the forums and web sites, but couldn't find solution. (I always do that before posting here.) Since I had different error messag for two different cases, I want to know why I did wrong for both cases. I also seperated question since the other one went too long and if I post this one as well, it might confuse experts.. ^^
Mick BarryJava Developer
Top Expert 2010
Commented:
> and some experts mentioned that it might be mysql and hibernate issue.

doubt it, looked to be caused by it not specify the column.

Author

Commented:
I found the problem. Somehow, these lines(generated by MyEclipse hibernate tool) were giving me problem.

  <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="assigned" />
        </id>

I changed "assigned" to increment and it worked.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial