Spring引导查询失败,使用MSSQL数据库

时间:2017-04-19 14:43:25

标签: spring spring-boot spring-data spring-data-jpa

我有一个spring boot应用程序版本1.4.3.RELEASE 使用驱动程序版本4.1

使用MS SQL db

我尝试实现一个查询,以下列方式显示前100个结果

div class="requestgrid" id="requestGrid">
@(Html.Kendo().Grid<CC.GRP.MCRequest.ViewModels.RequestViewModel>()
.Name("GridRequest")
.Columns(columns => {
    columns.Bound(o => o.RequestID).Width("100px");
    columns.Bound(o => o.CountryCode).Width("100px");
    columns.Bound(o => o.SalesOffice).Width("100px");
    columns.Bound(o => o.CustomerNumber).Width("120px");
    columns.Bound(o => o.ProjectName).Width("120px");
    columns.Bound(o => o.ProjectContent).Width("120px");
    columns.Bound(o => o.ContractStartDate).Width("140px");
    columns.Bound(o => o.Priority).Width("120px");
    columns.Bound(o => o.NameOfResponsiblePerson).Width("170px");
    columns.Bound(o => o.Status).Width("100px");
    columns.Bound(o => o.CreatedBy).Width("110px");
    columns.Bound(o => o.CreatedDate).Width("110px");
    columns.Bound(o => o.ModifiedBy).Width("110px");
})
//.ToolBar(toolbar => toolbar.Create())
//.Editable(editable => editable.Mode(GridEditMode.PopUp))
.Pageable()
.Sortable()
.Filterable()
.Scrollable()
.Groupable()

.Selectable(selectable => selectable
    .Mode(GridSelectionMode.Single)
    .Type(GridSelectionType.Row))
    .Events(events => events
    .Change("onChange")
)
.DataSource(dataSource => dataSource
    .Ajax()
    .Model(model => model.Id(t => t.RequestID))
        //.Create(update => update.Action("Team_Create", "Admin"))
        .Read(read => read.Action("Requests_Read", "Request"))
    //.Update(update => update.Action("Team_Update", "Admin"))
    )
)

项目编译很好,但是当我点击第二个查询 findTop100ByOrderByTimestampDesc 时,我收到以下错误:

  

休眠:       /* 选择           generatedAlias0       从           EventTracking as generatedAlias0       订购           generatedAlias0.timestamp desc / select               最佳 ? eventtrack0_.nEventNumber为nEventNu1_9_,               eventtrack0_.strAction为strActio2_9_,               eventtrack0_.strEventMsg as strEvent3_9_,               eventtrack0_.strEventType为strEvent4_9_,               eventtrack0_.FHandleEvent为FHandleE5_9_,               eventtrack0_.StrIpAddress为StrIpAdd6_9_,               eventtrack0_.strMACAddress为strMACAd7_9_,               eventtrack0_.nPort as nPort8_9_,               eventtrack0_.strSwitchIP为strSwitc9_9_,               eventtrack0_.nDate as nDate10_9_           从               SWAT.dbo.tblEventsTracking eventtrack0_           订购               eventtrack0_.nDate desc 2017-04-20 03:38:28.791 DEBUG 11016 --- [-nio-555-exec-3] c.m.s.jdbc.internals.SQLServerStatement:   SQLServerPreparedStatement:3创建者(ConnectionID:10   ClientConnectionId:a144ec47-037d-40a7-be92-db1725614208)2017-04-20   03:38:28.807 DEBUG 11016 --- [-nio-555-exec-3]   c.m.s.jdbc.internals.SQLServerStatement:   SQLServerPreparedStatement:3:调用sp_prepexec:PreparedHandle:0,   SQL:/ 从EventTracking中选择generatedAlias0作为generatedAlias0   按generateAlias0.timestamp desc * /选择TOP @ P0   eventtrack0_.nEventNumber为nEventNu1_9_,eventtrack0_.strAction为   strActio2_9_,eventtrack0_.strEventMsg as strEvent3_9_,   eventtrack0_.strEventType为strEvent4_9_,eventtrack0_.FHandleEvent   作为FHandleE5_9_,eventtrack0_.StrIpAddress为StrIpAdd6_9_,   eventtrack0_.strMACAddress as strMACAd7_9_,eventtrack0_.nPort as   nPort8_9_,eventtrack0_.strSwitchIP为strSwitc9_9_,   来自SWAT.dbo.tblEventsTracking的eventtrack0_.nDate as nDate10_9_   eventtrack0_ order by eventtrack0_.nDate desc 2017-04-20   03:38:28.815 DEBUG 11016 --- [-nio-555-exec-3]   c.m.s.jdbc.internals.SQLServerException:*** SQLException:   com.microsoft.sqlserver.jdbc.SQLServerException:附近的语法不正确   &#39; @ P0&#39 ;. Msg 102,Level 15,State 1,&#39; @ P0&#39;附近的语法不正确。   2017-04-20 03:38:28.815 WARN 11016 --- [-nio-555-exec-3]   o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:102,SQLState:   S0001 2017-04-20 03:38:28.815 ERROR 11016 --- [-nio-555-exec-3]   o.h.engine.jdbc.spi.SqlExceptionHelper:附近的语法不正确   &#39; @ P0&#39;

这是数据库的JPA错误还是我的查询错误

我从这里使用了参考资料 mapping

1 个答案:

答案 0 :(得分:0)

FirstTop关键字在某些SQL实现方面不能很好地发挥作用,如果我没记错的话,他们就不能使用MySQL。我总是遇到这个问题,所以我现在就避开它们。最简单的选择是在您的请求中使用Pageable并以此方式限制它。

所以这个:

public List<EventTracking> findTop100ByOrderByTimestampDesc();

会变成这样:

public List<EventTracking> findAllByOrderByTimestampDesc(Pageable pageable);

你可以像这样构建参数对象:

Pageable pageable = new PageRequest(0, 100);

不像使用TopFirst一样干净,而是快速修复以绕过SQL兼容性问题,这比将整个结果集放入数组并取出第一个更快&#34; N&#34;他们......

编辑:

另外,检查您的Spring Data JPA版本,它需要为1.7或更高版本。如果这不起作用,那就是数据库兼容性。