如何在JPA中访问通过连接创建的表

huangapple go评论73阅读模式
英文:

How to access table created by join in JPA

问题

我正在创建一个应用程序,其中有两个实体:

  1. 用户

  2. 聊天室

用户和聊天室之间存在多对多的关系。
我使用一个名为"users_chatrooms"的连接表创建了一个多对多的关系。当我为用户编写代码加入聊天室时,连接表中的值被正确地填充。

我的问题是,我需要一个能够获取给定聊天室中所有用户的端点。为此,我需要将连接创建的表(users_chatrooms)作为 JPA 的一部分。如何在 JPA 中实现这一点?

用户类

package com.example.chat.models;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name="USERS")
public class User {
    @Id
    @Column(name="userid")
    private String username;

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


    @ManyToMany(mappedBy = "users", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    List<Chatroom> rooms;

    public List<Chatroom> getRooms() {
        return rooms;
    }

    public void setRooms(List<Chatroom> rooms) {
        this.rooms = rooms;
    }



    public void setUsername(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

聊天室类

package com.example.chat.models;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name="Chatrooms")
public class Chatroom {
    @Id
    @Column(name="chatroomId")
    private String id;
    @Column(name="chatRoomName")
    private String name;
    @Column(name="chatroomDesc")
    private String description;

    @ManyToMany(fetch=FetchType.EAGER,cascade = CascadeType.ALL)
    @JoinTable(
            name = "users_chatrooms",
            joinColumns = @JoinColumn(name = "chatroomId"),
            inverseJoinColumns = @JoinColumn(name = "userid"))
    private List<User> users;

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

请注意,以上内容仅为翻译结果,可能需要根据你的实际需求进行调整。

英文:

I am creating an app where there are two entities

  1. users

  2. chatrooms

There is a many to many relationship between users and chatrooms.
I created a many to many relationship with a join table named users_chatrooms. The values are getting populated in the join table correctly when I wrote code for a user to join a chatroom.

My issue is that, I need an endpoint that can fetch all the users of a given chatroom. For this, I need the table created by the join (users_chatrooms) as part of Jpa. How to accomplish this in JPA ?

User class

package com.example.chat.models;
import javax.persistence.*;
import java.util.List;
@Entity
@Table(name=&quot;USERS&quot;)
public class User {
@Id
@Column(name=&quot;userid&quot;)
private String username;
@Column(name=&quot;pass&quot;)
private String password;
@ManyToMany(mappedBy = &quot;users&quot;,fetch=FetchType.EAGER,cascade = CascadeType.ALL)
List&lt;Chatroom&gt;rooms;
public List&lt;Chatroom&gt; getRooms() {
return rooms;
}
public void setRooms(List&lt;Chatroom&gt; rooms) {
this.rooms = rooms;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

Chatroom class

package com.example.chat.models;
import javax.persistence.*;
import java.util.List;
@Entity
@Table(name=&quot;Chatrooms&quot;)
public class Chatroom {
@Id
@Column(name=&quot;chatroomId&quot;)
private String id;
@Column(name=&quot;chatRoomName&quot;)
private String name;
@Column(name=&quot;chatroomDesc&quot;)
private String description;
@ManyToMany(fetch=FetchType.EAGER,cascade = CascadeType.ALL)
@JoinTable(
name = &quot;users_chatrooms&quot;,
joinColumns = @JoinColumn(name = &quot;chatroomId&quot;),
inverseJoinColumns = @JoinColumn(name = &quot;userid&quot;))
private List&lt;User&gt;users;
public List&lt;User&gt; getUsers() {
return users;
}
public void setUsers(List&lt;User&gt; users) {
this.users = users;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

答案1

得分: 1

你可以简单地使用 @JoinColumn 来连接这两个实体。

@Entity
@Table(name="Chatrooms")
public class Chatroom {
    @Id
    @Column(name="chatroomId")
    private String id;
    @Column(name="chatRoomName")
    private String name;
    @Column(name="chatroomDesc")
    private String description;

    @ManyToMany(fetch=FetchType.EAGER,cascade = CascadeType.ALL)
    @JoinColumn(name = "userid", referencedColumnName = "chatroomId")
    private List<User> users;
 
    // getters and setters   
}
英文:

You can simply join the two entities using @JoinColumn

@Entity
@Table(name=&quot;Chatrooms&quot;)
public class Chatroom {
@Id
@Column(name=&quot;chatroomId&quot;)
private String id;
@Column(name=&quot;chatRoomName&quot;)
private String name;
@Column(name=&quot;chatroomDesc&quot;)
private String description;
@ManyToMany(fetch=FetchType.EAGER,cascade = CascadeType.ALL)
@JoinColumn(name = &quot;userid&quot;, referencedColumnName = &quot;chatroomId&quot;)
private List&lt;User&gt;users;
// getters and setters   
}

huangapple
  • 本文由 发表于 2020年8月20日 20:02:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63504658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定