Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法从两个表中选择:调用属性的setter时发生_Java_Sql_Hibernate_Spring Mvc - Fatal编程技术网

Java 无法从两个表中选择:调用属性的setter时发生

Java 无法从两个表中选择:调用属性的setter时发生,java,sql,hibernate,spring-mvc,Java,Sql,Hibernate,Spring Mvc,我使用SpringMVC和Hibernate。当我从2个表中选择时,我收到一个异常,如下所示: 我的豆车和地图: 车辆类别: @Entity @Table(name="Cars") public class Car implements Serializable { private static final long serialVersionUID = 5924361831551833717L; @Id @Column(name = "id") @GeneratedValue(s

我使用SpringMVC和Hibernate。当我从2个表中选择时,我收到一个异常,如下所示:

我的豆车和地图:

车辆类别:

@Entity
@Table(name="Cars")
public class Car implements Serializable {

private static final long serialVersionUID = 5924361831551833717L;  

 @Id
 @Column(name = "id")
 @GeneratedValue(strategy=GenerationType.AUTO)
 private Integer carId;

 @Column(name = "name")
 private String name;

 @ManyToOne
 @JoinColumn(name="map_id")
 private Map mapId;

 public Integer getCarId() {
    return this.carId;
 }

 public void setCarId(Integer carId) {
    this.carId = carId;
 }

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Map getMapId() {
    return this.mapId;
}

public void setMapId(Map mapId) {
    this.mapId = mapId;
 }
}
地图类别:

@Entity
@Table(name="Map")
public class Map implements Serializable {

private static final long serialVersionUID = -5527566248002296042L;

 @Id
 @Column(name = "id")
 @GeneratedValue(strategy=GenerationType.AUTO)
 private Integer mapId;

 @Column(name = "address")
 private String address;

 public Integer getMapId() {
    return mapId;
 }

 public void setMapId(Integer mapId) {
    this.mapId = mapId;
 }

 public String getAddress() {
    return address;
 }

 public void setAddress(String address) {
    this.address = address;
  } 
 }
SQL从2个表中选择:

SELECT  CAR.ID AS carId, 
    CAR.NAME AS name, 
    MAP.ID AS mapId
FROM    CARS CAR, 
    MAP MAP
WHERE   CAR.MAP_ID = MAP.ID 
日志消息:

BasicPropertyAccessor - HHH000123: IllegalArgumentException in class: com.totoroads.web.model.Car, setter method of property: mapId
BasicPropertyAccessor - HHH000091: Expected type: com.totoroads.web.model.Map, actual value: java.lang.Integer
CarRepositoryImpl - error at CarRepositoryImpl.getAllCars: IllegalArgumentException occurred while calling setter for property [com.tct.web.model.Car.mapId (expected type = com.tct.web.model.Map)]; target = [com.tct.web.model.Car@23743c55], property value = [13] setter of com.tct.web.model.Car.mapId
如何修复此错误,非常感谢

@Entity
@Table(name="Map")
public class Map implements Serializable {

  private static final long serialVersionUID = -5527566248002296042L;

  @Id
  @Column(name = "id")
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Integer mapId;

  @OneToMany(mappedBy = "mapId", cascade = CascadeType.ALL)
  private List<Car> cars;

  @Column(name = "address")
  private String address;

  public Integer getMapId() {
     return mapId;
  }

  public void setMapId(Integer mapId) {
     this.mapId = mapId;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  } 
  public List<Car> getCars() {
    return cars;
  }

  public void setCars(List<Car> cars) {
      this.cars= cars;
  }
}

使用上面的map类

你可以共享map bean吗?嗨@santoshgore:我已经发布了map bean你正在尝试从map bean获取所有汽车,所以你必须在map bean中声明汽车列表。使用双向多对一映射。我想你应该使用地址作为嵌入实体。我已经按照你的建议尝试了,但它仍然是相同的错误add cascade=CascadeType.ALL in-car bean.hI@santosh-gore:当我使用方法时,这个问题已经得到了解决:return List sessionFactory.getCurrentSession.createCriteriaCar.class.List;非常感谢你: