Solved

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

Posted on 2007-03-17
5
2,071 Views
Last Modified: 2008-03-06
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
0
Comment
Question by:dkim18
  • 2
  • 2
5 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 18741712
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
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 18741766
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
 

Author Comment

by:dkim18
ID: 18742479
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
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 18742573
> 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
 

Author Comment

by:dkim18
ID: 18823948
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 102
hibernate example using maven 12 82
IBM TS2900 (3572) Tape Autoloader Java? 12 61
Tomcat 9 + java 8 error while trying to deploy a war file 2 731
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question