gpt4 book ai didi

java - Spring 一对一关系

转载 作者:行者123 更新时间:2023-12-02 09:28:46 27 4
gpt4 key购买 nike

我有一个非常简单的问题。我不知道是我没有理解什么,还是我不知道该怎么做。我很长一段时间想知道hibernate spring中如何一对一。我可以从两个方向到达 table 。假设我有 Hotel 和 hotelDetails 表。正在为我创建一个 key ,以便我可以从酒店访问 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 做 hotel 时。我得到:

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 到达 Hotel table 的最简单的可能性。例如,我有 HotelRating 列表,我必须从 Hotel 表中获取每个 hotelRanking 对象。

最佳答案

HotelRating 类中的“mappedBy”值必须提供

The field that owns the relationship.

在您的示例中,值为“hotel_ rating”

 @OneToOne(mappedBy = "hotel_rating")
Hotel hotel;

但是Hotel类中没有这样的字段。相应的字段可能是“hotelRating”。

关于java - Spring 一对一关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58138761/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com