gpt4 book ai didi

java - 当将 OrderColumn 与两个列表一起使用时,Hibernate 会抛出 "Repeated column in mapping for entity"

转载 作者:太空宇宙 更新时间:2023-11-04 13:08:22 24 4
gpt4 key购买 nike

我有一个棘手的问题,让 hibernate 对同一实体的两个列表进行排序。通过一些代码,可以更容易地理解我想要做什么。

@MappedSuperclass 
public abstract class AbstractParent {
List<CommonObject> commonObjects;

public abstract List<CommonObject> getCommonObjects();
}

@Entity
@Table
public class Child1 extends AbstractParent {

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name="child1_id", nullable = false)
@OrderColumn(name = "sort_index")
public List<CommonObject> getCommonObject() {
return this.commonObjects;
}
}

@Entity
@Table
public class Child2 extends AbstractParent {

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name="child2_id", nullable = false)
@OrderColumn(name = "sort_index")
public List<CommonObject> getCommonObject() {
return this.commonObjects;
}
}

但是因为 hibernate 处理列“sort_index”的映射,所以它被映射两次(对于 Child1 和 Child2)。所以我收到这个错误:

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity ... column: sort_index (should be mapped with insert="false" update="false")

我知道如果我放置两个不同的列进行排序,我可以解决这个问题。但我想知道是否有人有更好的解决方案可以给我。

谢谢

最佳答案

我添加了一个测试来复制您的问题on GitHub修改映射后它就可以工作了:

@MappedSuperclass
public abstract class AbstractParent {
public abstract List<CommonObject> getCommonObjects();
}

@Entity(name = "Child1")
public class Child1 extends AbstractParent {

@Id
private Long id;

@OneToMany(targetEntity = CommonObject.class, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinTable(name = "child_1_common_objects", joinColumns = @JoinColumn(name="child1_id", nullable = false))
@OrderColumn(name = "sort_index")
private List<CommonObject> commonObjects = new ArrayList<>();

public List<CommonObject> getCommonObjects() {
return this.commonObjects;
}
}

@Entity(name = "Child2")
public class Child2 extends AbstractParent {

@Id
private Long id;

@OneToMany(targetEntity = CommonObject.class, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinTable(name = "child_2_common_objects", joinColumns = @JoinColumn(name="child2_id", nullable = false))
@OrderColumn(name = "sort_index")
private List<CommonObject> commonObjects = new ArrayList<>();

public List<CommonObject> getCommonObjects() {
return this.commonObjects;
}
}

@Entity(name = "CommonObject")
public class CommonObject {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
}

关于java - 当将 OrderColumn 与两个列表一起使用时,Hibernate 会抛出 "Repeated column in mapping for entity",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34203162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com