休眠升级后的问题 - 休眠中的嵌套循环(CascadeType 和 EmbeddedId)

Problem after hibernate upgrade - nested loop in hibernate (CascadeType and EmbeddedId)

我已将 hibernate 版本升级到 5.3。10.Final (hibernate-core) 以在 java 8 和 WildFly 17.0.1 Final 上工作。该应用程序的大部分工作正常,但休眠某些操作中的实体和递归循环存在一个问题。 (升级前没有这个问题)

我试图以某种方式更改实体的属性来修复此问题,但无法获得正确的版本。问题是,当没有对项目进行任何更改然后执行某些操作(在数据库中搜索)时,我得到了由以下原因引起的休眠嵌套循环:

Caused by: java.lang.WhosebugError
    Caused by: java.lang.WhosebugError
    at org.jboss.jca.adapters.jdbc.WrappedConnection.checkException(WrappedConnection.java:2019)
    at org.jboss.jca.adapters.jdbc.WrappedStatement.checkException(WrappedStatement.java:1436)
    at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.executeQuery(WrappedPreparedStatement.java:509)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:60)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2167)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1930)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1892)
    at org.hibernate.loader.Loader.doQuery(Loader.java:937)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:310)
    at org.hibernate.loader.Loader.loadEntity(Loader.java:2281)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:64)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:54)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4273)
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:511)
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:481)
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:222)
    at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:119)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:92)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1257)
    at org.hibernate.internal.SessionImpl.immediateLoad(SessionImpl.java:1115)
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:178)
    at org.hibernate.proxy.AbstractLazyInitializer.getIdentifier(AbstractLazyInitializer.java:89)
    at org.hibernate.type.EntityType.getHashCode(EntityType.java:372)
    at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:242)
    at org.hibernate.engine.spi.EntityKey.generateHashCode(EntityKey.java:61)
    at org.hibernate.engine.spi.EntityKey.<init>(EntityKey.java:54)
    at org.hibernate.internal.AbstractSharedSessionContract.generateEntityKey(AbstractSharedSessionContract.java:524)
    at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:867)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:718)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:990)
    at org.hibernate.loader.Loader.doQuery(Loader.java:948)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:310)
    at org.hibernate.loader.Loader.loadEntity(Loader.java:2281)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:64)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:54)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4273)
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:511)
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:481)
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:222)
    at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:119)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:92)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1257)
    at org.hibernate.internal.SessionImpl.immediateLoad(SessionImpl.java:1115)
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:178)
    at org.hibernate.proxy.AbstractLazyInitializer.getIdentifier(AbstractLazyInitializer.java:89)
    at org.hibernate.type.EntityType.getHashCode(EntityType.java:372)
    at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:242)
    at org.hibernate.engine.spi.EntityKey.generateHashCode(EntityKey.java:61)
    at org.hibernate.engine.spi.EntityKey.<init>(EntityKey.java:54)
    at org.hibernate.internal.AbstractSharedSessionContract.generateEntityKey(AbstractSharedSessionContract.java:524)
    at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:867)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:718)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:990)
    at org.hibernate.loader.Loader.doQuery(Loader.java:948)
... (the same in loop)

我注意到第一次调用 doQuery 函数时它显示 937 行号,第二次时它在日志中显示 948 行号。 然后,当我在 WorkinfoEntity 中对下面的代码进行更改以删除级联 MERGE 和 PERSIST 时,Whosebug 的错误在搜索时不会出现在大多数实体上(这是过去,无法再次以这种方式工作...... ) 但有时会出现一些未找到实体的问题(带有一些附件的特殊实体)(但有一些带有附件的工作实体)

Unable to find AttachmentEntity with id AttachmentEntityKey [workInfoAttachmentNumber=1000000351 workInfo=WorkInfoEntity@d198fb73]

(在这个项目中workInfoAttachmentNumber不是唯一的,不像attachemnt的id) 我认为问题可能出在任何 hashCodeequals 函数或 @EmbeddedId class (我读过它们不应该被使用但它们已经在项目中并且我不确定我是否可以更改它)。我试图更改注释属性(即使我会随机地说)但没有预期的效果。

我认为有问题的classes的部分代码:

@Entity
@Table(name = "ITEM_WORK_INFO")
public class WorkInfoEntity extends AbstractEntryEntity implements Serializable {
    private static final long serialVersionUID = 7681809420366638143L;

    public WorkInfoEntity() {}

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ITEM_ID", nullable = false)
    private AbstractItemEntity item;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "SUBMIT_DATE")
    private Date submitDate;

    @Lob
    @Column(name = "NOTES", columnDefinition = "CLOB")
    private String notes;

    @Column(name = "SUMMARY", length = 4000)
    private String summary;

    @Column(name = "LAST_MODIFIED_BY")
    private String lastModifiedBy;

    @Column(name = "EMPTY_SLOTS")
    private String emptySlots = "";

    @OneToMany(mappedBy = "id.workInfo", cascade = {CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY) 
    // @Fetch(FetchMode.JOIN)
    private Set<AttachmentEntity> workinfoAttachments = new HashSet<AttachmentEntity>();

    ... // setters and getters

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 23;
        result = (prime * result) + ((emptySlots == null) ? 0 : emptySlots.hashCode());
        result = (prime * result) + ((lastModifiedBy == null) ? 0 : lastModifiedBy.hashCode());
        result = (prime * result) + ((submitDate == null) ? 0 : submitDate.hashCode());
        result = (prime * result) + ((summary == null) ? 0 : summary.hashCode());
        result = (prime * result) + ((workinfoAttachments == null) ? 0 : workinfoAttachments.hashCode());
        result = (prime * result) + ((notes == null) ? 0 : notes.hashCode());
        return result;
    }

    @Override
    public boolean equals(final Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final WorkInfoEntity other = (WorkInfoEntity) obj;
        if (this.submitDate != other.submitDate && (this.submitDate == null || !this.submitDate.equals(other.submitDate))) {
            return false;
        }
        if ((this.notes == null) ? (other.notes != null) : !this.notes.equals(other.notes)) {
            return false;
        }
        if ((this.summary == null) ? (other.summary != null) : !this.summary.equals(other.summary)) {
            return false;
        }
        if ((this.lastModifiedBy == null) ? (other.lastModifiedBy != null) : !this.lastModifiedBy.equals(other.lastModifiedBy)) {
            return false;
        }
        if ((this.emptySlots == null) ? (other.emptySlots != null) : !this.emptySlots.equals(other.emptySlots)) {
            return false;
        }
        if (this.workinfoAttachments != other.workinfoAttachments && (this.workinfoAttachments == null || !this.workinfoAttachments.equals(other.workinfoAttachments))) {
            return false;
        }
        return true;
    }

@Entity
@Table(name = "WORK_INFO_ATTACHMENT")
public class AttachmentEntity implements Serializable, IEntity<AttachmentEntityKey> {
    private static final long serialVersionUID = 6571993598130579018L;
    @EmbeddedId
    AttachmentEntityKey id = new AttachmentEntityKey();

    @Column(name = "FILE_NAME")
    private String fileName;

    @Column(name = "FILE_SIZE")
    private String fileSize;

    public AttachmentEntity() {}

    ... // getters and setters

    @Override
    public AttachmentEntityKey getId() {
        return id;
    }
}
@Embeddable
public class AttachmentEntityKey implements Serializable {

    private static final long serialVersionUID = 2354257422144993554L;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "WORK_INFO_ID", nullable = false)
    private WorkInfoEntity workInfo;

    @Column(name = "WORK_INFO_ATTACHMENT_NO")
    private Integer workInfoAttachmentNumber;

    ... // getters and setters

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof AttachmentEntityKey)) return false;
        AttachmentEntityKey that = (AttachmentEntityKey) o;
        return getWorkInfo() == that.getWorkInfo();
    }

    @Override
    public int hashCode() {
        int result = 19;
        result = (31 * result) + ((getWorkInfo() != null) ? getWorkInfo().hashCode() : 0);
        return result;
    }
}

equalshashCode 方法是我在升级后添加到 AttachmentEntityKey 中的,因为我收到警告说升级后未在此 class 中实现这些方法。 (据说是复合idclass)

我已经使用它几天了,找不到解决方案,所以真的很烦人。您是否看到任何应该更改以尝试使该项目完全运行的内容?

更新 当我删除字段 workinfoAttachments feom 和 class 及其所有用法时,项目会编译并且一切正常。所以我可以假设这是我的问题,但我不知道如何正确解决它。

我已经解决了这个问题,因为它似乎是这里描述的 hibernate+WildFly 错误 https://hibernate.atlassian.net/browse/HHH-13296

所以我的解决方法是删除 EmbeddedId 并在数据库的 table 中创建新的 IDcolumn,这是主键 EmbeddedId 中 2 个使用过的列的串联(并将它们设置在代码中的适当位置)