使用继承映射的一对多JPA映射

时间:2022-09-25 10:37:07

I have one scenario.

我有一个场景。

@Entity
@Table(name = "someTable")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Access(AccessType.FIELD)
@DiscriminatorColumn(name = "someDisc")
public abstract class AbstractClass{}

and

@Entity
@Access(AccessType.FIELD)
@DiscriminatorValue("1")
public class Child1 extends AbstractClass{
}

@Entity
@Access(AccessType.FIELD)
@DiscriminatorValue("2")
public class Child2 extends AbstractClass{
}

Now in 3rd table I want something like this

现在在第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
}

now while it persists well and values are getting saved correctly in the table. The problem I face while fetching object using ThridTable object. The generated query doesn't ditinguish between two instances ie child1 and child2 in same table and tries to update object of child2 in child one.

现在虽然它持续存在并且值正在表中正确保存。使用ThridTable对象获取对象时遇到的问题。生成的查询不会在同一个表中的两个实例(即child1和child2)之间进行定位,并尝试更新子项1中的child2的对象。

1 个解决方案

#1


5  

if you're using hibernate you can add a @Where condition to the @OneToMany Mappings.

如果您正在使用休眠,则可以在@OneToMany映射中添加@Where条件。

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<>();

have a look at the api: http://docs.jboss.org/hibernate/core/4.1/javadocs/org/hibernate/annotations/Where.html

看看api:http://docs.jboss.org/hibernate/core/4.1/javadocs/org/hibernate/annotations/Where.html

and: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-hibspec-collection

和:http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-hibspec-collection

#1


5  

if you're using hibernate you can add a @Where condition to the @OneToMany Mappings.

如果您正在使用休眠,则可以在@OneToMany映射中添加@Where条件。

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<>();

have a look at the api: http://docs.jboss.org/hibernate/core/4.1/javadocs/org/hibernate/annotations/Where.html

看看api:http://docs.jboss.org/hibernate/core/4.1/javadocs/org/hibernate/annotations/Where.html

and: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-hibspec-collection

和:http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-hibspec-collection