This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

ERROR OneTOMany/ManyToOne mapping: FK null at time of saving parent - child table

 
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I have two entities:

I understand the problem. The FK (name: LOGGINGEMPLOYEEMODEL_ID ) is in the tabel "loggingmodel".
At the moment of new record saving for the parent table 'employeedetails' the child table should also be updated.  But I get the error: "org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "LOGGINGEMPLOYEEMODEL_ID"; SQL statement:
insert into LoggingModel (infotext, loggingEmployeeModel_id, title, id) values (?, ?, ?, ?) [23502-199]"

I understand it, the new id for the employeedetails is unknown for the FK for child table. (If it take the part "employeeModelLogging" out of the Postman injection, than I don't get the constraint error, so it should the loggingModel part)

How do I solve this within JPA - Hibernate?

The parent side: One-To-Many:

The Child side: Many-To-One:


The create employee service implementation:

And this is what I inject with Postman:

Can someone can give an idea/hint what I'm doing wrong?
 
Sheriff
Posts: 22511
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I take it that the model classes are mapped from JSON directly to your entities, right? If so, you're running into a bidirectional relation that is only set on one side. The unmarshalling from JSON creates new LoggingModel instances, and puts them into a list. It then sets that list on the created EmployeeModel instance. The EmployeeModel instance now has a reference to all of its LoggingModel instances. What is still missing is the reference in the LoggingModel instances to the EmployeeModel instance. The JSON unmarshalling doesn't set this because it simply doesn't know it should.

A quick solution is to fix these references yourself. A quick for-each statement will suffice:
 
Nico van de Kamp
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,

Thanks for you're help,

Rob Spoor wrote:I take it that the model classes are mapped from JSON directly to your entities, right? If so, you're running into a bidirectional relation that is only set on one side. The unmarshalling from JSON creates new LoggingModel instances, and puts them into a list. It then sets that list on the created EmployeeModel instance. The EmployeeModel instance now has a reference to all of its LoggingModel instances. What is still missing is the reference in the LoggingModel instances to the EmployeeModel instance. The JSON unmarshalling doesn't set this because it simply doesn't know it should.

A quick solution is to fix these references yourself. A quick for-each statement will suffice:



Although I do not know what 'unmarshalling' exactly means, it is all right what you here suggest!!!

But I'm AMAZED that what you suggest here works!!!.

I've changed the code into this: So I add you're line just before saving the new employee model. What is amazing me, is that the id of employee is still unknown because the Employee is not saved or...,
Does it work in this way: JPA knows the next id sequence number for the Employee Entity. So this number is retrieved from the JPA and saved as FK for the loggingModel. Is this what is happening?






 
Rob Spoor
Sheriff
Posts: 22511
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nico van de Kamp wrote:Although I do not know what 'unmarshalling' exactly means, it is all right what you here suggest!!!


Unmarshalling is a generic term, often also called deserializing, for turning some form of data (JSON, XML, binary) into objects. The inverse is marshalling / serializing.

But I'm AMAZED that what you suggest here works!!!.


That's great!

So I add you're line just before saving the new employee model. What is amazing me, is that the id of employee is still unknown because the Employee is not saved or...,
Does it work in this way: JPA knows the next id sequence number for the Employee Entity. So this number is retrieved from the JPA and saved as FK for the loggingModel. Is this what is happening?


It depends on the backing database system. What JPA does is first insert the parent record (the one without the foreign key). It then knows the id - either because it retrieved it from a sequence, or from the last inserted id (e.g. in MySQL / MSSQL autoincrement columns). It then uses that id to insert all child records (the ones with the foreign key). It repeats this process as needed.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic