hibernate 测试报错

zerohh 发布于 2013/03/26 21:48
阅读 241
收藏 0

基于注解的entity hibernate测试例子:

项目目录截图:

话说最后那个 .reverng.xml是干什么的呢?

下面是.cfg.xml的配置代码:

<?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">


<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>


<session-factory>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.url">
jdbc:sqlserver://127.0.0.1:1433;databaseName=MyBookShop
</property>
<property name="connection.username">sa</property>
<property name="connection.password">hsf115</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="myeclipse.connection.profile">
dataconn
</property>
<property name="show_sql">true</property>
<mapping class="net.hsf.entity.SearchKeywords" />
<mapping class="net.hsf.entity.users" />
<mapping class="net.hsf.entity.OrderBook" />
<mapping class="net.hsf.entity.Orders" />
<mapping class="net.hsf.entity.sysfun" />
<mapping class="net.hsf.entity.Publishers" />
<mapping class="net.hsf.entity.Categories" />
<mapping class="net.hsf.entity.userstates" />
<mapping class="net.hsf.entity.sysdiagrams" />
<mapping class="net.hsf.entity.scores" />
<mapping class="net.hsf.entity.userroles" />
<mapping class="net.hsf.entity.Books" />
<mapping class="net.hsf.entity.ReaderComments" />
<mapping class="net.hsf.entity.BookRatigs" />
<mapping class="net.hsf.entity.scores" />
</session-factory>
</hibernate-configuration>
下面是HelloHIbernate测试类:

package com.hsf.test;


import net.hsf.entity.userstates;


import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class HibernateTest {

  public static void main(String[] args) {
 try {
          SessionFactory sf = new Configuration().configure().buildSessionFactory();
          Session session = sf.openSession();
          Transaction tx = session.beginTransaction();
          for (int i = 0; i < 3; i++) {
         userstates users = new userstates();
              users.setName("泪水");
              session.save(users);
          }
          tx.commit();
          session.close();
      } catch (HibernateException e) {
          e.printStackTrace();
      }
}

}


一run就报如下错误:

org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="net.hsf.entity.SearchKeywords"/>
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1648)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1603)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1582)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1556)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
at com.hsf.test.HibernateTest.main(HibernateTest.java:15)


很是郁闷啊,这是为什呢??

加载中
0
逝水fox
逝水fox

你用的注解配置,hibernate3.3需把Configuration换成AnnotationConfiguration。

不过更建议你至少升级Hibernate到3.6,3.6之后AnnotationConfiguration的功能合并到Configuration中,被标明过时了。

0
Wentasy
Wentasy

.reverng.xml是一些数据库数据类型和Java数据类型的映射文件,比如VARCHAR映射成Java的String。

还在用3.2的飘过,不建议使用新版本。

OSCHINA
登录后可查看更多优质内容
返回顶部
顶部