• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Onetoone mapping JPA confusion

 
Sanjay Jha
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

 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2531
112
Android Chrome Eclipse IDE
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic