• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to do JPA OneToOne relationship insert only one side of the relation

 
Greenhorn
Posts: 2
Google Web Toolkit Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi folks.

In this moment I'm having a problem with my JPA implementation. I have two Entities as following:

Entity Position



Entity Employee



In the other hand I have this Session Bean:

Remote Interface


Remote Interface Implementation



The previous code includes the class Sequence. By the moment it only generates a secuential value for using as primary key for the entity Employee.

This is the content of the file persistence.xml



This is my test class:



And finally, this is the database script:


Before I executed test class EmployeeTest, I inserted one row in the table tbl_positions like this:


Because tbl_positions is a catalog and it most not be created when I create an Employee.

My problem arise when I executed the test class EmployeeTest. When I invoke the method service.createEmployee(employee), I get the exception: Duplicate entry '1' for key 'PRIMARY' referencing the table tbl_positions. This is a problem for me because what I need is that if a Position already exists, it only creates the Employee. Even better, for business constraints when I create an Employee, all the Positions must exist.

Thanks for any help about it !!

Best tegards,
RADE
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your problem is because of the cascade type:

So when you attempt to create an Employee with position 1, it cascades this and attempts to create a Position with ID 1 which breaks the primary key constraint as there is already a row with this ID.
Try this instead:
 
Raul De Villa
Greenhorn
Posts: 2
Google Web Toolkit Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Excellent James !!... this solves my problem !

Thank you very much !
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you assigning some Ids manually?
Better let the persistence provider take care of persistence Ids.
 
reply
    Bookmark Topic Watch Topic
  • New Topic