开发者社区> 问答> 正文

hibernate 测试报错?报错

基于注解的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)


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

展开
收起
爱吃鱼的程序员 2020-06-22 21:00:40 410 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

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

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

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

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

    2020-06-22 21:00:59
    赞同 1 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
移动互联网测试到质量的转变 立即下载
给ITer的技术实战进阶课-阿里CIO学院独家教材(四) 立即下载
F2etest — 多浏览器兼容性测试整体解决方案 立即下载