由hibernate标准执行的查询与它们在Criteria中编写的顺序不同

时间:2016-03-15 05:16:11

标签: java mysql hibernate-criteria

由hibernate标准执行的从表中获取数据的查询与在条件中调用表的顺序不同。这是我的标准:

    Session session = connector.getSession();
    Criteria cri = session.createCriteria(ST_PRODUCTMASTER.class);       
    cri.createCriteria("ST_PRODUCT_SUPPLIER_PARENT","STPS",JoinType.LEFT_OUTER_JOIN);

    cri.createCriteria("ST_PRODUCT_MANUFACTURER_PARENT","STPM",JoinType.LEFT_OUTER_JOIN);
    cri.createCriteria("ST_PRODUCT_RATES_M","STPRM",JoinType.LEFT_OUTER_JOIN, Restrictions.eqProperty("ST_MANUFACTURER.MAN_ID","STPM.ST_MANUFACTURER.MAN_ID"));
    cri.createCriteria("ST_PRODUCT_RATES_S","STPRS",JoinType.LEFT_OUTER_JOIN, Restrictions.eqProperty("ST_SUPPLIER.SUPPLIER_ID","STPS.ST_SUPPLIER.SUPPLIER_ID"));

    ProjectionList p = Projections.projectionList();
    p.add(Projections.property("PRODUCT_ID"));
    cri.setProjection(p);
    List l = cri.list();

此代码执行的MySqlQuery是

Hibernate: 
select
    this_.PRODUCT_ID as y0_ 
from
    ST_PRODUCTMASTER this_ 
left outer join
    ST_PRODUCT_MANUFACTURER stpm2_ 
        on this_.PRODUCT_ID=stpm2_.PRODUCT_ID 
left outer join
    ST_PRODUCT_RATES stprm3_ 
        on this_.PRODUCT_ID=stprm3_.PRODUCT_ID 
        and (
            stprm3_.MAN_ID=stpm2_.MAN_ID 
        ) 
left outer join
    ST_PRODUCT_RATES stprs4_ 
        on this_.PRODUCT_ID=stprs4_.PRODUCT_ID 
        and (
            stprs4_.SUPPLIER_ID=stps1_.SUPPLIER_ID 
        ) 
left outer join
    ST_PRODUCT_SUPPLIER stps1_ 
        on this_.PRODUCT_ID=stps1_.PRODUCT_ID 
where
    (
        this_.PRODUCT_DELETED <> 'DELETED'
    )

在Query&#34; ST_PRODUCT_SUPPLIER&#34;在查询结束时执行。为了使查询正确执行,我需要查询符合条件中给出的顺序。我收到以下错误。

Mar 15, 2016 10:10:36 AM     org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1054, SQLState: 42S22
Mar 15, 2016 10:10:36 AM     org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Unknown column 'stps1_.SUPPLIER_ID' in 'on clause'
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:89)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2065)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
at org.hibernate.loader.Loader.doQuery(Loader.java:909)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
at org.hibernate.loader.Loader.doList(Loader.java:2551)
at org.hibernate.loader.Loader.doList(Loader.java:2537)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2367)
at org.hibernate.loader.Loader.list(Loader.java:2362)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:126)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1678)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380)
at UTIL.dbFunctions.main(dbFunctions.java:138)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'stps1_.SUPPLIER_ID' in 'on clause'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:116)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:80)
... 13 more
Java Result: 1

0 个答案:

没有答案