【问题标题】:How to solve "org.springframework.dao.DataIntegrityViolationException: could not execute query; SQL"如何解决“org.springframework.dao.DataIntegrityViolationException:无法执行查询;SQL”
【发布时间】:2020-11-16 17:37:10
【问题描述】:

我尝试通过给定的城市 ID 从表 Temperature 中获取所有项目。出了点问题,我收到以下错误:

org.springframework.dao.DataIntegrityViolationException: 不能 执行查询; SQL [选择 tempatur0_.id 作为 id1_3_, tempatur0_.Id 作为 Id1_3_,tempatur0_.dateTime 作为 dateTime2_3_, temperatur0_.TemperatureValue as Temperat3_3_ 来自温度 tempatur0_ 左外连接 city_attributes city1_ on tempatur0_.Id=city1_.Id where city1_.Id=?];嵌套异常是 org.hibernate.exception.DataException:无法执行查询

我是 java 新手,我不知道我是否以正确的方式使用了表关系的映射。

@Entity
@Table(name = "city_attributes")
public class City {
    
    @javax.persistence.Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "Id")
    private Long id;
    
    @Column(name = "City")
    private String name;
    
    @Column(name = "Country")
    private String country;
    
    @Column(name = "Latitude")
    private String latitude;
    
    @Column(name = "Longitude")
    private String longitude;
    
    @OneToMany(mappedBy = "city")
    private Collection<Temperature> temperatures = new LinkedList<Temperature>();
    
    public City() {}
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getLatitude() {
        return latitude;
    }
    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }
    public String getLongitude() {
        return longitude;
    }
    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }
    
    @Override
    public String toString() {
        return "City [Id=" + id + ", name=" + name + ", country=" + country + ", latitude=" + latitude + ", longitude="
                + longitude + "]";
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Collection<Temperature> getTemperatures() {
        return temperatures;
    }

    public void setTemperatures(Collection<Temperature> temperatures) {
        this.temperatures = temperatures;
    }
    
    
}

@Entity
@Table(name = "temperature")
public class Temperature {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Temporal(TemporalType.DATE)
    private Date dateTime;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "Id", nullable = false)
    private City city;
    
    @Column(name = "TemperatureValue")
    private double temperatureValue; 
    
    public Temperature() {}

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getDateTime() {
        return dateTime;
    }

    public void setDateTime(Date dateTime) {
        this.dateTime = dateTime;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    public double getTemperatureValue() {
        return temperatureValue;
    }

    public void setTemperatureValue(double temperatureValue) {
        this.temperatureValue = temperatureValue;
    }
}
try {
    city = cityService.findCityByName(name);
    Long cityId = city.getId();

    temperature = temperatureService.findTempByCityId(cityId); 
    map.addObject("temperature", temperature);
} catch (Exception e) {
    System.out.println(e.toString());
}
        

=== 已更新

我从控制器调试方法时发现了这个错误

java.sql.SQLDataException:无法从字符串确定值类型 '1/1/2015 0:00'

@Robert Niestroj 这是我对 TemperatureService 和 TemperatureRepository 的实现

@Override
public List<Temperature> findTempByCityId(Long id) {
    return temperatureRepository.findByCityId(id);
}
public List<Temperature> findByCityId(Long id);

【问题讨论】:

  • 您能添加您的应用程序代码吗?至少是您构建查询的部分。
  • 您的 JoinColumn 在我看来是错误的。它应该像 @JoinColumn(name = "city_id", nullable = false)
  • 我们需要看看temperatureService.findTempByCityId(cityId);的实现——查询
  • 在您的 Temperature 类中,尝试映射您的 dateTime 属性的列。试着去掉这个@Temporal注解,把“Date”改成“LocalDateTime”类型
  • Murilo Góes de Almeida,我试过了,但没用。

标签: java spring hibernate spring-mvc jpa


【解决方案1】:

Temperature 中,@JoinColumn(name = "Id", nullable = false) 看起来很可疑,因为它映射到 id 列。我怀疑它应该是id_city 或类似的东西。这可能会导致错误。

【讨论】:

  • 我尝试了您的解决方案并添加了一个新列“city_id”。我已经有一个名为 CityId 的列。
猜你喜欢
  • 2022-08-03
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
  • 2021-02-28
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
相关资源
最近更新 更多