I will try to keep this short, because I know time is important, but I am pretty stuck. I have recently implemented Hibernate into a project I am working on. I wrote my hbm files by hand and used hbm2java to create my classes. This was before I found out that hbm2java is not really supported anymore (at least not maintained). Both books I have been using
"Hibernate A Developer's Handbook" and
"Hibernate A J2EE Developer's Guide" discussing starting with the hbm files. Well never the less. My problem is that I have one table called Jobs and another table called Skills. Now any job can have a multitude of skills. So I have a many-to-many relationship. Well what's happening is that the job is getting inserted into the hsqldb, but the join table is not getting updated. Below is my hbm.xml files:
------- Job.hbm.xml ---------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="com.echostorm.resumes.evaluator.hibernate.Job" table="JOB">
<meta attribute="class-description">
Represents a Job with which multiple skills can be assigned
@author JVince Cordaro (with help from Hibernate)
</meta>
<id name="id" type="string" column="JOB_ID">
<meta attribute="finder-method">findByID</meta>
<generator class="uuid.hex" />
</id>
<version column="revision" name="revision" />
<property name="title" type="string"
column="TITLE" length="100">
<meta attribute="finder-method">findByTitle</meta>
</property>
<property name="department" type="string"
column="DEPARTMENT" length="100">
<meta attribute="finder-method">findByDepartment</meta>
</property>
<property name="description" type="string"
column="DESCRIPTION" length="500" />
<property name="date" type="string"
column="date" length="100" >
<meta attribute="finder-method">findByDate</meta>
</property>
<property name="interviewerID" type="string"
column="interviewerID" length="50" >
<meta attribute="finder-method">findByInterviewerID</meta>
</property>
<property name="clearance" type="string"
column="clearance" length="100" >
<meta attribute="finder-method">findByClearance</meta>
</property>
<property name="location" type="string"
column="location" length="100">
<meta attribute="finder-method">findByLocation</meta>
</property>
<property name="statusID" type="string"
column="statusID" length="100">
<meta attribute="finder-method">findByStatusID</meta>
</property>
<set name="skills" table="JOB_SKILLS" inverse="true">
<meta attribute="field-description">Skills for Job</meta>
<key column="JOB_ID"/>
<many-to-many class="com.echostorm.resumes.evaluator.hibernate.Skill" column="SKILL_ID"/>
</set>
</class>
</hibernate-mapping>
-------------------------------------------------------------------
----- Skill.hbm.xml ---------------------------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="com.echostorm.resumes.evaluator.hibernate.Skill" table="skill">
<id name="id" type="string" column="SKILL_ID">
<meta attribute="finder-method">findByID</meta>
<generator class="uuid.hex" />
</id>
<version column="revision" name="revision" />
<property name="name" type="string"
column="name" length="100" >
<meta attribute="finder-method">findByName</meta>
</property>
<property name="description" type="string"
column="description" length="500" />
<property name="department" type="string"
column="department" length="100" >
<meta attribute="finder-method">findByDepartment</meta>
</property>
<set name="jobs" table="JOB_SKILLS" inverse="true">
<meta attribute="field-description">Jobs that use this skill</meta>
<key column="SKILL_ID"/>
<many-to-many class="com.echostorm.resumes.evaluator.hibernate.Job" column="JOB_ID"/>
</set>
</class>
</hibernate-mapping>
----------------------------------------------------------------------------------
As you can see I have my set tag setup. At least that's how the books have been showing it. It did build the db, with a skill table, job table, and JOB_SKILLS table. However the JOB_SKILLS table never get's updated like I mentioned before.
Here is my code for inputing a new job:
----
try{
Transaction trans = hibernateSession.beginTransaction();
myJob.setTitle(ar.getParameter("title"));
myJob.setDescription(ar.getParameter("description"));
myJob.setDepartment(ar.getParameter("department"));
myJob.setClearance(ar.getParameter("clearance"));
myJob.setSkills(new HashSet());
long i = System.currentTimeMillis();
String t = Long.toString(i);
myJob.setDate(t);
myJob.setStatusID(StatusHandler.getIDByName(hibernateSession,"open"));
// Get skill id's from list box in portal page
String[] skills = ar.getParameterValues("skills");
for(int k=0; k<skills.length; k++)
{
Skill tempSkill = SkillHandler.getSkillByID(hibernateSession,skills[k]);
logger.debug("Setting skill for job : " +tempSkill.getName());
myJob.getSkills().add(tempSkill);
}
hibernateSession.save(myJob);
trans.commit();
}
----------------------
I have put in logging comments to make sure that the skills are getting added. And they seem to be because the logger shows skills being added. However when the save gets called the Job gets added but the JOB_SKILLS table never get's touched. I have show sql turned on for hibernate and I do not see any insert into JOB_SKILLS after I see the insert into JOB. I have also used the HSQL Database Manager to see if the table is still empty and it is.
The SkillHandler is just a class the extends the SkillFinder that hibernate made. My SkillHandler has methods dealing with skills of course. The getSkillByID takes in the session along with the id string to return a skilll.
Does anyone have any idea what's wrong, or can someone give me or point me to an example?
Thanks in advance,
Vince Cordaro
|