Hibernate Envers audit table foreign key columns gets null value from Element Collection – Spring Boot

huangapple 未分类评论46阅读模式
英文:

Hibernate Envers audit table foreign key columns gets null value from Element Collection - Spring Boot

问题

我有一个使用@IdClass实现的复合主键实体。当我想使用一个元素集合表,该表使用主键列作为外键时,Hibernate Envers将null值提供给审核表。当我禁用审核时,代码可以正常运行。我无法理解问题出在哪里。有人能帮助我吗?

我的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;
    }

    // 省略其他方法...
}

我想使用@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;
    }

    // 省略其他方法...

    @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;
    }

    // 省略其他方法...
}

错误信息:

Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
...
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
    (?, ?, ?, ?, ?)
...

元素集合表的截图:

Hibernate Envers audit table foreign key columns gets null value from Element Collection – Spring Boot

英文:

I have an entity with a Composite Primary Key implemented with @IdClass. When I want to use an Element Collection Table that uses primary key columns as foreign keys, hibernate envers gives null values to the audit table. When I disable auditing my code works fine. I could not understand what is the problem. Can someone help me?

My 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) &amp;&amp;
            item.equals(that.item);
}

@Override
public int hashCode() {
    return Objects.hash(subtask, item);
}
}

My Entity that I want to use @ElementCollection:

@Entity
@Audited
@IdClass(SubtaskItemPK.class)
@Table(name = &quot;subtask_items&quot;)
public class SubtaskItem {
private Subtask subtask;
private Item item;
private Collection&lt;String&gt; PhotoUrlList;

@Id
@ManyToOne
@JoinColumn(name = &quot;subtask_id&quot;, referencedColumnName = &quot;id&quot;, columnDefinition = &quot;uuid&quot;)
public Subtask getSubtask() {
    return subtask;
}

public void setSubtask(Subtask subtask) {
    this.subtask = subtask;
}

@Id
@ManyToOne
@JoinColumn(name = &quot;items_id&quot;, referencedColumnName = &quot;id&quot;, columnDefinition = &quot;uuid&quot;)
public Item getItem() {
    return item;
}

public void setItem(Item item) {
    this.item = item;
}

@ElementCollection
@CollectionTable(
        name = &quot;subtask_item_photos&quot;,
        joinColumns = {
                @JoinColumn(name = &quot;item_id&quot;, referencedColumnName = &quot;items_id&quot;),
                @JoinColumn(name = &quot;subtask_id&quot;, referencedColumnName = &quot;subtask_id&quot;)
        }
)
@Column(name = &quot;url&quot;, columnDefinition = &quot;TEXT&quot;)
public Collection&lt;String&gt; getPhotoUrlList() {
    return PhotoUrlList;
}

public void setPhotoUrlList(Collection&lt;String&gt; photoUrlList) {
    PhotoUrlList = photoUrlList;
}
}

The ElementCollection table that I want to use

The Error I am getting is:

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 &quot;item_id&quot; violates not-null constraint
Detail: Failing row contains (null, null, dummyString, 1229, 0).

The query generated by hibernate is:

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 &quot;item_id&quot; violates not-null constraint

huangapple
  • 本文由 发表于 2020年4月8日 03:33:41
  • 转载请务必保留本文链接:https://java.coder-hub.com/61088013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定