我试图在Hibernate中注册自定义方言。我找到了大量的例子,我以为我有它 - 现在它告诉我该功能在我的数据库中不存在。具体来说,我试图为MySQL 5.6创建date_add()方言。我正在使用hibernate 3.6.7。
在hibernate.cfg.xml中:
<hibernate-configuration>
<session-factory>
<property name="dialect">com.myPackage.CustomMySQLDialect</property>
</session-factory>
</hibernate-configuration>
在com.myPackage.CustomMySQLDialect中:
public class CustomMySQLDialect extends MySQL5InnoDBDialect {
public CustomMySQLDialect() {
super();
registerFunction( "date_add_interval", new SQLFunctionTemplate( Hibernate.DATE, "date_add(?1, interval ?2 ?3)" ) );
}
}
当我在DAOImpl类中调用它时:
List<User> userList = null;
Query confirmationUser = session.getCurrentSession().createQuery("from InitUser where enabled = 0 and date_add_interval(created_date, 1, day) < now() and end_date is null");
userList = confirmationUser.list(); // <-- this is where it dies
这是例外:
Hibernate: select inituser0_.init_user_id as init1_58_, inituser0_.confirmation_date as confirma2_58_, inituser0_.confirmation_sent_date as confirma3_58_, inituser0_.confirmation_token as confirma4_58_, inituser0_.created_date as created5_58_, inituser0_.current_sign_in_date as current6_58_, inituser0_.current_sign_in_ip as current7_58_, inituser0_.email as email58_, inituser0_.enabled as enabled58_, inituser0_.end_date as end10_58_, inituser0_.failed_sign_in_count as failed11_58_, inituser0_.last_sign_in_date as last12_58_, inituser0_.last_sign_in_ip as last13_58_, inituser0_.password as password58_, inituser0_.remember_created_date as remember15_58_, inituser0_.reset_password_confirmation_date as reset16_58_, inituser0_.reset_password_sent_date as reset17_58_, inituser0_.reset_password_token as reset18_58_, inituser0_.sign_in_count as sign19_58_, inituser0_.updated_by as updated20_58_, inituser0_.updated_date as updated21_58_, inituser0_.username as username58_ from InitUser inituser0_ where inituser0_.end_date is null
Hibernate: select inituser0_.init_user_id as init1_58_, inituser0_.confirmation_date as confirma2_58_, inituser0_.confirmation_sent_date as confirma3_58_, inituser0_.confirmation_token as confirma4_58_, inituser0_.created_date as created5_58_, inituser0_.current_sign_in_date as current6_58_, inituser0_.current_sign_in_ip as current7_58_, inituser0_.email as email58_, inituser0_.enabled as enabled58_, inituser0_.end_date as end10_58_, inituser0_.failed_sign_in_count as failed11_58_, inituser0_.last_sign_in_date as last12_58_, inituser0_.last_sign_in_ip as last13_58_, inituser0_.password as password58_, inituser0_.remember_created_date as remember15_58_, inituser0_.reset_password_confirmation_date as reset16_58_, inituser0_.reset_password_sent_date as reset17_58_, inituser0_.reset_password_token as reset18_58_, inituser0_.sign_in_count as sign19_58_, inituser0_.updated_by as updated20_58_, inituser0_.updated_date as updated21_58_, inituser0_.username as username58_ from InitUser inituser0_ where inituser0_.enabled=0 and date_add_interval(inituser0_.created_date, 1, day)<now() and (inituser0_.end_date is null)
WARN : org.hibernate.util.JDBCExceptionReporter - SQL Error: 1305, SQLState: 42000
ERROR: org.hibernate.util.JDBCExceptionReporter - FUNCTION myDBName.date_add_interval does not exist
ERROR: com.somePackage.InitUserDAOImpl - could not execute query
它超越了查询的创建,然后尝试执行它,然后抱怨它无法找到该功能。它在数据库名称的前缀中指定数据库的名称。
这是否未正确注册?我应该指定myDBName作为要注册的数据库吗?如果是这样,怎么样?
或者我离开了?
答案 0 :(得分:0)
最后,我使用Joda进行时间计算并将其预先计算好。一方面,我不得不重新做一堆代码来支持这个。另一方面,我一直在使用它,并且感觉不快乐。