Spring CrudRepository中的@NamedQuery - SQLServerException:列名无效' id'

时间:2017-04-06 19:57:57

标签: sql-server spring hibernate jpa spring-data

我正在调用一个在CrudRepository中使用@NamedQuery来返回域对象的服务。我看到了错误

2017-04-05 16:43:16.063  WARN 14240 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 207, SQL
State: S0001
2017-04-05 16:43:16.065 ERROR 14240 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : Invalid column name
 'id'.
2017-04-05 16:43:16.076  INFO 14240 --- [           main] utoConfigurationReportLoggingInitializer :

... 27 common frames omitted
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'id'.
        at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:232) ~[mssql-jd
bc-6.1.0.jre8.jar!/:na]

我不明白这个错误,因为没有专栏' id'在我的数据库表中。不知道发生了什么

我的桌子是

CREATE TABLE [dbo].[tb_changes_reports_v2](
    [reportID] [int] NOT NULL,
    [global_checksum] [int] NULL,
    [specific_checksum] [int] NULL,
    [batch] [int] NULL,
    [pushed] [bit] NULL,
    [compare_date] [datetime] NOT NULL,
    [ver] [bigint] NULL
) ON [PRIMARY]

GO

我的@Entity看起来像

@Entity(name = "ChangesReports")
@Table(name="tb_changes_reports_v2")
@NamedQuery(name = "ChangesReports.findByTheReportId", query = "from ChangesReports c where c.reportId = ?1")
public class ChangesReports extends AbstractPersistable<Long> {

    private static final long serialVersionUID = -452961221197917532L;

    @Column(name="reportID", unique = true)
    private Long reportId;

My @Repository looks like

@Repository
public interface ChangesReportsRepository extends CrudRepository<ChangesReports, Long>, ChangesReportsRepositoryCustom {

    ChangesReports findByTheReportId(Long reportId);

我的服务看起来像

@Override
public ChangesReports findByTheReportId(Long reportId) {
    return changesReportsRepository.findByTheReportId(reportId);
}

我使用这些@Bean定义自动装配我的bean

   @Bean
public ChangesReportsServiceImpl changesReportsServiceImpl() {
        ChangesReportsServiceImpl changesReportsServiceImpl = new ChangesReportsServiceImpl();
        return changesReportsServiceImpl;
    }

    @Bean
    public ChangesReportsRepositoryImpl changesReportsRepository() {
        ChangesReportsRepositoryImpl changesReportsRepositoryImpl = new ChangesReportsRepositoryImpl();
        return changesReportsRepositoryImpl;
    }

再次不确定我为什么会收到“无效的列名称&#39; id&#39;。&#39;因为没有名为&#39; id&#39;的列。任何想法都表示赞赏。

1 个答案:

答案 0 :(得分:0)

Fyi the answer to me was to alter my table so that it has an ID column which was required by AbstractPersistable interface

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tb_changes_reports_v2](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [reportID] [int] NOT NULL,
    [global_checksum] [int] NULL,
    [specific_checksum] [int] NULL,
    [batch] [int] NULL,
    [pushed] [bit] NULL,
    [compare_date] [datetime] NOT NULL,
    [ver] [bigint] NULL
    PRIMARY KEY CLUSTERED
            (
                [ID] ASC
            )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]