預訂系統涉及物體:用戶、預訂、課程。
問題: ReservationService 不會將預訂添加到資料庫或用戶。Reservation 預訂的自動生成的 long id = new Reservation(); 是 0 [我試過序列生成型別,沒用]。然后,reservationService.addReservation(reservation); 導致錯誤:分離的物體傳遞給持久化。關系:
用戶 -- oneToMany --> 預訂 -- oneToOne --> 課程
預訂控制器:
@Controller
public class ReservationController {
private final AppUserService appUserService;
private final ReservationService reservationService;
private final CourseService courseService;
public ReservationController(AppUserService appUserService, ReservationService reservationService, CourseService courseService) {
this.appUserService = appUserService;
this.reservationService = reservationService;
this.courseService = courseService;
}
@RequestMapping(value = "bookCourse/{courseId}")
public String bookCourse(@PathVariable("courseId") long id, Principal principal) {
AppUser user = appUserService.getAppUser(principal.getName());
Course course = courseService.getCourse(id);
Reservation reservation = new Reservation();
reservation.setAppUser(user);
reservation.setCourse(course);
reservationService.addReservation(reservation);
user.getReservations().add(reservation); //this line causes the error:
//org.hibernate.PersistentObjectException: detached entity passed to persist: application.domain.Course
appUserService.editAppUser(user);
return "redirect:/reservations.html";
}
預訂服務實施:
@Service("reservationService")
@Transactional
public class ReservationServiceImpl implements ReservationService {
private final ReservationRepository reservationRepository;
@Autowired
public ReservationServiceImpl(ReservationRepository reservationRepository) {
this.reservationRepository = reservationRepository;
}
@Transactional
public void addReservation(Reservation reservation) {
reservationRepository.save(reservation);
};
用戶.java:
@Entity
@Table(name="appuser")
public class AppUser {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
long id;
@OneToMany(mappedBy="appUser", cascade = CascadeType.ALL)
private Set<Reservation> reservations;
//getters and setters
}
預訂.java:
@Entity
@Table(name="reservation")
public class Reservation {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@OneToOne(cascade = CascadeType.ALL)
@JsonManagedReference
private Course course;
@ManyToOne(fetch = FetchType.EAGER)//(mappedBy="reservation")
private AppUser appUser;
//getters and setters
課程.java:
@Entity
@Table(name="course")
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotEmpty
private String name;
@NotEmpty
private String price;
@JsonBackReference
@OneToOne(mappedBy="course")
private Reservation reservation;
//getters and setters
In the database, an appuser_reservation junction table has been created to link the ids of a user and a reservation.
In the database, reservation has appuser_id and course_id "automatically" created, and of bigint type.
The ReservationController method is referenced by clicking in course list html:
<a href="bookCourse/${course.id}">Book!</a>
[course already is in the Database]
Framework is Spring, database is PostgreSQL.
If there are more information needed, please let me know!
Since I don't know if the issue is that new Reservation() creates a reservation with a null id, so then it can't be properly saved to DB by the service, or there is some other, more general issue, I don't know if the title of this question is appropriate.
我知道錯誤本身已經被討論過,但我現在好幾天都無法解決這個問題,我將非常感謝所有的洞察力。也許我在理解關系或 new Entity() 的作業方式方面犯了一個錯誤。
在 bookCourse{id} 中創建一個新的 Reservation() 有什么問題;并將其寫入資料庫?
uj5u.com熱心網友回復:
好的,我們親自見面并解決了問題。
與 JPA 和 Hibernate 的關系是錯誤的。
您需要將 Reservation 中的關系更改為:
@Entity
@Table(name="reservation")
public class Reservation {
...
@ManyToOne
private Course course;
...
}
你在這里有一個現場繪圖,所以你可以記住什么是什么以及關系是什么。

預訂預訂的自動生成的長 ID = new Reservation(); 是 0
首先,您需要將創建的物體添加到資料庫并獲取它以獲取自動生成的 id,如下所示:
Reservation reservation = new Reservation();
// fill the reservation
// I assume there is repository.save(reservation);
Reservation reservationWithID = reservationService.add(reservation);
// assign reservationWithID to User Reservation List to create connection
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/316325.html
標籤:春天 PostgreSQL 休眠
