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 (?, ?)