1

I need to convert the below condition in JPA2,but found there is no alternative to Apply a constraint expressed in SQL with no JDBC parameters

Restrictions.sqlRestriction("{alias}.col3& " + value + " = " + value);

I tried using NativeQuery like the below sinppet where value is the parameter passing to the method:-

String sqlString="select * from myTable where col1=3600 and col2=true and col3&"+value+"="+value;
        Query query=getSession().createNativeQuery(sqlString);
        List<userDefClass> results = new ArrayList<userDefClass>();
        results=(List<userDefClass>)query.getResultList();

For the above logic ,I am getting below error:-

"java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.pkg.userDefClass java.lang.RuntimeException

query.getResultList() is passing list of objects but not list of userdefClass objects.

ALso I tried using Typed Query but it is throwing SQL Grammer Exception.I guess its not parsing the bitwise '&' operator in the SQL String.

Please help.

  • You can't cast the the result list like that. See [this](http://stackoverflow.com/questions/13012584/jpa-how-to-convert-a-native-query-result-set-to-pojo-class-collection) answer on how to map a result set in JPA, I think that might set you on the right course. – Matt May 16 '17 at 05:23
  • Thanks..let me go through the link...hope it helps – user8014983 May 16 '17 at 07:47
  • Tried a lot,still getting SQL Grammer Exception as it is not able to understand the bitwise operator '&' in the native SQL query. – user8014983 May 16 '17 at 13:23
  • Below is the link if someone is looking for answer http://stackoverflow.com/a/44045138/8014983 – user8014983 May 19 '17 at 06:04

1 Answers1

0

please Try this :

query.setResultTransformer(new AliasToBeanResultTransformer(userDefClass.class));