我有一個非常簡單的問題。我不知道是我沒有理解什么,還是我不知道該怎么做。我很長一段時間想知道hibernate spring中如何一對一。我可以從兩個方向到達桌子。假設我有 Hotel 和 hotelDetails 表。正在為我創建一個密鑰,以便我可以從酒店訪問 hotelRating,但我不能走其他路。有時這對我很重要。我將使用示例代碼。public class Hotel { private Long id; private String name; private String currency; private String image; private HotelRating hotelRating; @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.AUTO) public Long getId() { return id; } public void setId(Long id) { this.id = id; }. . @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "hotel_rating_id", referencedColumnName = "id") public HotelRating getHotelRating() { return hotelRating; } public void setHotelRating(HotelRating hotelRating) { this.hotelRating = hotelRating; }酒店評級表。問題是,當我試圖讓 getter 和 setter 做酒店時。我得到:org.springframework.beans.factory.BeanCreationException:創建在類路徑資源[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]中定義的名為“entityManagerFactory”的bean時出錯:調用init方法失?。磺短桩惓J?javax.persistence.PersistenceException: [PersistenceUnit: default] 無法構建 Hibernate SessionFactory;嵌套異常是 org.hibernate.MappingException:無法確定類型:com.flightradar.flightradar.model.hotel.Hotel,表:hotel_ rating,列:[org.hibernate.mapping.Column(hotel)]@Entity@Table(name = "hotel_rating")public class HotelRating { private long id; private Integer votesNumber; @Min(1) @Max(5) private Double average; @OneToOne(mappedBy = "hotel_rating") Hotel hotel; @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.AUTO) public long getId() { return id; } public void setId(long id) { this.id = id; }所以,大家幫助我了解從 HotelRating Table 到達 Hotel table 的最簡單的可能性。例如,我有 HotelRating 列表,我必須從 Hotel 表中獲取每個 hotelRanking 對象。
1 回答

弒天下
TA貢獻1818條經驗 獲得超8個贊
類中的“mappedBy”值HotelRating
必須提供
擁有關系的字段。
在您的示例中,該值為“hotel_ rating”
@OneToOne(mappedBy = "hotel_rating") Hotel hotel;
但課堂上沒有這個字段Hotel
。相應的字段可能是“hotelRating”。
添加回答
舉報
0/150
提交
取消