[JIRA] (HHH-15786) Updating entity having @OneToOne with inverse side within embeddable leads to `ConstraintViolationException`
by Marko Bekhta (JIRA)
Marko Bekhta ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiZjNhMWNjMDVk... ) / Task ( https://hibernate.atlassian.net/browse/HHH-15786?atlOrigin=eyJpIjoiZjNhMW... ) HHH-15786 ( https://hibernate.atlassian.net/browse/HHH-15786?atlOrigin=eyJpIjoiZjNhMW... ) Updating entity having @OneToOne with inverse side within embeddable leads to `ConstraintViolationException` ( https://hibernate.atlassian.net/browse/HHH-15786?atlOrigin=eyJpIjoiZjNhMW... )
Issue Type: Task Assignee: Unassigned Components: hibernate-core Created: 30/Nov/2022 08:54 AM Priority: Major Reporter: Marko Bekhta ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
Discovered while running tests for Hibernate Search with Hibernate *ORM 6.2* upgrade.
With the model like this:
@Entity(name = "entitya")
public static class EntityA {
@Id
private Integer id;
private String name;
@OneToOne
private EntityB entityB;
}
@Embeddable
public static class EmbeddedValue implements Serializable {
@OneToOne
private EntityA entityA;
}
@Entity(name = "entityb")
public static class EntityB {
@Id
private Integer id;
private String name;
@Embedded
private EmbeddedValue embedded = new EmbeddedValue();
}
if an already saved entity is getting updated:
EntityA entityA = session.get( EntityA.class, "1" );
EntityB anotherEntityB = new EntityB( 3 );
entityA.getEntityB().getEmbedded().setEntityA( null );
entityA.setEntityB( anotherEntityB );
anotherEntityB.getEmbedded().setEntityA( entityA );
it leads to:
jakarta.persistence.PersistenceException: could not execute statement [Unique index or primary key violation: "PUBLIC.CONSTRAINT_INDEX_8 ON PUBLIC.CONTAINED(CONTAINING NULLS FIRST) VALUES ( /* 4 */ 2 )"; SQL statement:
insert into contained (containing,excludedFromAll,inBidirectionalEmbedded,inContainedSingle,includedInElementCollection,includedInEmbeddedList,includedInEmbeddedSingle,id) values (?,?,?,?,?,?,?,?) [23505-214]]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:163)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:169)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1406)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:477)
at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2234)
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:1929)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:454)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:183)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:281)
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:101)
...
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement [Unique index or primary key violation: "PUBLIC.CONSTRAINT_INDEX_8 ON PUBLIC.CONTAINED(CONTAINING NULLS FIRST) VALUES ( /* 4 */ 2 )"; SQL statement:
insert into contained (containing,excludedFromAll,inBidirectionalEmbedded,inContainedSingle,includedInElementCollection,includedInEmbeddedList,includedInEmbeddedSingle,id) values (?,?,?,?,?,?,?,?) [23505-214]]
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:60)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:56)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:96)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:200)
at org.hibernate.engine.jdbc.mutation.internal.AbstractMutationExecutor.performNonBatchedMutation(AbstractMutationExecutor.java:98)
at org.hibernate.engine.jdbc.mutation.internal.MutationExecutorSingleNonBatched.performNonBatchedOperations(MutationExecutorSingleNonBatched.java:40)
at org.hibernate.engine.jdbc.mutation.internal.AbstractMutationExecutor.execute(AbstractMutationExecutor.java:43)
at org.hibernate.persister.entity.mutation.InsertCoordinator.doStaticInserts(InsertCoordinator.java:160)
at org.hibernate.persister.entity.mutation.InsertCoordinator.coordinateInsert(InsertCoordinator.java:97)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2931)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:101)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:615)
at org.hibernate.engine.spi.ActionQueue.lambda$executeActions$1(ActionQueue.java:486)
at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:721)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:483)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:329)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1402)
... 48 more
Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Unique index or primary key violation: "PUBLIC.CONSTRAINT_INDEX_8 ON PUBLIC.CONTAINED(CONTAINING NULLS FIRST) VALUES ( /* 4 */ 2 )"; SQL statement:
insert into contained (containing,excludedFromAll,inBidirectionalEmbedded,inContainedSingle,includedInElementCollection,includedInEmbeddedList,includedInEmbeddedSingle,id) values (?,?,?,?,?,?,?,?) [23505-214]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:508)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:477)
at org.h2.message.DbException.get(DbException.java:223)
at org.h2.message.DbException.get(DbException.java:199)
at org.h2.index.Index.getDuplicateKeyException(Index.java:525)
at org.h2.mvstore.db.MVSecondaryIndex.checkUnique(MVSecondaryIndex.java:223)
at org.h2.mvstore.db.MVSecondaryIndex.add(MVSecondaryIndex.java:184)
at org.h2.mvstore.db.MVTable.addRow(MVTable.java:519)
at org.h2.command.dml.Insert.insertRows(Insert.java:174)
at org.h2.command.dml.Insert.update(Insert.java:135)
at org.h2.command.dml.DataChangeStatement.update(DataChangeStatement.java:74)
at org.h2.command.CommandContainer.update(CommandContainer.java:169)
at org.h2.command.Command.executeUpdate(Command.java:252)
at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:209)
at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:169)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197)
... 63 more
I’ll soon submit a test reproducing the issue.
( https://hibernate.atlassian.net/browse/HHH-15786#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-15786#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100210- sha1:fb625d0 )
2 years, 1 month