У меня есть сущность с составным первичным ключом, реализованная с помощью @IdClass. Когда я хочу использовать таблицу сбора элементов, в которой столбцы первичного ключа используются в качестве внешних ключей, hibernate envers дает нулевые значения для таблицы аудита. Когда я отключаю аудит, мой код работает нормально. Я не мог понять, в чем проблема. Может ли кто-нибудь мне помочь?
Мой IdClass:
@Embeddable
public class SubtaskItemPK implements Serializable {
private UUID subtask;
private UUID item;
public SubtaskItemPK(){
}
public SubtaskItemPK(UUID subtask, UUID item) {
this.subtask = subtask;
this.item = item;
}
public UUID getSubtask() {
return subtask;
}
public void setSubtask(UUID subtask) {
this.subtask = subtask;
}
public UUID getItem() {
return item;
}
public void setItem(UUID item) {
this.item = item;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SubtaskItemPK that = (SubtaskItemPK) o;
return subtask.equals(that.subtask) &&
item.equals(that.item);
}
@Override
public int hashCode() {
return Objects.hash(subtask, item);
}
}
Моя сущность, которую я хочу использовать @ElementCollection:
@Entity
@Audited
@IdClass(SubtaskItemPK.class)
@Table(name = "subtask_items")
public class SubtaskItem {
private Subtask subtask;
private Item item;
private Collection<String> PhotoUrlList;
@Id
@ManyToOne
@JoinColumn(name = "subtask_id", referencedColumnName = "id", columnDefinition = "uuid")
public Subtask getSubtask() {
return subtask;
}
public void setSubtask(Subtask subtask) {
this.subtask = subtask;
}
@Id
@ManyToOne
@JoinColumn(name = "items_id", referencedColumnName = "id", columnDefinition = "uuid")
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
@ElementCollection
@CollectionTable(
name = "subtask_item_photos",
joinColumns = {
@JoinColumn(name = "item_id", referencedColumnName = "items_id"),
@JoinColumn(name = "subtask_id", referencedColumnName = "subtask_id")
}
)
@Column(name = "url", columnDefinition = "TEXT")
public Collection<String> getPhotoUrlList() {
return PhotoUrlList;
}
public void setPhotoUrlList(Collection<String> photoUrlList) {
PhotoUrlList = photoUrlList;
}
}
Таблица ElementCollection, которую я хочу использовать
Ошибка, которую я получаю:
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:109)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:200)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3208)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3722)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:91)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)
at org.hibernate.engine.spi.ActionQueue.lambda$executeActions$1(ActionQueue.java:478)
at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:684)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:475)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:348)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:40)
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:108)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1344)
... 155 common frames omitted
Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "item_id" violates not-null constraint
Detail: Failing row contains (null, null, dummyString, 1229, 0).
Запрос, сгенерированный hibernate:
Hibernate:
insert
into
audit.subtask_item_photos_aud
(revtype, rev, item_id, subtask_id, url)
values
(?, ?, ?, ?, ?)
2020-04-08 13:15:12.050 TRACE [,0a5a8a68604ce2c0,4c33c3ce8b492db9,true] 17396 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [INTEGER] - [0]
2020-04-08 13:15:12.050 TRACE [,0a5a8a68604ce2c0,4c33c3ce8b492db9,true] 17396 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [BIGINT] - [1230]
2020-04-08 13:15:12.050 TRACE [,0a5a8a68604ce2c0,4c33c3ce8b492db9,true] 17396 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [3] as [OTHER] - [null]
2020-04-08 13:15:12.050 TRACE [,0a5a8a68604ce2c0,4c33c3ce8b492db9,true] 17396 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [4] as [OTHER] - [null]
2020-04-08 13:15:12.050 TRACE [,0a5a8a68604ce2c0,4c33c3ce8b492db9,true] 17396 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [5] as [VARCHAR] - [dummyString]
2020-04-08 13:15:12.095 ERROR [,0a5a8a68604ce2c0,4c33c3ce8b492db9,true] 17396 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: null value in column "item_id" violates not-null constraint