简体   繁体   English

三个实体类的一对多和多对一映射中的问题

[英]Issues in one-to-many and many-to-one mapping for three entity classes

I have three entity classes Country , State , and City .我有三个实体类CountryStateCity Now I am trying to display the cities that come under a particular state and states that come under a particular country.现在我试图显示属于特定 state 的城市和属于特定国家的州。 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;
//  }
    
    
}

mappedBy is only used once in a relationship between 2 classes, for the class that you want to define the relationship for both. mappedBy 仅在 2 个类之间的关系中使用一次,对于要为两者定义关系的 class。 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" 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM