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

我收到以下错误:


  com.microsoft.sqlserver.jdbc.SQLServerException:无效的列名
  '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>{
            }


控制者

            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显示/插入/删除出租车表数据。

最佳答案

默认情况下,您的持久性配置似乎通过用下划线将其分隔来转换驼峰格式。因此,如果您有:

   @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

10-08 16:28