Spring JPA:如何通过@ManytoMany关系插入数据以联接许多表

时间:2018-06-25 04:37:25

标签: spring jpa spring-data-jpa many-to-many

我开始学习Spring Java Framework。我创建了一些Enity来加入2 Model,例如我的数据库。现在,我想插入JpaRepository的Join Table。我该怎么办? 这是我的代码(如果有问题请修复,帮我解决)

使用模型 Users_RoomId 定义组合主键

@Embeddable
public class Users_RoomId implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "ID_room", nullable = false)
private String idRoom;
 @Column(name = "user_id", nullable = false)
 private int idUser;

}

模型 Users_Room 可以加入2个模型用户和会议室

@Entity
@Table(name ="bookroom")
public class Users_Room {
 @EmbeddedId
 private Users_RoomId usersroomId;

 @ManyToOne
 @MapsId("idRoom")
 private Room room;

 @ManyToOne
 @MapsId("idUser")
 private Users users;

 @Column(name = "Bookday")
 private String bookday;

模型用户和房间我使用了注释@OneToMany

模型用户

@Entity
@Table(name = "users")
public class Users  implements Serializable{
 private static final long serialVersionUID = 1L;
 @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "user_id", nullable = false)
    private int id;


    @Column(name = "name", nullable = false)
    private String name;


    @Column(name = "email")
    private String email;

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

    @Column(name = "role")
    private int role;

    @OneToMany(mappedBy = "users")
    private List<Users_Room> user;

样板房

@Entity
@Table(name ="room")
public class Room implements Serializable {
 private static final long serialVersionUID = 1L;
 @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID_room", nullable = false)
    private String id;


    @Column(name = "name_room", nullable = false)
    private String name;


    @Column(name = "Description")
    private String describe;

    @ManyToOne
    @JoinColumn(name = "ID_status")
    private Status status;

    @Column(name = "room_image")
    private String image;


    public Room() {
        super();
    }

    @ManyToOne
    @JoinColumn(name = "ID_kind")
    private KindRoom kind;


    @OneToMany(mappedBy = "room")
    private List<Users_Room> rooms;

这是我的数据库

enter image description here

因此,我不知道如何使用JPA存储库的iduser,idroom和bookday插入新的书房。必须在JPARepository中编写Query或只需要使用save()方法来插入数据

谢谢大家

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题并使用以下代码解决了。我使用方法 save() 插入数据。以下代码是'RoomService.java'中的'createRoom'方法。

RoomService.java

private final RoomRepository roomRepository;
private final UserRoomRepository userRoomRepository;
private final UserRepository userRepository;

public RoomService(RoomRepository roomRepository, UserRoomRepository userRoomRepository, UserRepository userRepository) {
    this.roomRepository = roomRepository;
    this.userRoomRepository = userRoomRepository;
    this.userRepository = userRepository;
}

@Transactional
public RoomDto createRoom(Long userId, Long chattingUserId) {
    Room room = roomRepository.save(new Room());

    room.addUserRoom(userRepository.findById(userId).orElseThrow(()->new NoSuchElementException("No User")));
    room.addUserRoom(userRepository.findById(chattingUserId).orElseThrow(()->new NoSuchElementException("No User")));
    userRoomRepository.save(new UserRoom(userRepository.findById(userId).orElseThrow(()->new NoSuchElementException("No User")),room));
    userRoomRepository.save(new UserRoom(userRepository.findById(chattingUserId).orElseThrow(()->new NoSuchElementException("No User")),room));

    RoomDto roomDto = RoomDto.of(room);
    return roomDto;
}