Hibernate:需要 AnnotationConfiguration 实例才能使用...错误
我这样构建 HibernateUtil :
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml) config file.
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
因此,当我尝试在 Eclipse 中的 HQL 编辑器中执行 HQL 命令(使用 Hibernate Tools)时,会出现以下错误: 为什么会出现这种情况?它不应该通过 ConfigureAnnotation 更改 AnnotationConfiguration 吗?
更新
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password"><password></property>
<property name="hibernate.connection.url">jdbc:mysql://<hostname>:3306/<schema></property>
<property name="hibernate.connection.username">root</property>
<!-- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- SQL -->
<property name="hibernate.format_sql">true</property>
<property name="hibernate.show_sql">true</property>
<!-- C3P0 -->
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.timeout">180</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<!-- Classes -->
<mapping class="com.suaparte.pojo.Area" />
</session-factory>
</hibernate-configuration>
提前致谢。
I build my HibernateUtil this way :
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml) config file.
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
So when I try to execute the HQL command in HQL Editor in Eclipse (with Hibernate Tools) gives the follow error:
Why this happening ? It shouln't change the AnnotationConfiguration by ConfigureAnnotation ?
UPDATE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password"><password></property>
<property name="hibernate.connection.url">jdbc:mysql://<hostname>:3306/<schema></property>
<property name="hibernate.connection.username">root</property>
<!-- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- SQL -->
<property name="hibernate.format_sql">true</property>
<property name="hibernate.show_sql">true</property>
<!-- C3P0 -->
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.timeout">180</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<!-- Classes -->
<mapping class="com.suaparte.pojo.Area" />
</session-factory>
</hibernate-configuration>
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果你有这个错误,
并且您使用的 hibernate 版本 >=4.0,问题可能出在 Hibernate 控制台配置中。
尝试转到:
运行->运行配置
并打开您创建的配置,在主选项卡上将类型从核心更改为注释。这里是一个截图:
If you have that error,
and you are using hibernate version >=4.0, the problem probably is in the Hibernate Console configuration.
Try to go to:
Run -> Run Configurations
and open the configuration that you have created, On the main tab change Type from Core to Annotations. Here a screenshot:
只需将 Configuration() 更改为 AnnotationConfiguration()
Just change Configuration() to AnnotationConfiguration()
我已将代码从 更改为
另外
,我还没有在 POJO 类中添加“@Id”注释。添加“@Id”后我完全解决了我的问题。
I have changed my code from
To
Also I havent added "@Id" annotations in my POJO class. After adding "@Id" I resolved my problem completly.
您可以使用 AnnotationConfiguration() 而不是 Configuration()
you can use AnnotationConfiguration() instead of Configuration()
尝试构建如下:
为此,您需要这个:
问候。
乌多.
Try to build like:
To do so, you need this:
Regards.
Udo.
尝试下载hibernate distribution jars 3.6.4版本。,使用jre1.6.0_07版本。
通过这样做,您可以成功创建新的配置对象,如下所示,而不是使用
AnnotationConfiguration()
。Try to download the hibernate distribution jars 3.6.4 version., use jre1.6.0_07 version.
by doing this, you can successfully create new configuration object as below instead of using
AnnotationConfiguration()
.尝试将 Configuration().configure().buildSessionFactory() 更改为 AnnotationConfiguration().configure().buildSessionFactory() 并在类路径中包含 hibernate-annotations.jar
Try changing Configuration().configure().buildSessionFactory() into AnnotationConfiguration().configure().buildSessionFactory() and include hibernate-annotations.jar in the class path
使用 Hibernate 最新版本 jar 的 5.x 或更高版本
Use Hibernate latest version jar's 5.x or above
创建一个返回 SessionFactory 对象的实用程序类:
并在主类中调用它,如下所示:
Create a utility class which return a
SessionFactory
object:and call this in your main class like this: