Spring 一对一关系
Spring one to one relationship
我有一个很简单的问题。不知道是我没看懂还是不知道怎么做。
想了好久怎么在hibernate中一对一spring。我可以从两个方向到达 table。
假设我有 Hotel 和 hotelDetails tables。
正在为我创建一个密钥,以便我可以从酒店访问 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;
}
酒店评分table。
问题是,当我试图让 getter 和 setter 做旅馆时。我得到:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: com.flightradar.flightradar.model.hotel.Hotel, at table: hotel_rating, for columns: [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;
}
public Integer getVotesNumber() {
return votesNumber;
}
public void setVotesNumber(Integer votesNumber) {
this.votesNumber = votesNumber;
}
public Double getAverage() {
return average;
}
public void setAverage(Double average) {
this.average = average;
}
}
所以,伙计们帮助我了解从 HotelRating Table 到达酒店 table 的最简单方法。
例如,我有 HotelRating 列表,我必须从 Hotel table 获取每个 hotelRanking 对象。
class HotelRating
中的 "mappedBy" 值必须提供
The field that owns the relationship.
在您的示例中,值为 "hotel_rating"
@OneToOne(mappedBy = "hotel_rating")
Hotel hotel;
但是classHotel
中没有这个字段。相应的字段可能是 "hotelRating".
我有一个很简单的问题。不知道是我没看懂还是不知道怎么做。 想了好久怎么在hibernate中一对一spring。我可以从两个方向到达 table。 假设我有 Hotel 和 hotelDetails tables。 正在为我创建一个密钥,以便我可以从酒店访问 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;
}
酒店评分table。
问题是,当我试图让 getter 和 setter 做旅馆时。我得到:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: com.flightradar.flightradar.model.hotel.Hotel, at table: hotel_rating, for columns: [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;
}
public Integer getVotesNumber() {
return votesNumber;
}
public void setVotesNumber(Integer votesNumber) {
this.votesNumber = votesNumber;
}
public Double getAverage() {
return average;
}
public void setAverage(Double average) {
this.average = average;
}
}
所以,伙计们帮助我了解从 HotelRating Table 到达酒店 table 的最简单方法。 例如,我有 HotelRating 列表,我必须从 Hotel table 获取每个 hotelRanking 对象。
class HotelRating
中的 "mappedBy" 值必须提供
The field that owns the relationship.
在您的示例中,值为 "hotel_rating"
@OneToOne(mappedBy = "hotel_rating")
Hotel hotel;
但是classHotel
中没有这个字段。相应的字段可能是 "hotelRating".