• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

JPA Persistance with mapping

 
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to JPA and need some help understanding persisting JPA parent entity along with it's child (through Mapping).
Say I have following two entities



Now I am trying to save Employee setting Address Entity to it.But it throws a foreign key validation exception because e_id is auto_increment and is generated by database while save.
Does JPA provide some mechanism to handle this ?

 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not be defining the emp_id property in the address entity. With JPA you are supposed to defining the relationships using objects not manually defining the database key links.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since your Employee entity doesn't have a compound primary key, you don't need to set all those parameters on Address just to target your employee. Try something like this:



Some highlights you should consider when refactoring your code:

* insertable and updatable , when setted to false, are meant to inform the persistence provider that whenever an insert and/or update operation are fired on Address entity it should not include the mapping on the generated query.
* referencedColumnName is used to specify a column name which the foreign key targets. When not present, the default behavior is target's primary key.

Just remember, the Entity who holds the "mappedBy" is the target(Address), the other one the source Entity(Employee).
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!