JPA:映射复杂数据

时间:2012-10-23 19:41:03

标签: data-binding jpa

我有这个模型的复杂数据:

@Entity
public class A {
    @id
    public Long id;

    @Column
    public String code;

    @OneToMany
    @JoinTable(name = "A_REF",
    joinColumns = @JoinColumn(name = "ID_A"),
    inverseJoinColumns = @JoinColumn(name = "ID_REF"))
    public Set<AbstractRef> refs;

}


@Entity
@Table("T_REF")
@DiscriminatorColumn(name="type")
public abstract class AbstractRef {
    @id
    public Long id;

    @Column
    public String value;
}



@Entity
@DiscriminatorValue(value="B")
public class B extends AbstractRef {
}



@Entity
@DiscriminatorValue(value="C")
public class C extends AbstractRef{
}

@Entity
@DiscriminatorValue(value="D")
public class D extends AbstractRef {
}

数据库示例:

T_A
ID | NAME
1  |  X
2  |  Y

T_REF
ID | VALUE | TYPE | ID_A
1  |  H    |   B  |  1
2  |  I    |   B  |  1
3  |  H    |   B  |  2
4  |  P    |   C  |  1
5  |  O    |   C  |  1
6  |  O    |   C  |  2
7  |  Z    |   D  |  1
8  |  F    |   D  |  1
9  |  M    |   D  |  2

我想通过使用B,C和D的集合值来获得A(1,X),而不是通过技术ID

List<String> values = Arrays.asList(new String[]{"H","I","P","O","Z","F"});
A aWithFunctinnalId = service.getA(values);

对于我的情况,值的集合(“H”,“I”,“P”,“O”,“Z”,“F”)是我的函数id。

我怎么能这样做?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

你应该可以使用JPQL,例如(其中:size是数组的大小)

Select a from A a where (Select count(r) from a.refs r where r.value in :values) >=:size