-->
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.  [ 2 posts ] 
Author Message
 Post subject: Inserting data in Intermediate table on the Many-to-Many
PostPosted: Mon Oct 03, 2005 6:53 am 
Newbie

Joined: Wed Sep 28, 2005 10:13 am
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp



Hi i 'm new to the Hibernate and i 'm trying different kind of mappings
When i 'm doing many-to-many mapping, i 'm not able to insert the data in Intermediate table( consist of both the tables primary keys)

I 'm dong many-to-many mapping for Employee and Project.

EMPLOYEE Table consist of EMPID,NAME
PROJECT Table consist of PROID, PROJECT_NAME
Mapping table ( EMP_PROJECT ) consist of EMPID, PROID

my mappinc xmls

Hibernate version:3.0

Mapping documents:

Mapping for Employee
<hibernate-mapping package="mapping/many2many" auto-import="false">
<class name="Employee" table="EMPLOYEE" lazy="true">
<id name="empId" column="EMPID">
<generator class="increment"/>
</id>
<property name="name" column="NAME"/>
<set name="projects" table="EMP_PROJECT" cascade="all" inverse="true">
<key column="EMPID"/>
<many-to-many class="Project" column="PROID">
</many-to-many>
</set>

</class>
</hibernate-mapping>

Mapping for Project
<hibernate-mapping package="mapping/many2many" auto-import="false">
<class name="Project" table="PROJECT" lazy="true">
<id name="proId" column="PROID">
<generator class="increment"/>
</id>
<property name="project_name" column="PRO_NAME"/>
<set name="employee" table="EMP_PROJECT" cascade="all" inverse="true">
<key column="PROID"/>
<many-to-many class="Employee" column="EMPID">
</many-to-many>
</set>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
txn = ses.beginTransaction();
Employee emp = new Employee();
emp.setName("Name1");

Employee emp2 = new Employee();
emp2.setName("Name2");

Project project2 = new Project();
project2.setProject_name("Project");
project2.setItem(emp);
project2.setItem(emp2);

ses.save(project2);
txn.commit();



Name and version of the database you are using:

HSQLDB server 1.7.1

The generated SQL (show_sql=true):
Hibernate: insert into PROJECT (PRO_NAME, PROID) values (?, ?)
Hibernate: insert into EMPLOYEE (NAME, EMPID) values (?, ?)
Hibernate: insert into EMPLOYEE (NAME, EMPID) values (?, ?)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 11:26 pm 
Newbie

Joined: Thu Sep 08, 2005 9:27 am
Posts: 10
Hi!
The only thing I noticed is that you have
Code:
inverse="true"
repeated in both mappings. Inverse tells hibernate to ignore that collection changes, meaning that it will commit changes to the database only if there are changes in the collection without the inverse property set to true.

What you should do, if adding an employee to the project implies that the new employee should have a reference to the project it now belongs to, is to force the "double reference". For example:

Code:
//inside project
void addItem(Employee e){
items.add(e);
e.addProject(this);
}


And do the same thing in the Employee class.

If this doesn't work, maybe you should post your java code to see if there's something strange in it.

Good luck!

_________________
Pablo N. Alvarez
Austral University
Argentina


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.