Hibernate和Mysql结合开发注意事项

mysql在hibernate中使用原生态sql注意事项:
org.hibernate.Query q = session.createSQLQuery("select a.name,b.name from table1 a,table b where a.id=b.id");
1、包含子查询的必须给子查询的整体一个别名;
2、子查询中的别名和不能能和外层查询冲突;
3、当查询的自动有重名时,需要字段别名,然后给org.hibernate.Query需要addScalar方式去区分,否则获取的值永远是第一个比如上边的例子可以写成
org.hibernate.Query q = session.createSQLQuery("select a.name namea,b.name nameb from table1 a,table b where a.id=b.id").addScalar("namea").addScalar("nameb");
4、数据库方言必须为org.hibernate.dialect.MySQL5InnoDBDialect。

猜你喜欢

转载自xyzxingyan.iteye.com/blog/1666923