4

我有一个场景。

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

现在在第三张桌子我想要这样的东西

@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,并尝试更新 child 1 中 child2 的对象。

4

1 回答 1

6

如果您使用的是休眠,则可以向映射添加@Where条件。@OneToMany

例如:

@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

于 2012-12-20T13:37:37.257 回答