Hibernate: how to map parent table with static data child table?

I’m trying to figure out how to map an an parent table ‘Employee’, with an static data child table. (Is ‘static data’ the right name for this or do you call it: reference data, list of values, lookup data?)

When I insert an new employee the id record number of the static table data record must be added to the employee table. Nothing needs to be changed on the static data table side (= child table).

Unit is my static data table with the fields: id, unitcode, description.

This is my mapping on the employee side:

Public class Employee {

@ManyToOne(fetch = FetchType.LAZY) //No cascade, because on static table side, nothing need to be changed. I think it needs a many-to-one because there more employees working in one team.
private Unit unit;


[/code]In the employee table I have an field ‘unit_id’ (generated by Hibernate, due to ManyToOne declaration, that’s for me an indication that it is till so far good)

On the unit side I have nothing defined.

When I insert an employee record: { "employeeName": "Name", "employeeCode": "ECN0004", "designation": "ZZZZZ", "unit": { "unit_id": 3 } } I get the error: [tt]org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : nl.nkamp.crud.model.EmployeeModel.unit → nl.nkamp.crud.moel[/tt]
But this error indicates if I’m right, that hibernate want to save the data on the side of the child table and it is gone, before it can be saved on the parent side. But I only want to save the unit id on the parent = employee side.[/color][/b]

I have tried different FetchTypes.

I hope that someone can point me in the right direction to help me out.

The problem, as you can read on various forums like here or Stackoverflow, is that the Unit object is like the messages says is transient. You can read more about the entity states here: Hibernate ORM 5.5.7.Final User Guide

So to resolve this, you have to set a managed object on that field which is best retrieved by using entityManager.getReference(Unit.class, unitId).