简体   繁体   中英

Hibernate HQL Alias Issue

MainTable.java extends Common.java

private Long id ;
private Long version ; 
private String name ;
private SubTable sub ;


SubTable.java extends Common.java

private String subname ;
prviate String dualname ; 


Common.java

private Long id ;
prviate Date createDate ;


HQL
v
String sql = "update MainTable set name = ?  where sub.id = ? and version = ?" ; 
Query query = session..createQuery(sql);
// set paramerts
query.executeUpdate();

Hibernate Generated SQL

update MainTable set name =? where templateve0_.SUB_ID=? and version =?

Error

ERROR org.hibernate.util.JDBCExceptionReporter - ORA-00904: "TEMPLATEVE0_"."SUB_ID": invalid identifier

FYI - SUB_ID is a valid column name.

I am not sure why is hibernate adding templateve0_ alias only for the sub-object. Any help?

Original - String sql = "update MainTable set name = ? where sub.id = ? and version = ?" ;

Updated String sql = "update MainTable set name = ? where SUB_ID = ? and version = ?" ;

I replaced sub.id with the actual column name and it seems to be working! Very Strange!

Try adding an alias to the root entity:

update MainTable m set m.name = :name where m.sub.id = :id and m.version = :version

(NOTE: I prefer named vars to ordinal ones)

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