SpringBoot - MySQL - 列表错误中的未知列字段

时间:2018-02-10 11:10:16

标签: mysql spring-boot

以下是我的Spring启动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

请查看错误: -

Eclipse error log

http://localhost:8089/demo/all

Table details

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

仍然,我收到错误:

eclipse log

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

3 个答案:

答案 0 :(得分:1)

我只是尝试使用小写的列名,但它确实有效!

@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;

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

如果您使用的是Hibernate v5,请在application.properties

中使用以下行
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl