• 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 3289
296
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!