首页 新闻 会员 周边

在Hibernate中我想做一个测试,同时构建XML的sessionFactory和Annoation的SessionFactory但失败了

0
悬赏园豆:20 [已关闭问题] 关闭于 2013-02-03 00:47

我想做两种实现,一个是用XML写的hbm.xml 另一种是采用Annoation版本的实现。我利用单例模式分别构建了sessionFactory_Anno和sessionFactory_XML两个session工厂。可是当我运行测试发现,总是报create sessionFactory failed An AnnotationConfiguration instance is required to use <mapping class="com.sane.model.Teacher"/>我在hibernate.cfg.xml中已经针对Teacher类 做了映射。为什么两个工厂不能同时创建?请大牛指点!!!

 

 1  hibernate.cfg.xml 文件如下:

<?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 8 <session-factory> 9 <!-- 数据库配置 --> 10 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 11 <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> 12 <property name="connection.username">scott</property> 13 <property name="connection.password">scott</property> 14 15 <!-- 连接池 --> 16 <!--<property name="connection.pool_size">1</property>--> 17 18 <!-- 方言 --> 19 <property name="dialect">org.hibernate.dialect.OracleDialect</property> 20 21 <!-- Enable Hibernate's automatic session context management --> 22 <!--<property name="current_session_context_class">thread</property>--> 23 24 <!-- 禁用二级缓存 --> 25 <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 26 27 <!-- 输出所有执行的Sql --> 28 <property name="show_sql">true</property> 29 30 <!-- 格式化SQL语句 --> 31 <property name="format_sql">true</property> 32 33 <!-- 自动生成建表语句 --> 34 <property name="hbm2ddl.auto">update</property> 35 36 <!-- 实体类配置文件列表 --> 37 <mapping class="com.sane.model.Teacher"/> 38 <mapping resource="com/sane/model/Student.hbm.xml"/> 39 40 </session-factory> 41 42 </hibernate-configuration>
单例构建代码:
package
com.sane.util; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; /** * * @author mys * create sessionFactory * */ public class HibernateUtil { private static final SessionFactory sessionFactory_Xml=buildSessionFactory_Xml(); private static final SessionFactory sessionFactory_Anno=buildSessionFactory_Anno(); private static SessionFactory buildSessionFactory_Anno(){ try { return new AnnotationConfiguration().configure().buildSessionFactory();//Annotation构建 } catch (Throwable e) { System.out.println("create sessionFactory failed"+e.getMessage()); throw new ExceptionInInitializerError(e); } } private static SessionFactory buildSessionFactory_Xml(){ try { return new Configuration().configure().buildSessionFactory();//xml构建 } catch (Throwable e) { System.out.println("create sessionFactory failed "+e.getMessage()); throw new ExceptionInInitializerError(e); } } /** * * @return SessionFactory */ public static SessionFactory getSessionFactory_Xml() { return sessionFactory_Xml; } public static SessionFactory getSessionFactory_Anno() { return sessionFactory_Anno; } }
sanemu的主页 sanemu | 初学一级 | 园豆:178
提问于:2013-01-10 22:33
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册