使用 Crud 存储库保存带有嵌套对象的实体
Saving an entity with a nested object with Crud repo
如何保存嵌套了另一个实体的实体?
回购
public interface ReservationRepository extends JpaRepository<Reservation, Integer> {
Optional<Reservation> findReservationBySection_SectionId(int id);
}
服务
@Service
public class ReservationService {
private final Logger logger = LoggerFactory.getLogger(getClass().getName());
@Autowired
private ReservationRepository reservationRepository;
@Autowired
private SectionRepository sectionRepository;
@Autowired
private UserRepository userRepository;
public Reservation addReservation(Reservation reservation) {
Reservation newReservation = new Reservation();
newReservation.setCount(reservation.getCount());
newReservation.setCustom_section_name(reservation.getCustom_section_name());
newReservation.setFrom_time(reservation.getFrom_time());
newReservation.setTo_time(reservation.getTo_time());
newReservation.setSection(sectionRepository.getOne(reservation.getSection().getSectionId()));
newReservation.setUser(userRepository.getOne(reservation.getUser().getUserId()));
logger.info(newReservation.toString());
return reservationRepository.save(newReservation);
}
}
用户实体
User
@Id
@GeneratedValue
@Column(name = "user_id")
private int userId;
@Column(name = "name")
private String name;
@Column(name="email", unique = true)
private String email;
@JsonIgnore
@Column(name = "password")
private String password;
@Column(name = "phone")
private String phone;
@Column(name = "expiration_date")
private Timestamp expirationDate;
部门实体
Section
@Id
@GeneratedValue
@Column(name = "section_id")
private int sectionId;
@Column(name = "section_name")
private String sectionName;
@OneToOne
@JoinColumn(name="room_id")
private Section roomId;
预订实体
@Id
@GeneratedValue
@Column(name = "reservation_id")
private int reservationId;
@ManyToOne
@MapsId("section_id")
@JoinColumn(name = "section_id")
private Section section;
@ManyToOne
@MapsId("user_id")
@JoinColumn(name = "user_id")
private User user;
@Column(name = "count")
private int count;
@Column(name = "custom_section_name")
private String custom_section_name;
@Column(name = "from_time")
private Timestamp from_time;
@Column(name = "to_time", unique = true)
private Timestamp to_time;
预期结果:
想要在数据库中保存包含部分和用户的预订。像这样:
实际结果:
已创建预订但缺少 section_id 和 user_id
SQL 数据库 table 有 section_id 和 user_id 不为空,因为它们的 table 中有主键。
如果需要,将提供更多信息。
如果 Reservation 拥有与 Section 的关系,则需要将 Cascade 添加到关系中。当您保留 Reservation 时,Hibernate 将拦截您还想保留一个 Section 并将其相应引用附加到 Reservation 实体的事实。
您可以添加
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "section_id")
private Section section;
您可以根据需要修改级联类型(例如:如果您不想级联删除操作)
如何保存嵌套了另一个实体的实体?
回购
public interface ReservationRepository extends JpaRepository<Reservation, Integer> {
Optional<Reservation> findReservationBySection_SectionId(int id);
}
服务
@Service
public class ReservationService {
private final Logger logger = LoggerFactory.getLogger(getClass().getName());
@Autowired
private ReservationRepository reservationRepository;
@Autowired
private SectionRepository sectionRepository;
@Autowired
private UserRepository userRepository;
public Reservation addReservation(Reservation reservation) {
Reservation newReservation = new Reservation();
newReservation.setCount(reservation.getCount());
newReservation.setCustom_section_name(reservation.getCustom_section_name());
newReservation.setFrom_time(reservation.getFrom_time());
newReservation.setTo_time(reservation.getTo_time());
newReservation.setSection(sectionRepository.getOne(reservation.getSection().getSectionId()));
newReservation.setUser(userRepository.getOne(reservation.getUser().getUserId()));
logger.info(newReservation.toString());
return reservationRepository.save(newReservation);
}
}
用户实体
User
@Id
@GeneratedValue
@Column(name = "user_id")
private int userId;
@Column(name = "name")
private String name;
@Column(name="email", unique = true)
private String email;
@JsonIgnore
@Column(name = "password")
private String password;
@Column(name = "phone")
private String phone;
@Column(name = "expiration_date")
private Timestamp expirationDate;
部门实体
Section
@Id
@GeneratedValue
@Column(name = "section_id")
private int sectionId;
@Column(name = "section_name")
private String sectionName;
@OneToOne
@JoinColumn(name="room_id")
private Section roomId;
预订实体
@Id
@GeneratedValue
@Column(name = "reservation_id")
private int reservationId;
@ManyToOne
@MapsId("section_id")
@JoinColumn(name = "section_id")
private Section section;
@ManyToOne
@MapsId("user_id")
@JoinColumn(name = "user_id")
private User user;
@Column(name = "count")
private int count;
@Column(name = "custom_section_name")
private String custom_section_name;
@Column(name = "from_time")
private Timestamp from_time;
@Column(name = "to_time", unique = true)
private Timestamp to_time;
预期结果:
想要在数据库中保存包含部分和用户的预订。像这样:
实际结果:
已创建预订但缺少 section_id 和 user_id
SQL 数据库 table 有 section_id 和 user_id 不为空,因为它们的 table 中有主键。 如果需要,将提供更多信息。
如果 Reservation 拥有与 Section 的关系,则需要将 Cascade 添加到关系中。当您保留 Reservation 时,Hibernate 将拦截您还想保留一个 Section 并将其相应引用附加到 Reservation 实体的事实。
您可以添加
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "section_id")
private Section section;
您可以根据需要修改级联类型(例如:如果您不想级联删除操作)