Search code examples
javahibernatejbossejb

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


I've upgraded the version of hibernate to 5.3.10.Final (hibernate-core) to work on java 8 and on WildFly 17.0.1 Final. Most of the app is working fine but there is one problem with entities and recursive loop in hibernate some actions. (There was no such problem before upgrade)

I've tried to change somehow the properties of entities to repair this problem but cannot get the right version. The problem is that when no changes are done to the project then on some action (searching in database) I get the nested loop of hibernate which is caused by:

Caused by: java.lang.StackOverflowError
    Caused by: java.lang.StackOverflowError
    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)

I have noticed there that the first time the doQueryfunction is called it shows 937 line number and when the second time then it is 948 line number in log. Then when I made changes in code below in WorkinfoEntity to remove the cascade MERGE and PERSIST the error with StackOverflow when searching not appears on most entities (it was in the past and cannot get it working in this way again...) but then sometime there wa some problem with not found entities (special one with some attachemnts) (but there are working entities with attachemnts)

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

(In this project workInfoAttachmentNumber is not unique and is not like the id of attachemnt) I think that the problem may be with any hashCode or equals functions or with the @EmbeddedId class (I've read that they should not be used but they were already in the project and I'm not sure if I can change it). I have tried to change the annotation properties (even I would say somehow randomly) but with no desired effect.

The parts of codes of the classes that I think the problem is in:

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

The equals and hashCode methods were added by me to AttachmentEntityKey after upgrade because I got warning about not implemented these methods in this class after upgrade. (It was said to be composite id class)

I've been working with it for a few days and cannot find solution so it is really annoying. Do you see anything that should be changed in order to try get this project fully working?

UPDATE When I remove the field workinfoAttachments feom the class and all its usages then the project compiles and evrything is working fine. So I can assusme that this is my problem but I don't know how to solve it properly.


Solution

  • I've done a workaround to this problem as it seemed to be the hibernate+WildFly bug as described here https://hibernate.atlassian.net/browse/HHH-13296.

    So my workaround was to remove the EmbeddedId and create new IDcolumn in table in database which is a concatenation of 2 used columns in primary key which was EmbeddedId (and set them in proper places in code)