基于注解的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();
}
}
}
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)
很是郁闷啊,这是为什呢??
你用的注解配置,hibernate3.3需把Configuration换成AnnotationConfiguration。
不过更建议你至少升级Hibernate到3.6,3.6之后AnnotationConfiguration的功能合并到Configuration中,被标明过时了。
.reverng.xml是一些数据库数据类型和Java数据类型的映射文件,比如VARCHAR映射成Java的String。
还在用3.2的飘过,不建议使用新版本。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。