gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:35:46 24 4
gpt4 key购买 nike

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

供应商.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

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

最佳答案

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

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

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

关于mysql - SpringBoot - MySQL - 列表错误中的未知列字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48720091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com