DO Quang Bao (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=61f8fb9...
) *created* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNjk0ZWIzYTEx...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16834?atlOrigin=eyJpIjoiNjk0ZW...
) HHH-16834 (
https://hibernate.atlassian.net/browse/HHH-16834?atlOrigin=eyJpIjoiNjk0ZW...
) Impossible to remove an entity that contains OneToMany relation (CascadeType_ALL) while
using @EmbeddedId for the entity associated (
https://hibernate.atlassian.net/browse/HHH-16834?atlOrigin=eyJpIjoiNjk0ZW...
)
Issue Type: Bug Affects Versions: 6.2.4 Assignee: Unassigned Created: 21/Jun/2023 09:39 AM
Environment: Spring boot 3.1.0
Hibernate 6.2.4.Final
Database H2 Priority: Blocker Reporter: DO Quang Bao (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=61f8fb9...
)
I encountered an exception while attempting to remove EntityA, which has a OneToMany
relationship with EntityB. The joinColumn is one of the fields within the EmbeddedId in
EntityB. I'm aware that Hibernate tries to set the join column as null, provided that
it has the same value as EntityA's id, before deleting EntityA and all associated
EntityBs. However, it's possible that a not-null constraint exists for the EmbeddedId
object, which prevents setting it as null. OR MAYBE i don’t use it correctly?
@Entity
@Table("entitya")
@Getter @Setter
public class EntityA {
@Id @org.springframework.data.annotation.Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@Fetch(FetchMode.JOIN)
@JoinColumn(name = "entityaid")
private final List<EntityB> entityBList = new ArrayList<>();
public void addEntityB(final EntityB entityB) {
entityBList.add(entityB);
}
}
@Entity
@Table("entityb")
@Getter
public class EntityB {
@EmbeddedId
private EmbeddedKey id = new EmbeddedKey();
public void setType(final Integer type) {
id.type = type;
}
@Embeddable
@Getter
@Setter
static class EmbeddedKey {
@Column(name = "entityaid", updatable = false)
private Integer entityAId;
@Column(name = "type", updatable = false)
private Integer type;
}
}
//Test method in test class
public void testDeleteEntityA() {
final EntityA entityA = new EntityA();
final EntityB entityB = new EntityB();
entityB.setType(5);
entityA.addEntityB(entityB);
entityManager.getTransaction().begin();
final EntityA entityAPersisted = entityManager.persist(entityA);
entityManager.getTransaction().commit();
entityManager.getTransaction().begin();
entityManager.remove(entityAPersisted); //Exception throwed here
entityManager.getTransaction().commit();
}
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute
statement [NULL non permis pour la colonne "entityaid"
NULL not allowed for column "entityaid"; SQL statement:
update entityB set entityaid=null where entityaid=? [23502-197]] [update entityb set
entityaid=null where entityaid=?]
at
org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:95)
at
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:56)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:108)
at
org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:278)
at
org.hibernate.engine.jdbc.mutation.internal.AbstractMutationExecutor.performNonBatchedMutation(AbstractMutationExecutor.java:108)
at
org.hibernate.engine.jdbc.mutation.internal.MutationExecutorSingleNonBatched.performNonBatchedOperations(MutationExecutorSingleNonBatched.java:40)
at
org.hibernate.engine.jdbc.mutation.internal.AbstractMutationExecutor.execute(AbstractMutationExecutor.java:53)
at
org.hibernate.persister.collection.mutation.RemoveCoordinatorStandard.deleteAllRows(RemoveCoordinatorStandard.java:109)
at
org.hibernate.persister.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:1088)
at
org.hibernate.action.internal.CollectionRemoveAction.execute(CollectionRemoveAction.java:112)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:606)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:475)
at
org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:358)
at
org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
at
org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:127)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1412)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:485)
at
org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2301)
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:1966)
at
org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:439)
at
org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:169)
at
org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:267)
at
org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:101)
at
org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:561)
... 25 common frames omitted
Caused by: org.h2.jdbc.JdbcSQLException: NULL non permis pour la colonne
"entityaid"
NULL not allowed for column "entityaid"; SQL statement:
update entityb set entityaid=null where entityaid=? [23502-197]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.table.Column.validateConvertUpdateSequence(Column.java:374)
at org.h2.table.Table.validateConvertUpdateSequence(Table.java:798)
at org.h2.command.dml.Update.update(Update.java:157)
at org.h2.command.CommandContainer.update(CommandContainer.java:102)
at org.h2.command.Command.executeUpdate(Command.java:261)
at
org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:199)
(
https://hibernate.atlassian.net/browse/HHH-16834#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16834#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=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100227- sha1:28a616e )