How come foreign key of the email table(PersonID) will be set to the primary key of person table since i set the inverse="false"? Base on the documentation, the inverse has to be set to true inorder to do this.
Hibernate version:3
Mapping documents:
CREATE TABLE person ( personid int4, person_name varchar(20) )
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
<class name="source.Person" table="Person"> <id name="personID" column="PersonID" type="int" > <generator class="increment"/> </id>
<property name="personName" column="Person_Name" type="java.lang.String" not-null="false"/> <set name="emails" table="Email" inverse="false" cascade="all-delete-orphan" lazy="true" > <key column="PersonID"/> <one-to-many class="source.Email"/> </set> </class>
</hibernate-mapping>
CREATE TABLE email ( emailid int4, personid int4, email varchar(20) null ) <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <class name="source.Email" table="Email" >
<id name="emailID" column="EmailID" type="int" unsaved-value="0" > <generator class="increment"/> </id> <property name="email" column="Email" type="java.lang.String" not-null="false" /> <many-to-one name="person" class="source.Person" cascade ="save-update" column="PersonID"/> </class> </hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Person person = new Person(); Email email = new Email(); email.setEmail("whatcni3@yahoo.com"); email.setPerson(person); person.getEmails().add(email); session.save(person);
Name and version of the database you are using: postgres 8
The generated SQL (show_sql=true):
Hibernate: insert into Person (PersonID) values (?) Hibernate: insert into Email (PersonID, Email, EmailID) values (?, ?, ?) Hibernate: update Email set PersonID=? where EmailID=?
|