RedDevil1007
RedDevil1007

Reputation: 21

How to access table created by join in JPA

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="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;
    }

}

Upvotes: 0

Views: 152

Answers (1)

SSK
SSK

Reputation: 3766

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   
}

Upvotes: 1

Related Questions