我想在我的HQL查詢使用自定義功能,我在數據庫中註冊的功能,這是我的函數的SQL代碼:如何使用Hibernate寄存器功能在HQL查詢
BEGIN
DECLARE user_id_var VARCHAR(64);
SELECT
e.username
FROM
users e where e.id=30 INTO user_id_var;
return user_id_var;
END
並註冊一類爲MysqlCustomDilect:
public class MysqlCustomDilect extends MySQLDialect{
public MysqlCustomDilect() {
super();
registerFunction("getActiveUser", new StandardSQLFunction("getActiveUser"));
}
}
,並用這條線把它添加到hibernate.cfg.xml文件:
<property name="hibernate.dialect" value="myProject.common.MysqlCustomDilect" />
並調用它像這樣的代碼在我DAO層:
@Override
public List<Entity> getAll() {
Session session = getSession();
String hql = " select e.id as id,function('getActiveUser') as name from " + domainClass.getName() + " e ";
Query query=session.createQuery(hql);
return query.list();
}
但Hibernate不知道它,提出這個錯誤:
unexpected token: function near line 1, column 18 [ from e.id as id,function('getActiveUser') ........
這是歪曲,我修復它,但它不是那個 –