-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: inverse="false" still work
PostPosted: Mon Jun 20, 2005 4:34 am 
Beginner
Beginner

Joined: Fri Jun 17, 2005 7:24 am
Posts: 23
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=?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 20, 2005 7:10 am 
Newbie

Joined: Mon Jun 20, 2005 6:20 am
Posts: 7
You ask "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"?

The reason that the foreign key of the email table (PersonId) will be set to the primary key of the person table i the following part of you mapping document:

<many-to-one name="person" class="source.Person" cascade ="save-update" column="PersonID"/>

The reference will allways be established in the database no matter what value the inverse has.

Setting inverse="false" only says that the association between a person and his email shouldn't be bidirectional. Set inverse = "false" means that you can't say person.getEmails();


Top
 Profile  
 
 Post subject: still can use person.getEmails
PostPosted: Mon Jun 20, 2005 9:32 pm 
Beginner
Beginner

Joined: Fri Jun 17, 2005 7:24 am
Posts: 23
Ii still can use person.getEmails. How?

Query q = session.createQuery("from Person d where d.personID=1");
List list = q.list();
Iterator iter = list.iterator();
Email email = new Email();

while(iter.hasNext()){

Person person = (Person)iter.next();
System.out.println("name="+person.getPersonName());
email = (Email) person.getEmails().iterator().next();
System.out.println("email="+email.getEmail());
}

output in console:
Hibernate: select person0_.PersonID as PersonID, person0_.Person_Name as Person2_2_ from Person person0_ where person0_.PersonID=1
name=andy lau
Hibernate: select emails0_.PersonID as PersonID__, emails0_.EmailID as EmailID__, emails0_.EmailID as EmailID0_, emails0_.Email as Email3_0_, emails0_.PersonID as PersonID3_0_ from Email emails0_ where emails0_.PersonID=?
email=whatcni3@yahoo.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 21, 2005 4:05 am 
Newbie

Joined: Mon Jun 20, 2005 6:20 am
Posts: 7
Hi why,

If you want to be able to say person.getEmails() you have to set inverse="true".


Top
 Profile  
 
 Post subject: No
PostPosted: Wed Jun 22, 2005 2:07 am 
Beginner
Beginner

Joined: Fri Jun 17, 2005 7:24 am
Posts: 23
No. I have tested. It work.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.