如何解决"java.sql.SQLSyntaxErrorException:“字段列表"中的未知列"product0_.return_policy"例外? [英] How to fix "java.sql.SQLSyntaxErrorException: Unknown column 'product0_.return_policy' in 'field list' " exception?

查看:1020
本文介绍了如何解决"java.sql.SQLSyntaxErrorException:“字段列表"中的未知列"product0_.return_policy"例外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经收到此"SQLSyntaxErrorException:字段列表"中的未知列"product0_.return_policy""当我尝试在浏览器上运行URL以 GET 所有产品.

I've been getting this "SQLSyntaxErrorException:Unknown column 'product0_.return_policy' in 'field list'" when I try to run the URL on my browser to GET all the products.

看这里

浏览器也会显示以下内容:

发生意外错误(类型=内部服务器错误,状态= 500). 无法提取ResultSet; SQL [n/a];嵌套的异常是org.hibernate.exception.SQLGrammarException:无法提取ResultSet

There was an unexpected error (type=Internal Server Error, status=500). could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

returnPolicy 是导致此问题的唯一变量.当我同时从数据库和Java中的Product类中删除变量本身时,便能够从数据库中成功检索所有值.

returnPolicy is the only variable that's causing this problem. I'm able to successfully retrieve all the values from the database when I remove the variable itself from both the database and from the Product class in Java.

这是RESTController一部分的 getAllProducts 方法:

This is the getAllProducts method that is part of the RESTController:

@RequestMapping(method=RequestMethod.GET, value="/products")
    public List<Product> getAllProducts() {
        return productService.getAllProducts();
    }

当我完全删除returnPolicy变量时,效果很好.

which works fine when I remove the returnPolicy variable altogether.

是MySQL表描述:

This is the MySQL table description:

存储在returnPolicy列中的值:

returnPolicy

returnPolicy

  0 
  0 
  1 
  1 
  1 

这是产品"模型变量的代码:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="product")
public class Product {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String name;
    private int price;
    private String vendor;
    private String description;
    private Boolean returnPolicy;

ProductRepository

@Repository public interface ProductRepository extends JpaRepository<Product, String>{ }

SQL tinyint(Boolean)与Java的Boolean类型之间的映射是否存在问题?

Is there a problem with the mapping between SQL tinyint(Boolean) and the Boolean type of Java?

推荐答案

Hibernate假定实体字段returnPolicy对应于表列return_policy.但实际上,列名是returnPolicy

Hibernate is assuming that the entity field returnPolicy corresponds to the table column return_policy. But actually, the column name is returnPolicy

Hibernate遵循一种命名策略,即应从实体字段名称派生出什么列名称.您应该明确指定是使用ImplicitNamingStrategy还是PhysicalNamingStrategy. Hibernate为此提供了开箱即用的类.

Hibernate follows a naming strategy as to what column name should it derive from entity field names. You should explicitly specify whether to use ImplicitNamingStrategy or PhysicalNamingStrategy. Hibernate provides out of the box classes for this.

或者,对于此特定问题,使用显式列名注释字段将很容易理解.

Alternatively, for this specific issue, annotating the field with explicit column name will make is understand.

请参阅 SO答案以获取更多信息.

See this SO answer for more.

这篇关于如何解决"java.sql.SQLSyntaxErrorException:“字段列表"中的未知列"product0_.return_policy"例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆