I have an ActionRequestEntity and then we have events ActionRequestEventEntity which are basically updates for this action request.
The entities defined are as follows (removed non relevant fields for brevity) :
@Table(name = "action_request")
@Entity(name = "action_request")
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString(onlyExplicitlyIncluded = true)
public class ActionRequestEntity implements Serializable {
@EmbeddedId
@ToString.Include
private ActionRequestPK pk;
@Column(name = "version")
@ToString.Include
private int version;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "request", cascade = {CascadeType.ALL})
private List<ActionRequestEventEntity> events;
}
And the ActionRequestEventEntity
@Table(name = "action_request_events")
@Entity(name = "action_request_event")
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString(onlyExplicitlyIncluded = true)
public class ActionRequestEventEntity implements Serializable {
@EmbeddedId
@ToString.Include
private ActionRequestPK pk;
@Column(name = "version")
@ToString.Include
private int version;
@ManyToOne(optional = false, cascade = {CascadeType.ALL})
@JoinColumn(name = "request_id", referencedColumnName = "id", insertable = false, updatable = false)
@JoinColumn(name = "org_id", referencedColumnName = "org_id", insertable = false, updatable = false)
private ActionRequestEntity request;
}
And ActionRequestPK
@Embeddable
@Setter
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString(includeFieldNames=false)
public class ActionRequestPK implements Serializable {
@Column(name = "id")
@GeneratedValue
private String id;
@Column(name = "org_id")
private String orgId;
}
Repository:
@Component
public interface ActionRequestRepository extends JpaRepository<ActionRequestEntity, ActionRequestPK> {
@Query("select ar from action_request ar join fetch ar.events where ar.pk.orgId = ?#{principal?.orgId} and ar.id in :requestIds order by ar.createdOn desc")
List<ActionRequestEntity> findAllById(List<String> requestIds);
@Query("select ar from action_request ar join fetch ar.events where ar.pk.orgId = ?#{principal?.orgId} and ar.id = :requestId")
ActionRequestEntity findOneById(String requestId);
@Query("select ar from action_request ar join fetch ar.events where ar.pk.orgId = ?#{principal?.orgId} and ar.entityId = :entityId")
List<ActionRequestEntity> findAllByEntityId(String entityId);
@Query("select ar from action_request ar join fetch ar.events where ar.pk.orgId = ?#{principal?.orgId} and ar.entityId in :entityIds")
List<ActionRequestEntity> findAllByEntityIds(List<String> entityIds);
}
Service:
@Component
public class ActionRequestService {
@Transactional
public void save(ActionRequestEntity actionRequestEntity){
actionRequestRepository.save(actionRequestEntity);
}
@Transactional
public void saveAll(List<ActionRequestEntity> actionRequestEntities){
actionRequestRepository.saveAll(actionRequestEntities);
}
}
While saving ActionRequestEntiy I get the following error:
23:41:41.954+05 [thread='Test worker' user='' org=''] DEBUG org.hibernate.SQL - insert into action_request_events (created_at, details, status, version, id, org_id) values (?, ?, ?, ?, ?, ?)
23:41:41.955+05 [thread='Test worker' user='' org=''] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [TIMESTAMP] - [2021-10-18T20:36:17.585Z]
23:41:41.955+05 [thread='Test worker' user='' org=''] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [VARCHAR] - [Test Details]
23:41:41.956+05 [thread='Test worker' user='' org=''] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [3] as [INTEGER] - [0]
23:41:41.956+05 [thread='Test worker' user='' org=''] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [4] as [INTEGER] - [0]
23:41:41.957+05 [thread='Test worker' user='' org=''] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [5] as [VARCHAR] - [c8083219-badc-4197-8da5-ecfdb8a32a31]
23:41:41.957+05 [thread='Test worker' user='' org=''] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [6] as [VARCHAR] - [orgid1]
23:41:41.959+05 [thread='Test worker' user='' org=''] WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 23502
23:41:41.960+05 [thread='Test worker' user='' org=''] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ERROR: null value in column "request_id" violates not-null constraint
Detail: Failing row contains (c8083219-badc-4197-8da5-ecfdb8a32a31, orgid1, 0, null, 0, Test Details, 2021-10-19 02:06:17.585).
could not execute statement; SQL [n/a]; constraint [request_id]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [request_id]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:276)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:233)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:566)
..<Other items in the stacktrace reduced for brevity>
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:743)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:711)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:109)
..<Other items in the stacktrace reduced for brevity>
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:183)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$300(JdbcResourceLocalTransactionCoordinatorImpl.java:40)
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)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:562)
... 90 more
Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "request_id" violates not-null constraint
Detail: Failing row contains (c8083219-badc-4197-8da5-ecfdb8a32a31, orgid1, 0, null, 0, Test Details, 2021-10-19 02:06:17.585).
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2552)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2284)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:322)
..<Other items in the stacktrace reduced for brevity>
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at io.opentracing.contrib.common.WrapperProxy$1.invoke(WrapperProxy.java:73)
at com.sun.proxy.$Proxy311.executeUpdate(Unknown Source)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197)
... 110 more
I have gone through these to figure out what might be wrong but cant seem to get my hand on it. How to save parent and child in one shot (JPA & Hibernate)
JPA OneToMany/ManyToOne relationship not working - What am I missing?
missing value after saving cascading entity in spring jpa entity
Would love for some help here to figure out why the parent entity is not converted to a request_id string and set as null.
Update:
Added the logs from hibernate that show the queries being created, in the insert query Hibernate is not trying to even insert the request_id column.