Solved

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

Posted on 2007-03-17
5
2,038 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:mayankeagle
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now