Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Secondary table

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi rani vini..

May i know your goal using Secondary Table ?..
Thanks in advance...
 
author and cow tipper
Posts: 5000
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could it be the referencedColumnName = "ID" element?

-Cameron McKenzie
 
Not looking good. I think this might be the end. Wait! Is that a tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic