Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Onetoone mapping JPA confusion

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everybody,

I am struggling to understand following code snippet unidirectional mapping..

@Entity
public class Employee {

@Id
private int employeeId;
@OneToOne
@JoinColumn(name="dept_id")
private Department dept;

}
In this case I understood deptid foreign key value is unique in employee table and wont be shared across rows. but I am able to persist 2 employee instance with same dept instance as follows : -

where e and e1 are employee class reference.

e.setDept(d);
e1.setDept(d);
em.persist(d);
em.persist(e);
em.persist(e1);

this code works absolutely fine and 2 records are getting inserted into employee table with deptid as 1. Could anyone explain please or if my understanding itself is wrong . I am using hibernate implementation of jpa and database in MySQL.Thanks in advance.

Kind Regards,
Sanjay

 
Creator of Enthuware JWS+ V6
Posts: 3341
303
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is a unique constraint missing in the JPA implementation. It was also noticed and discussed here.
 
Sanjay Jha
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Frits,

Thanks a lot for your clarification.. Happy with your answer .

Regards,
Sanjay
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic