升级Hibernate会导致列表的收集错误的索引列为空

时间:2019-02-14 09:02:11

标签: java hibernate hibernate-onetomany

我们正在尝试将休眠版本从4.3.5升级到5.4.0。这样,jpa版本也从hibernate-jpa-2.1更改为jpa-2.2。 升级后,当将List <>集合与@OrderColumn

一起使用时,会出现以下错误

javax.persistence.PersistenceException: org.hibernate.HibernateException: null index column for collection

它是具有以下注释的单向一对一映射

@Entity
@Table(name = "DS_GENERIC")
@PrimaryKeyJoinColumn(name = "DSG_DS_ID")
public class GenericConnection extends DataSourceConnection {

    @OneToMany(cascade = ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    @JoinColumn(name = "DSC_DS_ID", nullable = false)
    @OrderColumn(name = "DSC_ORDER", updatable = false, insertable = false)
    List<Credentials> credentials = new ArrayList<>();

}

@Entity
@Table(name = "DS_CREDENTIALS")
public class Credentials {

    @Id
    @GeneratedValue(generator = "uid-generator")
    @Column(name = "DSC_ID", updatable = false)
    private Long id;

    //...
}

以下测试代码失败

//construct the connection object with credentials
entityManager.persist(connection);
entityManager.find(DataSourceConnection.class, id); //this call fails with the above error

请注意,此代码在hibernate 4.3.5中工作正常,并且一种可行的解决方法是将List转换为Set,如下所示:

@OneToMany(cascade = ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@JoinColumn(name = "DSC_DS_ID", nullable = false)
@OrderBy("DSC_ID")
Set<Credentials> credentials = new HashSet<>();

我不了解此失败的根本原因。任何输入将不胜感激。

谢谢。

0 个答案:

没有答案