gpt4 book ai didi

java - 如何修复 "com.jdbc.SQLServerException: Invalid column name ' taxi_id' "

转载 作者:行者123 更新时间:2023-11-30 06:41:00 27 4
gpt4 key购买 nike

我是 Spring Boot/JPA 的初学者。我正在尝试对出租车模型进行 CRUD 操作。

我收到以下错误:

com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name'taxi_id'

如有任何帮助,我们将不胜感激。

这是我的仓库代码:

package com.example.SpringBoot.Model.Repository;

import org.springframework.data.repository.CrudRepository;
import com.example.SpringBoot.Model.Taxi;

public interface DAO extends CrudRepository<Taxi, Integer>{
}

Controller

package com.example.SpringBoot.Controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.SpringBoot.Model.Taxi;
import com.example.SpringBoot.Model.Repository.DAO;


@RestController
public class TaxiController {

@Autowired
public DAO repo;

@PutMapping(path= "/Taxi" , produces = {"application/json"})
public Taxi insertTaxi(Taxi taxi) {
repo.save(taxi);
return taxi;
}

@DeleteMapping(path ="/Taxi/{TaxiId}")
public void deleteTaxi(@RequestParam int TaxiId) {

repo.deleteById(TaxiId);
}

@GetMapping(path ="/Taxi")
public List<Taxi> displayTaxi() {
return (List<Taxi>) repo.findAll();
}
}

模型类

package com.example.SpringBoot.Model;

import java.math.BigDecimal;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Taxi {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int TaxiId;
private String TaxiType;


public int getTaxiId() {
return TaxiId;
}


public void setTaxiId(int taxiId) {
TaxiId = taxiId;
}

public String getTaxiType() {
return TaxiType;
}

public void setTaxiType(String taxiType) {
TaxiType = taxiType;
}
}

SQL 服务器代码

CREATE TABLE [dbo].[Taxi](
[Taxiid] [int] IDENTITY(1,1) NOT NULL,
[TaxiType] [varchar](1) NOT NULL,

PRIMARY KEY CLUSTERED
(
[TaxiId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

堆栈跟踪:

    .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.4.RELEASE)

2019-06-03 15:37:24.554 INFO 7808 --- [ main] c.e.SpringBoot.TaxiBookingApplication : Starting TaxiBookingApplication on IMCHLT080 with PID 7808 (D:\workspace\TaxiBooking\target\classes started by vignesh_nithin in D:\workspace\TaxiBooking
.............
2019-06-03 15:37:34.446 INFO 7808 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-06-03 15:37:34.456 INFO 7808 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 10 ms
2019-06-03 15:37:34.530 INFO 7808 --- [nio-8081-exec-1] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
2019-06-03 15:37:34.645 WARN 7808 --- [nio-8081-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 207, SQLState: S0001
2019-06-03 15:37:34.645 ERROR 7808 --- [nio-8081-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : Invalid column name 'taxi_id'.
2019-06-03 15:37:34.668 ERROR 7808 --- [nio-8081-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause

com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'taxi_id'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:259) ~[mssql-jdbc-6.4.0.jre8.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1547) ~[mssql-jdbc-6.4.0.jre8.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:548) ~[mssql-jdbc-6.4.0.jre8.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:479) ~[mssql-jdbc-6.4.0.jre8.jar:na]

我希望根据被点击的 URL 显示/插入/删除 Taxi 表数据。

最佳答案

默认情况下,您的持久性配置似乎通过用下划线分隔来翻译驼峰式。所以如果你有:

   @Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int TaxiId;

它将尝试使用:'taxi_id';

在 db 上你有:

        CREATE TABLE [dbo].[Taxi](
[Taxiid] [int] IDENTITY(1,1) NOT NULL,

您应该将字段名称更改为:'Taxiid' 或明确命名该列:

 @Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Taxiid")
private int TaxiId;

您还可以尝试通过相应地更改这些属性来更改使用的默认命名策略:

spring.jpa.hibernate.naming.implicit-strategy
spring.jpa.hibernate.naming.physical-strategy

关于java - 如何修复 "com.jdbc.SQLServerException: Invalid column name ' taxi_id' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56425582/

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