簡體   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