【问题标题】:SpringBoot - MySQL - Unknown column field in list errorSpringBoot - MySQL - 列表错误中的未知列字段
【发布时间】:2018-07-21 01:34:32
【问题描述】:

以下是我的 Spring boot Rest API 应用程序。

Vendor.Java

package hello;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity(name="vendor")
public class Vendor {

    @Id
    @Column(name="vendorId")
    private int vendorId;
    @Column(name="vendorName")
    private String vendorName;
    @Column(name="vendorPhone")
    private int vendorPhone;
    @Column(name="vendorBalance")
    private int vendorBalance;
    @Column(name="vendorChequeAmount")
    private int vendorChequeAmount;


    public int getVendorId() {
        return vendorId;
    }
    public void setVendorId(int vendorId) {
        this.vendorId = vendorId;
    }
    public String getVendorName() {
        return vendorName;
    }
    public void setVendorName(String vendorName) {
        this.vendorName = vendorName;
    }
    public int getVendorPhone() {
        return vendorPhone;
    }
    public void setVendorPhone(int vendorPhone) {
        this.vendorPhone = vendorPhone;
    }
    public int getVendorBalance() {
        return vendorBalance;
    }
    public void setVendorBalance(int vendorBalance) {
        this.vendorBalance = vendorBalance;
    }
    public int getVendorChequeAmount() {
        return vendorChequeAmount;
    }
    public void setVendorChequeAmount(int vendorChequeAmount) {
        this.vendorChequeAmount = vendorChequeAmount;
    }

}

VendorRepository.Java

package hello;

import org.springframework.data.repository.CrudRepository;

import hello.Vendor;

// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete

public interface VendorRepository extends CrudRepository<Vendor, Integer> {

}

MainController.Java

package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import hello.Vendor;
import hello.VendorRepository;

@Controller    // This means that this class is a Controller
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
public class MainController {
    @Autowired // This means to get the bean called userRepository
               // Which is auto-generated by Spring, we will use it to handle the data
    private VendorRepository vendorRepository;

    @GetMapping(path="/all")
    public @ResponseBody Iterable<Vendor> getAllVendors() {
        // This returns a JSON or XML with the users
        return vendorRepository.findAll();
    }
    @GetMapping(path="/msg")
    public @ResponseBody String getMsg(){
        return "Hi";
    }
}

Application.Java

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

当我尝试访问 @http://localhost:8089/demo/all 时出现以下错误

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Feb 10 14:59:39 GST 2018
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

请查看错误:-

Application.properties

server.port=8089
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.type=trace 
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.datasource.url=jdbc:mysql://localhost:3306/web_customer_tracker
spring.datasource.username=springstudent
spring.datasource.password=springstudent

仍然,我得到了错误:

这是什么错误?如何解决?

【问题讨论】:

    标签: mysql spring-boot


    【解决方案1】:

    从错误信息来看,似乎是在 SQL 查询中生成了错误的列名。我建议在开发时在application.properties 中设置spring.jpa.show-sql=true 以在控制台中查看生成的查询。

    该错误很奇怪,因为当您将其名称设置为 vendorId 时,它会抱怨列 vendor_id(werid 数据库列命名策略,顺便说一句)。稍微研究了一下,我遇到了this question,根据它的第一个答案,它似乎是某种列名的错误。如那里所说,尝试将其添加到您的application.preferences:

    spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
    

    【讨论】:

    • 您好,请查看我的 application.properties。此外,我无法看到控制台中生成的 SQL 查询。我正在将应用程序作为 Java 应用程序运行。
    • 我作为 Spring boot App 运行,我可以在日志中看到生成的 SQL。是的,这是错误的..Hibernate: /* select generatedAlias0 from vendor as generatedAlias0 */ select vendor0_.vendor_id as vendor_i1_0_, vendor0_.vendor_balance 作为 vendor_b2_0_, vendor0_.vendor_cheque_amount 作为 vendor_c3_0_, vendor0_.vendor_name 作为 vendor_n4_0_, vendor0_.vendor_phone 作为 vendor_p5_0_ from供应商 vendor0_
    【解决方案2】:

    如果您使用的是 Hibernate v5,请在 application.properties 中使用以下行

    spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    

    【讨论】:

      【解决方案3】:

      我刚刚尝试使用小写的列名,它成功了!

      @Id
      @Column(name="vendorid")
      private int vendorId;
      @Column(name="vendorname")
      private String vendorName;
      @Column(name="vendorphone")
      private int vendorPhone;
      @Column(name="vendorbalance")
      private int vendorBalance;
      @Column(name="vendorchequeamount")
      private int vendorChequeAmount;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多