我在Maven项目中使用Hibernate。 DBMS是MySQL。库按如下方式导入到pom中:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.2.0.Final</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>
现在,当我尝试使用Netbeans运行简单的HQL查询(即from User
)时,我收到以下错误:
org.hibernate.HibernateException: could not instantiate QueryTranslatorFactory: org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory
at org.hibernate.cfg.SettingsFactory.createQueryTranslatorFactory(SettingsFactory.java:436)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:258)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2863)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2859)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1870)
Caused by: java.lang.ClassCastException: org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory cannot be cast to org.hibernate.hql.QueryTranslatorFactory
at org.hibernate.cfg.SettingsFactory.createQueryTranslatorFactory(SettingsFactory.java:433)
... 7 more
最奇怪的是我看到了ClassCastException
org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory
。我从here看到hibernate.cfg.xml
hibernate.query.factory_class
的正确配置是:
<property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>
此外,我的hibernate.cfg.xml
配置文件是:
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/datadb?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>
<mapping resource="edu/project/entities/User.hbm.xml"/>
<mapping resource="edu/project/entities/Code.hbm.xml"/>
<mapping resource="edu/project/entities/Incident.hbm.xml"/>
</session-factory>
</hibernate-configuration>
怎么了?
PS:MySQL服务器在线。此外,我尝试了其他查询,例如select u from User
。