Java 使用Spring数据的多级一对多关系

Java 使用Spring数据的多级一对多关系,java,spring,spring-data-jpa,Java,Spring,Spring Data Jpa,我正试图建立一个小型演示会议室预订 我的实体是城市-->位置-->楼层-->房间,具有自上而下的一对多关系 这些是我的实体 @Entity @Table(name="office_city") public class City { @Id @GeneratedValue private int cityId; private String cityName; @OneToMany(fetch = FetchType.EAGER,mappedBy="city", cascade = Casca

我正试图建立一个小型演示会议室预订

我的实体是城市-->位置-->楼层-->房间,具有自上而下的一对多关系

这些是我的实体

@Entity
@Table(name="office_city")
public class City {

@Id
@GeneratedValue
private int cityId;
private String cityName;
@OneToMany(fetch = FetchType.EAGER,mappedBy="city", cascade = CascadeType.ALL)
@JsonManagedReference
private List<Location> location;

public List<Location> getLocation() {
    return location;
}
public void setLocation(List<Location> location) {
    this.location = location;
}
public int getCityId() {
    return cityId;
}
public void setCityId(int cityId) {
    this.cityId = cityId;
}
public String getCityName() {
    return cityName;
}
public void setCityName(String cityName) {
    this.cityName = cityName;
}

@Override
public String toString() {
    return "[cityId="+cityId+" cityName="+cityName+" location="+location.size()+"]";
}
}

public class Location {

@Id
@GeneratedValue
private int locationId;
private String locationName;

@ManyToOne
@JoinColumn(name="cityId")
@JsonBackReference
private City city;

@OneToMany(fetch = FetchType.EAGER,mappedBy="location",cascade=CascadeType.ALL)
@JsonManagedReference
private List<Floor> floor;


public List<Floor> getFloor() {
    return floor;
}
public void setFloor(List<Floor> floor) {
    this.floor = floor;
}
public City getCity() {
    return city;
}
public void setCity(City city) {
    this.city = city;
}
public int getLocationId() {
    return locationId;
}
public void setLocationId(int cityId) {
    this.locationId = cityId;
}
public String getLocationName() {
    return locationName;
}
public void setLocationName(String cityName) {
    this.locationName = cityName;
}
}

public class Floor {

@Id
@GeneratedValue
private int floorId;
private String floorName;

@ManyToOne
@JoinColumn(name="locationId")
@JsonBackReference
private Location location;

@OneToMany(fetch = FetchType.EAGER,mappedBy="floor",cascade=CascadeType.ALL)
@JsonManagedReference
private List<Room> room;

public List<Room> getRoom() {
    return room;
}
public void setRoom(List<Room> room) {
    this.room = room;
}
public Location getLocation() {
    return location;
}
public void setLocation(Location location) {
    this.location = location;
}
public int getFloorId() {
    return floorId;
}
public void setFloorId(int cityId) {
    this.floorId = cityId;
}
public String getFloorName() {
    return floorName;
}
public void setFloorName(String cityName) {
    this.floorName = cityName;
}
}
@实体
@表(name=“办公城市”)
公营城市{
@身份证
@生成值
私人国际城市ID;
私有字符串cityName;
@OneToMany(fetch=FetchType.EAGER,mappedBy=“city”,cascade=CascadeType.ALL)
@JsonManagedReference
私人名单位置;
公共列表getLocation(){
返回位置;
}
公共无效设置位置(列表位置){
这个位置=位置;
}
public int getCityId(){
返回cityId;
}
公共void setCityId(int cityId){
this.cityId=cityId;
}
公共字符串getCityName(){
返回城市名称;
}
公共void setCityName(字符串cityName){
this.cityName=cityName;
}
@凌驾
公共字符串toString(){
返回“[cityId=“+cityId+”cityName=“+cityName+”location=“+location.size()+””;
}
}
公共类位置{
@身份证
@生成值
私有地址ID;
私有字符串locationName;
@许多酮
@JoinColumn(name=“cityId”)
@JsonBackReference
私人城市;
@OneToMany(fetch=FetchType.EAGER,mappedBy=“location”,cascade=CascadeType.ALL)
@JsonManagedReference
私人楼层;
公共列表getFloor(){
返回层;
}
公共楼层(列表楼层){
这个地板=地板;
}
公共城市{
回归城市;
}
公共城市(城市){
this.city=城市;
}
public int getLocationId(){
返回位置ID;
}
public void setLocationId(int cityId){
this.locationId=cityId;
}
公共字符串getLocationName(){
返回位置名称;
}
public void setLocationName(字符串cityName){
this.locationName=cityName;
}
}
公共楼层{
@身份证
@生成值
私人室内地板;
私家串门;
@许多酮
@JoinColumn(name=“locationId”)
@JsonBackReference
私人位置;
@OneToMany(fetch=FetchType.EAGER,mappedBy=“floor”,cascade=CascadeType.ALL)
@JsonManagedReference
私人名单室;
公共列表getRoom(){
返回室;
}
公共无效设置室(列表室){
这个房间=房间;
}
公共位置getLocation(){
返回位置;
}
公共无效设置位置(位置){
这个位置=位置;
}
public int getFloorId(){
返回地板;
}
公共void setFloorId(int cityId){
this.floorId=cityId;
}
公共字符串getFloorName(){
返回楼层名称;
}
public void setFloorName(字符串cityName){
this.floorName=cityName;
}
}
但现在,当我尝试构建时,我得到了以下错误

org.springframework.data.mapping.PropertyReferenceException: No property id found for type Location! Traversed path: City.location.
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:337)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:361)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241)
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76)
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:235)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:353)
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:87)
org.springframework.data.mapping.PropertyReferenceException:找不到类型位置的属性id!穿越路径:City.location。
位于org.springframework.data.mapping.PropertyPath.(PropertyPath.java:75)
位于org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
位于org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
位于org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:337)
位于org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:361)
位于org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
位于org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)
位于org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241)
位于org.springframework.data.repository.query.parser.Part.(Part.java:76)
位于org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:235)
位于org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373)
位于org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:353)
位于org.springframework.data.repository.query.parser.PartTree(PartTree.java:87)

这是关键:
找不到类型位置的属性id
-请尝试将
位置id
命名为
id
你好,罗伯特,谢谢您的快速回复。我应该在哪里将locationId更改为id?在您的location类中-如果错误更改为另一个类,则遵循相同的模式在进一步调查中,我也发现了以下错误信息:HH000262:未找到表:office_room 7月19日,2018年6:32:17 PM org.hibernate.tool.hbm2ddl.SchemaUpdate执行错误:HH000388:不成功:更改表办公室位置添加约束FK_emdg9gon3uwmpdorp1256a7g2外键(位置ID)引用办公室位置(位置ID)7月19日,2018年6:32:17 PM org.hibernate.tool.hbm2ddl.SchemaUpdate执行错误:重命名“”时出错\mrbs@002dauto\#sql-c24_131“到”\mrbs@002dauto\办公室位置(错误编号:150)我不知道为什么会发生这种情况。我注意到您的
位置
楼层
类没有
@实体
@表格
注释,因此它们应该匹配的表格不可见。你是忘了把它们复制到你的问题中,还是它们真的不见了?