Hi fellows. I'm facing a problem with collections.
I got two collections, addresses of
PersonAddress, and contactMeans of
PersonContactMean, in a class called
Person.
First, I had only contactMeans in Person, and when I tried to saveOrUpdate a new instance of Person that had one or more contactMeans everything was ok.
But after I created addresses field, same way I did with contactMeans, I started to get "ERROR: null value in column person_id violates not-null constraint" when I try to save it.
And it's true: I didn't assign person's id to a new PersonAddress I created. But I didn't do it to PersonContactMean too, and this last was working fine.
What am I doing wrong here?
I'm using hibernate 2.1.3 with PostgreSQL 7.4.1. Here's my mappings:
Code:
<hibernate-mapping>
<class name="eg.beans.Person" proxy="eg.beans.Person" discriminator-value="person" table="person">
.
.
.
my properties
.
.
.
<set name="contactMeans" cascade="all" lazy="true" table="contact_mean">
<key column="person_id"/>
<one-to-many class="eg.beans.PersonContactMean"/>
</set>
<set name="addresses" cascade="all" lazy="true" table="person_address">
<key column="person_id"/>
<one-to-many class="eg.beans.PersonAddress"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="eg.beans.PersonAddress" table="person_address">
.
.
.
my properties
.
.
.
<many-to-one name="person" class="eg.beans.Person" column="person_id" insert="false" update="false"/>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="eg.beans.PersonContactMean" table="person_contact_mean">
.
.
.
my properties
.
.
.
<many-to-one name="person" class="eg.beans.Person" column="person_id" insert="false" update="false"/>
</class>
</hibernate-mapping>
And here's my code:
Code:
if (person.getAddresses().isEmpty()) {
pa = new PersonAddress();
pa.setPersonId(id);
pa.setAddressTypeId(new Integer(1));
pa.setPerson(person);
person.getAddresses().add(pa);
} else {
pa = (PersonAddress)person.getAddresses().iterator().next();
}
.
.
.
fill PersonAddress with form's values
session.saveOrUpdate(person);
JDBCException occurs
.
.
.
Thanx in advance for any help.