我正在使用Spring数据JPA 1.8.2并尝试获得最佳结果。我使用以下方法
LearningSession findTopBySourceAndExternalLmsSessionIdAndCourseCodeAndLearnerEnrollmentEnrollmentStatusOrderByIdAsc(String source, String externalLmsSessionId, String courseCode, String enrollmentStatus);
我也尝试删除OrderBy
LearningSession findTopBySourceAndExternalLmsSessionIdAndCourseCodeAndLearnerEnrollmentEnrollmentStatus(String source, String externalLmsSessionId, String courseCode, String enrollmentStatus);
在这两种情况下,hibernate都会生成以下查询。只需使用*
即可。对于最后的订单order by learningse0_.id asc
select TOP ? learningse0_.*
from LearningSession learningse0_
left outer join LearnerEnrollment learnerenr1_ on learningse0_.ENROLLMENT_ID=learnerenr1_.id
where learningse0_.source=?
and learningse0_.externalLmsSessionId=?
and learningse0_.courseCode=?
and learnerenr1_.enrollmentStatus=?
查询是正确但我收到以下异常
Hibernate: select TOP ? learningse0_.id as id1_47_, learningse0_.brandName as brandNam2_47_, learningse0_.courseApprovalId as courseAp3_47_, learningse0_.courseCode as courseCo4_47_, learningse0_.endTime as endTime5_47_, learningse0_.externalLmsSessionId as external6_47_, learningse0_.externalLmsUrl as external7_47_, learningse0_.isCourseMessageDisplay as isCourse8_47_, learningse0_.LANGUAGE_ID as LANGUAG15_47_, learningse0_.LEARNER_ID as LEARNER16_47_, learningse0_.ENROLLMENT_ID as ENROLLM17_47_, learningse0_.learningSessionGuid as learning9_47_, learningse0_.lmsProvider as lmsProv10_47_, learningse0_.redirectUrl as redirec11_47_, learningse0_.source as source12_47_, learningse0_.startTime as startTi13_47_, learningse0_.uniqueUserGuid as uniqueU14_47_ from LearningSession learningse0_ left outer join LearnerEnrollment learnerenr1_ on learningse0_.ENROLLMENT_ID=learnerenr1_.id where learningse0_.source=? and learningse0_.externalLmsSessionId=? and learningse0_.courseCode=? and learnerenr1_.enrollmentStatus=? order by learningse0_.id asc
13:53:47.686 [main] DEBUG org.hibernate.engine.jdbc.spi.SqlExceptionHelper - could not extract ResultSet [n/a]
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '@P0'
com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216) ~[sqljdbc4-4.0.jar:?]
我做错了什么?我尝试了First
,Top1
,First1
,但结果是错误的。请帮忙。
由于
答案 0 :(得分:8)
不确定这是否适合您,但我遇到了同样的错误,经过大量挖掘后我发现我在JpaVendorAdapter中使用了SqlServerDialect,当我将其更改为更具体的SqlServer2012时(SqlServerDialect2012) )错误消失了:
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="SQL_SERVER" />
<property name="databasePlatform" value="org.hibernate.dialect.SQLServer2012Dialect" />
<property name="showSql" value="true" />
</bean>
答案 1 :(得分:0)
我遇到了与Hibernate 5 + Spring Data相同的问题。 将Dialect更改为SQLServer2012Dialect对我有用。
public class SQLServerNativeDialect扩展SQLServer2012Dialect
供参考:: https://hibernate.atlassian.net/browse/HHH-10032
谢谢,