-->
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.  [ 7 posts ] 
Author Message
 Post subject: extra relationship attribute in relastionship table
PostPosted: Fri Apr 22, 2005 2:48 pm 
Newbie

Joined: Fri Apr 22, 2005 2:18 pm
Posts: 4
Hi,
I am new to Hibernate, sorry if I use the terminology wrong. I am trying to use “set” to represent the following diagram. It shows the relationship between JOBS and EMPLOYEES. The relationship is JOB_HISTORY. The problem I am facing is I have one extra relationship attribute (START_DATE) in the JOB_HISTORY table compare to other “set” examples in the Hibernate tutorial.

__________........_______________ ....._____________
|....................|......|...........................|......|..........................|
|..... JOBS......|......|...JOB_HISTORY...|......|..EMPLOYEES.....|
|__________|......|_____________|......|______________|
|...................|......|...........................|......|..........................|
|.*JOB_ID.....|<-->|.*JOB_ID............|......|..........................|
|..JOB_TITLE.|......|.*EMPLOYEE_ID..|<-->| *EMPLOYEE_ID|
|..SALARY......|......|..START_DATE......|......|..FIRST_NAME....|
|__________|......|_____________|......|..LAST_NAME.....|
................................................................|_____________|

I check the Hibernate 3 reference manual charter 8.2 (page #87) and FAQ. It shows an example that can handle this situation. So I create an xml file for Employee like this:
<hibernate-mapping>
<class name="test.Employee" table="EMPLOYEES">
……..
<set name="jobHistory" table="JOB_HISTORY" lazy="true">
<key column="EMPLOYEE_ID"/>
<composite-element class="test.JobHistory">
<property name="start_date" />
<many-to-one name="job_id" class="test.Job"/>
</composite-element>
</set>
…………
For JOB and JOBHistory mapping table, I just simply map their table name to the class property. (Am I doing right in this step?)

Then I use following call in java trying to add an JobHistory entry for a specified employee.
.............
Employee employee = (Employee) session.load(Employee.class, employeeId);
Job theJob = (Job) session.load(Job.class, jobId); // ??where I should use this ???
JobHistory jh = new JobHistory();
jh.setStart_date(new Date());
employee.getJobHistory().add(jh);
...........

As you see in the code, I don’t know where I should use the theJob object and obviously I got an exception from the system complain about I try to put a null value in Job_id:
---------trace log -----------------
Hibernate: select employee0_.EMPLOYEE_ID as EMPLOYEE1_0_, employee0_.FIRST_NAME as FIRST2_3_0_, employee0_.LAST_NAME as LAST3_3_0_ employee0_.JOB_ID as JOB6_3_0_ from EMPLOYEES employee0_ where employee0_.EMPLOYEE_ID=?
Hibernate: select jobhistory0_.EMPLOYEE_ID as EMPLOYEE2___, jobhistory0_.start_date as start7___, jobhistory0_.job_id as job9___ from JOB_HISTORY jobhistory0_ where jobhistory0_.EMPLOYEE_ID=?
Hibernate: insert into JOB_HISTORY (EMPLOYEE_ID, start_date, job_id) values (?, , ?, ?)
…..
java.lang.RuntimeException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
…….
Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("HR"."JOB_HISTORY"."JOB_ID")


I am not sure whether I create the table wrong or I don’t know how to map the composite-element to java class. Please help.

Thanks in advance.
Elwin


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 2:51 pm 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
It looks like you're missing the explicit set (which you'd need) of the Job_id property on the JobHistory class. This should work:
Code:
jh.setJob_id(theJob);


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 5:09 pm 
Newbie

Joined: Fri Apr 22, 2005 2:18 pm
Posts: 4
Hi,
Right now, my JobHistory.hbm.xml table looks like:
Code:
<hibernate-mapping>
………
<class name="test.JobHistory" table="JOB_HISTORY">
   <id name="id" column="ID" type="long">
     <generator class="increment"/>
   </id>
   <property name="employee_id" column="EMPLOYEE_ID"/>
   <property name="start_date" column="START_DATE"/>
   <property name="job_id" column="JOB_ID"/>
</class>


do I need to change the job_id entry to “set” or other mapping?


Thanks again.
Elwin


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 24, 2005 10:54 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
I don't think the same table can be mapped both as a class AND a composite element - your composite element mapping looks fine - did you try what I suggested?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 12:55 pm 
Newbie

Joined: Fri Apr 22, 2005 2:18 pm
Posts: 4
Hi,
I am still trying to figure out what exactly you want me to try. (I am new in Hibernate)
You suggest me to use :
Code:
jh.setJob_id(theJob); //theJob is an instance of Job


But my mapping table for JobHistory is:
Code:
<property name="job_id" column="JOB_ID"/>


So my JobHistory class will have an entry like :
Code:
protected String getJob_id() {
     return job_id;
}
protected void setJob_id(String job_id) {
     this.job_id = job_id;
}

It can only take String object. So what should I change in here?

Thanks again.
Elwin


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 1:18 pm 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
Let me clarify.

In your Employee mapping:
Quote:
<set name="jobHistory" table="JOB_HISTORY" lazy="true">
<key column="EMPLOYEE_ID"/>
<composite-element class="test.JobHistory">
<property name="start_date" />
<many-to-one name="job_id" class="test.Job"/>
</composite-element>
</set>


You have declared jobHistory as a class of type testJobHistory. You have specified that the set should consist of composite elements, and be persisted to the table JOB_HISTORY. This is your definition for the JOB_HISTORY table and the JobHistory ("testJobHistory") object. You do not need, and should not have, a separate mapping for a different class named JobHistory.

If you don't understand how sets of composite elements work, please re-read the documentation. It seems as though you are confused between collections composite elements (which are defined and wholly contained within another class) and collections of classes (which have explicit e.g. one-to-many relationships defined to another class).

Given the Employee XML definition you provided -only-, you should be generating a class of name JobHistory (ref. the <composite-element class= attribute). That class should have a setter and getter of type test.Job, then you could use the code I posted.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 4:46 pm 
Newbie

Joined: Fri Apr 22, 2005 2:18 pm
Posts: 4
It is working now.

Thanks
Elwin


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