Persist parent and child - With data integrity

I have noticed it’s not easy to persist a parent with a child at the same time if you want to maintain data integrity at the DB level. In this case, I would like the child’s parent ID to be not null with a foreign key.

This appears to not be possible because Hibernate persists the parent successfully, then the child without updating the child’s parent ID property. This leaves the insert statement for the child with an invalid value for parent ID. I am confused as to why the parent ID isn’t updated automatically; perhaps there is a config option I haven’t found…

Hibernate’s docs seems to suggest this behaviour is intended/known:

Extract below:

@Entity(name = "Phone")
public static class Phone {

	private Long id;

	@Column(name = "`number`")
	private String number;

		mappedBy = "phone",
		cascade = CascadeType.ALL,
		orphanRemoval = true,
		fetch = FetchType.LAZY
	private PhoneDetails details;

	//Getters and setters are omitted for brevity

	public void addDetails(PhoneDetails details) {
		this.details = details;

	public void removeDetails() {
		if (details != null) {
			this.details = null;

@Entity(name = "PhoneDetails")
public static class PhoneDetails {

	private Long id;

	private String provider;

	private String technology;

	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "phone_id")
	private Phone phone;

	//Getters and setters are omitted for brevity

    number VARCHAR(255) ,
    PRIMARY KEY ( id )

CREATE TABLE PhoneDetails (
    provider VARCHAR(255) ,
    technology VARCHAR(255) ,
    phone_id BIGINT ,
    PRIMARY KEY ( id )

ALTER TABLE PhoneDetails
ADD CONSTRAINT FKeotuev8ja8v0sdh29dynqj05p

Why does PhoneDetails need its own ID column? This leaves the possibility to end up with a PhoneDetails row without a valid Phone to link to. Why not just have:


This would link PhoneDetails more tightly to Phone and ensure data is always valid.

Note in my case I am persisting my parent with children in one go, the object comes into my API from JSON so it is impractical to persist the children one by one for every entity/endpoint (and this is surely what Hibernate should be doing for the user!)

Is there some logic to this I’m missing or is it just impossible due to a constraint I’m not aware of? (If so, it would be great to add it to the docs!)

You are mapping two distinct entities, Phone and PhoneDetails, each of which has an @Id private Long id property - that’s why you are seeing distinct id columns. You are then defining a bidirectional @OneToOne association, where PhoneDetails is the owning side and Phone is the indirect side (mappedBy).

If you wish to use the associated Phone’s id as an identifier for PhoneDetails, you should simply drop the Long id column and just annotate the to-one association with @Id. Please also take a look at the @MapsId annotation and at Hibernate’s inheritance capabilities, which might be best suited depending on what you’re trying to achieve.

1 Like

Thank you for the fast reply!

I don’t think inheritance is right for what I’m after. And I think @MapsId requires me to be in the Hibernate context to manage the IDs for me.

I think an example of what I’m trying to do might make more sense. I have the following tables:

create table companies
    id      int auto_increment primary key,
    name    varchar(255)      null

create table company_details
    company_id       int           not null,
    website          varchar(500)  null,
    constraint company_details_companies_id_fk
        foreign key (company_id) references companies (id)
            on delete cascade

Mapped to:

@Table(name = "companies")
public class Company {

	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	public int id;

	@Column(name = "name")
	public String name;

	@OneToOne(mappedBy = "company", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
	public CompanyDetails details;


@Table(name = "company_details")
public class CompanyDetails {

	@Column(name = "company_id")
	public int companyId;

	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "company_id")
	private Company company;

	@Column(name = "website", length = 500)
	public String website;


I am then:

JSON to Java

Take a new Company from JSON:

{"name":"f", "details": {"website": "test"}}

Run it through Jackson and persist it:
Screenshot from 2024-02-13 15-51-25

And I get the following:

2024-02-13 15:53:13.393 | DEBUG | org.hibernate.SQL                        | dev        : insert into companies (name) values (?)
2024-02-13 15:53:13.398 | TRACE | org.hibernate.orm.jdbc.bind              | dev        : binding parameter (1:VARCHAR) <- [f]
2024-02-13 15:53:14.434 | DEBUG | org.hibernate.SQL                        | dev        : insert into company_details (website,company_id) values (?,?)
2024-02-13 15:53:14.440 | TRACE | org.hibernate.orm.jdbc.bind              | dev        : binding parameter (1:VARCHAR) <- [test]
2024-02-13 15:53:14.442 | TRACE | org.hibernate.orm.jdbc.bind              | dev        : binding parameter (2:INTEGER) <- [0]
2024-02-13 15:53:14.449 |  WARN | o.h.engine.jdbc.spi.SqlExceptionHelper   | dev        : SQL Error: 1452, SQLState: 23000
2024-02-13 15:53:14.450 | ERROR | o.h.engine.jdbc.spi.SqlExceptionHelper   | dev        : Cannot add or update a child row: a foreign key constraint fails (`testDb`.`company_details`, CONSTRAINT `company_details_companies_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`) ON DELETE CASCADE)
2024-02-13 15:53:29.866 | ERROR | testDb.Functions.CompanyFunctions        | dev        : Error while committing the transaction
jakarta.persistence.RollbackException: Error while committing the transaction
	at org.hibernate.internal.ExceptionConverterImpl.convertCommitException(
	at org.hibernate.engine.transaction.internal.TransactionImpl.commit(
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement [Cannot add or update a child row: a foreign key constraint fails (`testDb`.`company_details`, CONSTRAINT `company_details_companies_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`) ON DELETE CASCADE)] [insert into company_details (website,company_id) values (?,?)]
	at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(
	at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(
	at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(
	at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(
	at org.hibernate.engine.jdbc.mutation.internal.AbstractMutationExecutor.performNonBatchedMutation(
	at org.hibernate.engine.jdbc.mutation.internal.MutationExecutorSingleNonBatched.performNonBatchedOperations(
	at org.hibernate.engine.jdbc.mutation.internal.AbstractMutationExecutor.execute(
	at org.hibernate.persister.entity.mutation.InsertCoordinator.doStaticInserts(
	at org.hibernate.persister.entity.mutation.InsertCoordinator.coordinateInsert(
	at org.hibernate.persister.entity.AbstractEntityPersister.insert(
	at org.hibernate.action.internal.EntityInsertAction.execute(
	at org.hibernate.engine.spi.ActionQueue.executeActions(
	at org.hibernate.engine.spi.ActionQueue.executeActions(
	at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(
	at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(
	at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(
	at org.hibernate.internal.SessionImpl.doFlush(
	at org.hibernate.internal.SessionImpl.managedFlush(
	at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(
	at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(
	at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(
	at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(
	at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(
	at org.hibernate.engine.transaction.internal.TransactionImpl.commit(
	... 101 common frames omitted
Caused by: java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`testDb`.`company_details`, CONSTRAINT `company_details_companies_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`) ON DELETE CASCADE)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(
	at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeUpdate(
	at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeUpdate(
	at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(
	... 121 common frames omitted

Have I just misinterpreted what I should be doing? I’m aware this might not be the 100% the intended use of Hibernate - most examples show the Java objects being created and manipulated in Java within an entity manager context. But I feel like I’m just missing one tiny annotation I haven’t seen yet!

Thank you :slight_smile:

Thank you for the fast reply. My reply got hidden by the spam filter! Hopefully will be unlocked soon.

I’ve had my reply unhidden now :slight_smile:

This is what’s happening:

  • Company gets persisted, it has a @GeneratedId identifier so when inserting the row in the database a new value is assigned to its id column
  • then, CompanyDetails gets cascade-persisted since it’s mapped as @OneToOne(mappedBy = "company", cascade = CascadeType.ALL, [...]), and it as 0 as companyId
  • when the insert is executed on the datase, the id column of the companies table has another value, so you get a constraint violation error.

If you want the Company foreign key as the identifier for CompanyDetails you should just use @MapsId or remove the companyId property and simply annotate the association with @Id. Otherwise, you will have to manually assign the Company identifier after persisting the entity in the CompanyDetails#companyId property, so you won’t be able to use cascade-persist.

Thank you for the info!

Got it working using @MapsId and the @JsonManagedReference and @JsonBackReference annotations from Jackson.