我有一个场景。
@Entity
@Table(name = "someTable")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Access(AccessType.FIELD)
@DiscriminatorColumn(name = "someDisc")
public abstract class AbstractClass{}
和
@Entity
@Access(AccessType.FIELD)
@DiscriminatorValue("1")
public class Child1 extends AbstractClass{
}
@Entity
@Access(AccessType.FIELD)
@DiscriminatorValue("2")
public class Child2 extends AbstractClass{
}
现在在第3个表中我想要这样的东西
@Entity
@Table
public class ThridTable{
@OneToMany(cascade = CascadeType.ALL, fetch = EAGER, orphanRemoval = true)
@JoinColumn(name = "foreinKeyCol", nullable = false)
@OrderColumn(name = "orderCol")
private List<Child2> child2 = new ArrayList<>();
@OneToMany(cascade = CascadeType.ALL, fetch = EAGER, orphanRemoval = true)
@JoinColumn(name = "foreinKeyCol", nullable = false)
@OrderColumn(name = "orderCol")
private List<Child1> child1 = new ArrayList<>();
//more setters/getters
}
现在虽然它持续存在并且值正在表中正确保存。使用ThridTable对象获取对象时遇到的问题。生成的查询不会在同一个表中的两个实例(即child1和child2)之间进行调整,并尝试更新子项1中child2的对象。
答案 0 :(得分:5)
如果您正在使用休眠,则可以向@Where
映射添加@OneToMany
条件。
E.g:
@OneToMany(cascade = CascadeType.ALL, fetch = EAGER, orphanRemoval = true)
@JoinColumn(name = "foreinKeyCol", nullable = false)
@OrderColumn(name = "orderCol")
@Where(clause="someDisc=1")
private List<Child1> child1 = new ArrayList<>();
看看api: http://docs.jboss.org/hibernate/core/4.1/javadocs/org/hibernate/annotations/Where.html
和: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-hibspec-collection