Basit
Basit

Reputation: 8626

Spring data Jpa findTop and findFirst generating error Incorrect syntax near '@P0'

I am using spring data JPA 1.8.2 and trying to get top result. I am using the following method

LearningSession findTopBySourceAndExternalLmsSessionIdAndCourseCodeAndLearnerEnrollmentEnrollmentStatusOrderByIdAsc(String source, String externalLmsSessionId, String courseCode, String enrollmentStatus);

I tried also by removing OrderBy

LearningSession findTopBySourceAndExternalLmsSessionIdAndCourseCodeAndLearnerEnrollmentEnrollmentStatus(String source, String externalLmsSessionId, String courseCode, String enrollmentStatus);

In both cases hibernate is generating the following query. Just use * for every thing. For order by in the end 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=?

Query is right But I am getting the following exception

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:?]

What I am doing wrong ? I tried First, Top1, First1 but result is error. Please help.

Thanks

Upvotes: 4

Views: 4216

Answers (2)

Noman Liaquat
Noman Liaquat

Reputation: 23

I had the same issue with Hibernate 5 + Spring Data. Changing the Dialect to SQLServer2012Dialect worked for me.

public class SQLServerNativeDialect extends SQLServer2012Dialect

For reference:: https://hibernate.atlassian.net/browse/HHH-10032

Thanks,

Upvotes: 0

Kevin Dubois
Kevin Dubois

Reputation: 380

Not sure if this will work for you, but I ran into the same error and after a lot of digging I figured out that I was using SqlServerDialect in my JpaVendorAdapter, and when I changed it to be more specifically for SqlServer2012 (SqlServerDialect2012) the error went away:

<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>

Upvotes: 8

Related Questions