我有四个表,如下图所示。
餐厅和类别之间的关系是多对多的。为此,使用了一个名为restaurant_category的新表。 我将“餐厅”和“类别”之间的关系设置如下。
RestaurantEntity.java
================
package com.upgrad.FoodOrderingApp.service.entity;
import javax.persistence.*;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Entity
@Table(name = "restaurant")
@NamedQueries(
{ @NamedQuery(name = "getAllRestaurants", query = "select res from RestaurantEntity res")
}
)
public class RestaurantEntity implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "UUID")
@Size(max = 200)
private UUID uuid;
@Column(name = "restaurant_name")
@Size(max = 50)
private String restaurantName;
@Column(name = "photo_url")
@Size(max = 225)
private String photoURL;
@Column(name = "customer_rating")
private BigDecimal customeRating;
@Column(name = "average_price_for_two")
private Integer avgPriceForTwo;
public List<CategoryEntity> getCategories() {
return categories;
}
public void setCategories(List<CategoryEntity> categories) {
this.categories = categories;
}
@Column(name = "number_of_customers_rated")
private Integer numbrOfCustomersRated;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "address_id")
private AddressEntity addressEntity;
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="restaurant_category", joinColumns={@JoinColumn(referencedColumnName="ID")}
, inverseJoinColumns={@JoinColumn(referencedColumnName="ID")})
private List<CategoryEntity> categories = new ArrayList<>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public UUID getUuid() {
return uuid;
}
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
public String getRestaurantName() {
return restaurantName;
}
public void setRestaurantName(String restaurantName) {
this.restaurantName = restaurantName;
}
public String getPhotoURL() {
return photoURL;
}
public void setPhotoURL(String photoURL) {
this.photoURL = photoURL;
}
public BigDecimal getCustomeRating() {
return customeRating;
}
public void setCustomeRating(BigDecimal customeRating) {
this.customeRating = customeRating;
}
public Integer getAvgPriceForTwo() {
return avgPriceForTwo;
}
public void setAvgPriceForTwo(Integer avgPriceForTwo) {
this.avgPriceForTwo = avgPriceForTwo;
}
public Integer getNumbrOfCustomersRated() {
return numbrOfCustomersRated;
}
public void setNumbrOfCustomersRated(Integer numbrOfCustomersRated) {
this.numbrOfCustomersRated = numbrOfCustomersRated;
}
public AddressEntity getAddressEntity() {
return addressEntity;
}
public void setAddressEntity(AddressEntity addressEntity) {
this.addressEntity = addressEntity;
}
}
CategoryEntity.java
===============
package com.upgrad.FoodOrderingApp.service.entity;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import javax.persistence.*;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Entity
@Table(name = "category")
public class CategoryEntity implements Serializable {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
@Column(name = "UUID")
@Size(max = 200)
public UUID uuid;
public List<RestaurantEntity> getRestaurants() {
return restaurants;
}
public void setRestaurants(List<RestaurantEntity> restaurants) {
this.restaurants = restaurants;
}
@Column(name = "CATEGORY_NAME")
@Size(max = 255)
public String category_name;
@ManyToMany(mappedBy="categories")
private List<RestaurantEntity> restaurants = new ArrayList<>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public UUID getUuid() {
return uuid;
}
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
public String getCategory_name() {
return category_name;
}
public void setCategory_name(String category_name) {
this.category_name = category_name;
}
@Override
public boolean equals(Object obj) {
return new EqualsBuilder().append(this, obj).isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder().append(this).hashCode();
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
}
我还如下所示在RestaurantEntity中使用了namedQuery,
@NamedQueries(
{ @NamedQuery(name = "getAllRestaurants", query = "select res from RestaurantEntity res")
}
)
此namedQuery返回Restaurants所需的所有数据及其相关对象-address。但是它不会返回相关类别的数据。在设置餐厅和类别之间的关系时我做错了吗?请引导我。
所以我期望命名查询也应该返回相关类别的数据。
注意:在终端中,我遇到以下错误,
org.postgresql.util.PSQLException:错误:列 category0_.categories_id不存在
答案 0 :(得分:0)
您必须通过添加连接列名称来更正映射:
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="restaurant_category", joinColumns={@JoinColumn(name = "RESTAURANT_ID", referencedColumnName="ID")}
, inverseJoinColumns={@JoinColumn(name = "CATEGORY_ID", referencedColumnName="ID")})
private List<CategoryEntity> categories = new ArrayList<>();
答案 1 :(得分:0)
请如下更改您的JoinTable
列名称。
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="restaurant_category", joinColumns={@JoinColumn(name="restaurant_id")}
, inverseJoinColumns={@JoinColumn(name="category_id")})
private List<CategoryEntity> categories = new ArrayList<>();