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

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

How to access table created by join in JPA

问题

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

  1. 用户

  2. 聊天室

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

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

用户类

  1. package com.example.chat.models;
  2. import javax.persistence.*;
  3. import java.util.List;
  4. @Entity
  5. @Table(name="USERS")
  6. public class User {
  7. @Id
  8. @Column(name="userid")
  9. private String username;
  10. @Column(name="pass")
  11. private String password;
  12. @ManyToMany(mappedBy = "users", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  13. List<Chatroom> rooms;
  14. public List<Chatroom> getRooms() {
  15. return rooms;
  16. }
  17. public void setRooms(List<Chatroom> rooms) {
  18. this.rooms = rooms;
  19. }
  20. public void setUsername(String username) {
  21. this.username = username;
  22. }
  23. public String getUsername() {
  24. return username;
  25. }
  26. public String getPassword() {
  27. return password;
  28. }
  29. public void setPassword(String password) {
  30. this.password = password;
  31. }
  32. }

聊天室类

  1. package com.example.chat.models;
  2. import javax.persistence.*;
  3. import java.util.List;
  4. @Entity
  5. @Table(name="Chatrooms")
  6. public class Chatroom {
  7. @Id
  8. @Column(name="chatroomId")
  9. private String id;
  10. @Column(name="chatRoomName")
  11. private String name;
  12. @Column(name="chatroomDesc")
  13. private String description;
  14. @ManyToMany(fetch=FetchType.EAGER,cascade = CascadeType.ALL)
  15. @JoinTable(
  16. name = "users_chatrooms",
  17. joinColumns = @JoinColumn(name = "chatroomId"),
  18. inverseJoinColumns = @JoinColumn(name = "userid"))
  19. private List<User> users;
  20. public List<User> getUsers() {
  21. return users;
  22. }
  23. public void setUsers(List<User> users) {
  24. this.users = users;
  25. }
  26. public String getId() {
  27. return id;
  28. }
  29. public void setId(String id) {
  30. this.id = id;
  31. }
  32. public String getName() {
  33. return name;
  34. }
  35. public void setName(String name) {
  36. this.name = name;
  37. }
  38. public String getDescription() {
  39. return description;
  40. }
  41. public void setDescription(String description) {
  42. this.description = description;
  43. }
  44. }

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

英文:

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

  1. package com.example.chat.models;
  2. import javax.persistence.*;
  3. import java.util.List;
  4. @Entity
  5. @Table(name=&quot;USERS&quot;)
  6. public class User {
  7. @Id
  8. @Column(name=&quot;userid&quot;)
  9. private String username;
  10. @Column(name=&quot;pass&quot;)
  11. private String password;
  12. @ManyToMany(mappedBy = &quot;users&quot;,fetch=FetchType.EAGER,cascade = CascadeType.ALL)
  13. List&lt;Chatroom&gt;rooms;
  14. public List&lt;Chatroom&gt; getRooms() {
  15. return rooms;
  16. }
  17. public void setRooms(List&lt;Chatroom&gt; rooms) {
  18. this.rooms = rooms;
  19. }
  20. public void setUsername(String username) {
  21. this.username = username;
  22. }
  23. public String getUsername() {
  24. return username;
  25. }
  26. public String getPassword() {
  27. return password;
  28. }
  29. public void setPassword(String password) {
  30. this.password = password;
  31. }
  32. }

Chatroom class

  1. package com.example.chat.models;
  2. import javax.persistence.*;
  3. import java.util.List;
  4. @Entity
  5. @Table(name=&quot;Chatrooms&quot;)
  6. public class Chatroom {
  7. @Id
  8. @Column(name=&quot;chatroomId&quot;)
  9. private String id;
  10. @Column(name=&quot;chatRoomName&quot;)
  11. private String name;
  12. @Column(name=&quot;chatroomDesc&quot;)
  13. private String description;
  14. @ManyToMany(fetch=FetchType.EAGER,cascade = CascadeType.ALL)
  15. @JoinTable(
  16. name = &quot;users_chatrooms&quot;,
  17. joinColumns = @JoinColumn(name = &quot;chatroomId&quot;),
  18. inverseJoinColumns = @JoinColumn(name = &quot;userid&quot;))
  19. private List&lt;User&gt;users;
  20. public List&lt;User&gt; getUsers() {
  21. return users;
  22. }
  23. public void setUsers(List&lt;User&gt; users) {
  24. this.users = users;
  25. }
  26. public String getId() {
  27. return id;
  28. }
  29. public void setId(String id) {
  30. this.id = id;
  31. }
  32. public String getName() {
  33. return name;
  34. }
  35. public void setName(String name) {
  36. this.name = name;
  37. }
  38. public String getDescription() {
  39. return description;
  40. }
  41. public void setDescription(String description) {
  42. this.description = description;
  43. }
  44. }

答案1

得分: 1

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

  1. @Entity
  2. @Table(name="Chatrooms")
  3. public class Chatroom {
  4. @Id
  5. @Column(name="chatroomId")
  6. private String id;
  7. @Column(name="chatRoomName")
  8. private String name;
  9. @Column(name="chatroomDesc")
  10. private String description;
  11. @ManyToMany(fetch=FetchType.EAGER,cascade = CascadeType.ALL)
  12. @JoinColumn(name = "userid", referencedColumnName = "chatroomId")
  13. private List<User> users;
  14. // getters and setters
  15. }
英文:

You can simply join the two entities using @JoinColumn

  1. @Entity
  2. @Table(name=&quot;Chatrooms&quot;)
  3. public class Chatroom {
  4. @Id
  5. @Column(name=&quot;chatroomId&quot;)
  6. private String id;
  7. @Column(name=&quot;chatRoomName&quot;)
  8. private String name;
  9. @Column(name=&quot;chatroomDesc&quot;)
  10. private String description;
  11. @ManyToMany(fetch=FetchType.EAGER,cascade = CascadeType.ALL)
  12. @JoinColumn(name = &quot;userid&quot;, referencedColumnName = &quot;chatroomId&quot;)
  13. private List&lt;User&gt;users;
  14. // getters and setters
  15. }

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:

确定