英文:
How to access table created by join in JPA
问题
我正在创建一个应用程序,其中有两个实体:
-
用户
-
聊天室
用户和聊天室之间存在多对多的关系。
我使用一个名为"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
-
users
-
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="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;
}
}
Chatroom class
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;
}
}
答案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="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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论