Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Onetoone mapping JPA confusion  RSS feed

 
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: 3273
296
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
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!