繁体   English   中英

JPQL 中简单的多对多选择查询和可嵌入键

[英]Simple many-to-many select query in JPQL and embeddable key

我有这个模型,只有两个实体,一个用于键的可嵌入实体和一个将该键作为 id 字段的实体。

我想知道,如何编写像“给我一个 id 为 5 的人的所有功能”和“给我一个名字为 Somebody 的人的所有功能”这样的简单查询。

当有可嵌入的密钥时,我不明白如何访问这些信息......

我对重写我的模型犹豫不决,因为我必须围绕代码重写大量的东西。

我什至如何从该关联表中删除一些内容? 我真的不知道我应该采取哪种“方式”来解决这个问题。

谢谢各位大佬指点

@Entity
@Table(name = "PERSON")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "person_id")
    private Long id;

    @Column(name = "name", unique = true)
    private String name;
    // .. getters and setters

@Entity
@Table(name = "FUNC")
public class Function {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "function_id")
    private Long id;

    @Column(name = "name")
    private String name;
    // .. getters and setter

@Embeddable
public class PersonFunctionPK {

    @Column(name = "person_id")
    private Long personId;

    @Column(name = "function_id")
    private Long functionId;

    public PersonFunctionPK() {
    }

    PersonFunctionPK(Long personId, Long functionId) {
        this.personId = personId;
        this.functionId = functionId;
    }
    // .. getters and setter

@Entity
@Table(name = "PERSON_FUNC")
public class PersonFunction {

    @EmbeddedId
    protected PersonFunctionPK personFunctionPK;

    public PersonFunction() {}

    public PersonFunction(PersonFunctionPK personFunctionPK) {
        this.personFunctionPK = personFunctionPK;
    }

    public PersonFunction(Long personId, Long functionId) {
        this.personFunctionPK = new PersonFunctionPK(personId, functionId);
    }

    // .. getters and setter for personFunctionPK

您似乎将这些映射为单个独立实体。 如果您映射实体之间的关系,那么您应该能够通过简单地调用 get 方法来完成大部分查询(不需要 jpql)

@Entity
@Table(name = "PERSON")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "person_id")
    private Long id;

    @Column(name = "name", unique = true)
    private String name;

    @ManyToMany(mappedBy = "persons", cascade=CascadeType.ALL) 
    private Collection<Function> functions;

    // .. getters and setters

@Entity
@Table(name = "FUNC")
public class Function {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "function_id")
    private Long id;

    @Column(name = "name")
    private String name;

    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name = "PERSON_FUNC",
    joinColumns = {@JoinColumn(name = "function_id", referencedColumnName = "id")}, 
    inverseJoinColumns = {@JoinColumn(name = "person_id", referencedColumnName = "id")}) 
    private Collection<Person> persons;

    // .. getters and setter

现在,如果你得到一个 id 为 5 的人,你可以调用一个简单的 getter 来获取这个人的功能。 如果您想将一组函数分配给所有名为 Stefan 的人,您可能仍需要使用 JPQL。 您仍然需要映射 @ManyToMany,因为在 JPQL 中您指定对象关系(而不是底层数据库)

select distinct f from Function f inner join f.persons p where p.name = "Stefan"

我还没有测试过任何这些代码,但它应该大致正确。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM