简体   繁体   中英

Why is Hibernate throwing an invalid column exception? My query runs well in Oracle, but throws an exception in Java code

Constants.java

 public static  final String A=" SELECT  name as teamName,location as bookingLoc FROM table where emp_ps_id = :empPSID and rownum<2" ;

 public static  final String A=" SELECT  name as team,location as booking FROM table where emp_ps_id = :empPSID and rownum<2" ;

If I use these queries with Hibernate, first one throws an invalid column name exception and the second one produces correct result.

 public Details getDetails(final String psid) {
        final String finalQuery = Constants.A;
        return (Details) entityManager.createNativeQuery(finalQuery, Details.class)
                .setParameter("empPSID", psid)
                .getSingleResult();
    }

As no explicit query mapping is given, the implicit mapping is taking place and thus the aliased names and types in the query (those after as such as teamName ) needs to correspond to fields on the result class Details .

Synchronizing the aliased names and types between the query and result class will fix the problem. Note that every fields on the result class needs to be delivered by the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM