aspose file tools
The moose likes Object Relational Mapping and the fly likes Secondary table Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "Secondary table" Watch "Secondary table" New topic
Author

Secondary table

rani vini
Greenhorn

Joined: May 13, 2009
Posts: 24
How can map these two tables using secondary table
Table EMP
ID NUMBER primarykey, EMPNAME VARCHAR2(15)
Table DEPT
EMPID NUMBER , DEPTNAME VARCHAR2(15)

Where EMPID refers to the ID column of EMP

My mapping calss is like below
@Entity
@Table(name="emp")
@SecondaryTable(name="dept")
@PrimaryKeyJoinColumn( name = "EMPID", referencedColumnName = "ID")
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="EMP")
@SequenceGenerator(name="EMP", sequenceName="SEQ_EMP")
long Id;


@Column(name="EMPNAME")
String empName;

@Column(table="dept")
String deptName;

When I try to persist like below

Employee empObj = new Employee();
empObj.setEmpName("emp1");
empObj.setDeptName("dept1");
em.persist(empObj);

It is displayign the Sql statements like
Hibernate: insert into emp (EMPNAME, Id) values (?, ?)
Hibernate: insert into dept (deptName, Id) values (?, ?)
In DEPT table there is no "ID" column. So Iam getting error.
The insert Statement for DEPT should contain "DEPTNAME and EMPID" instead of "DEPTNAME AND ID"

Can anyone help?
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 482
Hi rani vini..

May i know your goal using Secondary Table ?..
Thanks in advance...


Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4967

Could it be the referencedColumnName = "ID" element?

-Cameron McKenzie


Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Secondary table
 
Similar Threads
Strange Behavior of Hibernate - NOT RESPONDING
Problem with all-delete-orphan
hybernate doubt
Performance concern with Select Query
INDEX PROPERTY <html:form> with nested beans