如何解决spring boot REST Api中的一对多关系
我正在使用 Spring Boot 创建一个 REST API。在这个 API 中,我在 签入 和 访客 之间建立了一对多的关系。我创建了一个用于签入的控制器并使用 spring JPA 的保存功能。 save 方法同时更新checkin 和guest 表,但对于guest 表,guest 表中的签入外键 没有被添加,而是显示为空。请有人帮助我。我需要同时创建访客和签到。
签到模型
@Data
@Entity
public class Checkin {
@Id
private Long id;
private Integer no_of_guests;
@OneToMany(mappedBy = "checkin",cascade = CascadeType.ALL)
private List<Guest> guests;
}
访客模型
@Data
@Entity
public class Guest {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long guest_id;
private String name;
private String mobile_no;
private String address;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "guest_checkin_id",nullable = false )
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Checkin checkin;
}
签到控制器
@RestController
@RequestMapping("/checkin")
public class CheckinController {
private final CheckinRepository checkinRepo;
private final GuestRepository guestRepo;
public CheckinController(CheckinRepository checkinRepo,GuestRepository guestRepo){
this.checkinRepo = checkinRepo;
this.guestRepo = guestRepo;
}
@PostMapping("/add")
ResponseEntity<Object> roomCheckin(@RequestBody Checkin checkin){
if(checkinRepo.save(checkin) != null){
return ResponseEntity.accepted().body("Checkin Successfull");
}
return ResponseEntity.unprocessableEntity().body("Failed to Checkin");
}
}
解决方法
使用实体类作为视图模型类可能有点棘手,尤其是在 Checkin
和 Guest
之间存在双向一对多关系的情况下。
让我们首先验证实体类和存储库是否如所描述的那样工作。为了进行测试运行,我必须为类 @GeneratedValue
中的 id 字段添加 Checkin
。
其他变化:
- 使用
Set
而不是List
。见https://stackoverflow.com/a/6563037/14072498 - 对于实体类使用
@Getter
和@Setter
而不是@Data
关系
我在下面添加了一个测试类 CheckinRepositoryTest
以验证代码。
如前所述,使用实体类作为视图模型类可能很棘手,因此下一步将引入两个新的视图模型类:CheckinVM
和 GuestVM
,以及一个新的服务类 {{ 1}} 将负责保存 GuestService
和 Checkin
实例。代码如下所示。
请注意,我只为 Guest
类添加了骨架,我将它留给您来实现。 GuestService
中的代码指示如何实现它。
有了视图模型类,CheckinRepositoryTest
应该被删除。
所有部分都启动并运行后,您应该向新的视图模型类添加验证,以及一些验证行为的集成测试。如果您遵循 TDD,您将在途中添加测试。
您还应该查看您的控制器方法,并针对成功和失败情况使用适当的状态代码。
嘉宾班
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
GuestVM 类
package no.mycompany.myapp.misc;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
@Getter
@Setter
@Entity
public class Guest {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long guest_id;
private String name;
private String mobile_no;
private String address;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "guest_checkin_id",nullable = false)
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Checkin checkin;
}
签到类
package no.mycompany.myapp.misc;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
public class GuestVM {
private long id;
private String name;
private String mobile_no;
private String address;
public GuestVM(Guest guest) {
this.id = guest.getGuest_id();
this.name = guest.getName();
this.mobile_no = guest.getMobile_no();
this.address = guest.getAddress();
}
}
CheckinVM 类
package no.mycompany.myapp.misc;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Getter
@Setter
@Entity
public class Checkin {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Integer no_of_guests;
@OneToMany(mappedBy = "checkin",cascade = CascadeType.ALL)
private Set<Guest> guests = new HashSet<>();
}
签入仓库
package no.mycompany.myapp.misc;
import lombok.Getter;
import lombok.Setter;
import lombok.NoArgsConstructor;
import java.util.List;
import java.util.stream.Collectors;
@Getter
@Setter
@NoArgsConstructor
public class CheckinVM {
private long id;
private List<GuestVM> guests;
public CheckinVM(Checkin checkin) {
this.id = checkin.getId();
this.guests = checkin.getGuests().stream().map(GuestVM::new).collect(Collectors.toList());
}
}
CheckinRepositoryTest
package no.mycompany.myapp.misc;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CheckinRepository extends JpaRepository<Checkin,Long> { }
CheckinService 类:这个交给你来实现
package no.mycompany.myapp.misc;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@DataJpaTest
public class CheckinRepositoryTest {
@Autowired
TestEntityManager testEntityManager;
@Autowired
CheckinRepository checkinRepository;
@Test
public void test() {
// create instances
var checkinInDb = new Checkin();
var guestInDb = new Guest();
// add relations
guestInDb.setCheckin(checkinInDb);
checkinInDb.getGuests().add(guestInDb);
// save check-in
checkinRepository.save(checkinInDb);
// verify that check-in has one guest
var checkin = testEntityManager.find(Checkin.class,checkinInDb.getId());
assertThat(checkin.getGuests().size()).isEqualTo(1);
// verify that guest is connected to a check-in
var guest = testEntityManager.find(Guest.class,guestInDb.getGuest_id());
assertThat(guest.getCheckin()).isNotNull();
}
}
控制器类
package no.mycompany.myapp.misc;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class CheckinService {
private final CheckinRepository checkinRepository;
public CheckinVM saveCheckin(CheckinVM checkin) {
return null; // TODO: implement this
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。