英文:
Issues in one-to-many and many-to-one mapping for three entity classes
问题
我有三个实体类,分别是Country
(国家)、State
(州/省)和City
(城市)。我现在正在尝试显示属于特定州/省的城市以及属于特定国家的州/省,但是我遇到了以下错误:
org.springframework.dao.DataAccessResourceFailureException:
无法创建 JPA EntityManager;
嵌套异常是 org.hibernate.AnnotationException:
在 com.region.model.State.cities 引用中的 @OneToOne 或 @ManyToOne 引用了未知实体:java.util.List。
请帮我解决这个问题,以便进行三个表之间的一对多和多对一映射。
以下是相关的代码部分:
// Country.java
@Entity
@Table(name = "country")
public class Country {
// ...(属性和注解)
@OneToMany(targetEntity = State.class, mappedBy = "country", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonBackReference
private List<State> states;
// ...
}
// State.java
@Entity
@Table(name = "state")
public class State {
// ...(属性和注解)
@ManyToOne()
@JoinColumn(name = "country_id", referencedColumnName = "country_id", insertable = false, updatable = false)
@JsonManagedReference
private Country country;
@OneToMany(targetEntity = City.class, mappedBy = "state", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<City> cities;
// ...
}
// City.java
@Entity
@Table(name = "city")
public class City {
// ...(属性和注解)
@ManyToOne(targetEntity = State.class)
@JoinColumn(name = "state_id", referencedColumnName = "sk_state_id", insertable = false, updatable = false)
private State state;
private Country country;
// ...
}
请注意,上述代码片段中的注释和引用部分已被我翻译为中文。如果您需要进一步的帮助,请随时告诉我。
英文:
I have three entity classes Country
, State
, and City
. Now I am trying to display the cities that come under a particular state and states that come under a particular country. but I am facing an error like
org.springframework.dao.DataAccessResourceFailureException:
Could not create JPA EntityManager;
nested exception is org.hibernate.AnnotationException:
@OneToOne or @ManyToOne on com.region.model.State.cities reference
an unknown entity: java.util.List.
Please help me out in one-to-many and many-to-one mapping for the three tables.
package com.region.model;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
import javax.persistence.Transient;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
@Entity
@Table(name = "country")
public class Country {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column
private Integer country_id;
@Column
private String country_name;
// @Column
// private String status="active";
@Transient
private String statusCode;
@Transient
private String statusmessage;
@OneToMany(targetEntity=State.class, mappedBy="country",cascade=CascadeType.ALL, fetch = FetchType.LAZY)
@JsonBackReference
private List<State> states;
public Country() {
}
public Integer getCountry_id() {
return country_id;
}
public void setCountry_id(Integer country_id) {
this.country_id = country_id;
}
public String getCountry_name() {
return country_name;
}
public void setCountry_name(String country_name) {
this.country_name = country_name;
}
// public String getStatus() {
// return status;
// }
// public void setStatus(String status) {
// this.status = status;
// }
public String getStatusCode() {
return statusCode;
}
public void setStatusCode(String statusCode) {
this.statusCode = statusCode;
}
public String getStatusmessage() {
return statusmessage;
}
public void setStatusmessage(String statusmessage) {
this.statusmessage = statusmessage;
}
public List<State> getStates() {
return states;
}
public void setStates(List<State> states) {
this.states = states;
}
}
package com.region.model;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
@Entity
@Table(name = "state")
public class State {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column
private Integer sk_state_id;
@Column
private String state_name;
@Column
private String country_id;
// @Column
// private String status="active";
@Transient
private String statusCode;
@Transient String statusMessage;
@ManyToOne()
@JoinColumn(name="country_id", referencedColumnName = "country_id", insertable = false, updatable = false)
@JsonManagedReference
@OneToMany(targetEntity=City.class, mappedBy="state",cascade=CascadeType.ALL, fetch = FetchType.LAZY)
private List<City>cities;
private Country country;
public State() {
}
public Integer getSk_state_id() {
return sk_state_id;
}
public void setSk_state_id(Integer sk_state_id) {
this.sk_state_id = sk_state_id;
}
public String getState_name() {
return state_name;
}
public void setState_name(String state_name) {
this.state_name = state_name;
}
public String getCountry_id() {
return country_id;
}
public void setCountry_id(String country_id) {
this.country_id = country_id;
}
// public String getStatus() {
// return status;
// }
// public void setStatus(String status) {
// this.status = status;
// }
public String getStatusCode() {
return statusCode;
}
public void setStatusCode(String statusCode) {
this.statusCode = statusCode;
}
public String getStatusMessage() {
return statusMessage;
}
public void setStatusMessage(String statusMessage) {
this.statusMessage = statusMessage;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public List<City> getCities() {
return cities;
}
public void setCities(List<City> cities) {
this.cities = cities;
}
}
package com.region.model;
import java.lang.annotation.Repeatable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
@Entity
@Table(name = "city")
public class City {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column
private Integer sk_city_id;
@Column
private String city_name;
@Column
private String state_id;
@Column
private String country_id;
@Transient
private String statusCode;
@Transient
String statusMessage;
@ManyToOne(targetEntity = State.class)
@JoinColumn(name="state_id", referencedColumnName ="sk_state_id", insertable = false, updatable = false)
private State state;
private Country country;
//private List<State>states;
public City() {
}
public Integer getSk_city_id() {
return sk_city_id;
}
public void setSk_city_id(Integer sk_city_id) {
this.sk_city_id = sk_city_id;
}
public String getCity_name() {
return city_name;
}
public void setCity_name(String city_name) {
this.city_name = city_name;
}
public String getState_id() {
return state_id;
}
public void setState_id(String state_id) {
this.state_id = state_id;
}
public String getCountry_id() {
return country_id;
}
public void setCountry_id(String country_id) {
this.country_id = country_id;
}
public String getStatusCode() {
return statusCode;
}
public void setStatusCode(String statusCode) {
this.statusCode = statusCode;
}
public String getStatusMessage() {
return statusMessage;
}
public void setStatusMessage(String statusMessage) {
this.statusMessage = statusMessage;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
@Override
public String toString() {
return "City [sk_city_id=" + sk_city_id + ", city_name=" + city_name + ", state_id=" + state_id
+ ", country_id=" + country_id + ", statusCode=" + statusCode + ", statusMessage=" + statusMessage
+ "]";
}
// public List<State> getStates() {
// return states;
// }
// public void setStates(List<State> states) {
// this.states = states;
// }
}
答案1
得分: 0
mappedBy
只在两个类之间的关系中使用一次,用于定义你想要双向建立关系的那个类。所以在Countries
类中可能根本不需要mappedBy
,在Cities
类中,你应该将mappedBy
设置为"state"
,而在State
类中将mappedBy
设置为"country"
会更合适。
英文:
mappedBy is only used once in a relationship between 2 classes, for the class that you want to define the relationship for both. So in the class Countries you probably don't need mappedBy at all, in the class Cities, you should put mappedBy="state", and in the class State it would make sense to put mappedBy="country"
专注分享java语言的经验与见解,让所有开发者获益!
评论