How to load only ids from Many to Many mapping tables?

前端 未结 2 989
时光说笑
时光说笑 2020-12-30 04:35

In a Many to Many relation between two table with a Mapping table in between, how can I only load ids for the second entity.

Following is the example to explain what

2条回答
  •  粉色の甜心
    2020-12-30 05:03

    In User class:

    @ManyToMany
    @JoinTable(
        name="user_page",
        joinColumns = @JoinColumn(name="id_user"),
        inverseJoinColumns = @JoinColumn(name="id_page")
    )
    public Set pages;
    

    You can get the id's by iterating over the returned set. By default collections are lazily (i.e. only ids) loaded.

    EDIT: If you don't want to map Page for some reason, you can use @ElementCollection like this:

    @ElementCollection
    @CollectionTable(name="user_page", joinColumns=@JoinColumn(name="id_user"))
    @Column(name="id_page")
    public Set pageIds;
    

提交回复
热议问题