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

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
dkim18Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
Is this a duplicate of http://www.experts-exchange.com/Programming/Languages/Java/Q_22455089.html? Pls wait till somebody posts the solution there....
0
objectsCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dkim18Author 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.. ^^
0
objectsCommented:
> and some experts mentioned that it might be mysql and hibernate issue.

doubt it, looked to be caused by it not specify the column.
0
dkim18Author 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.