Hibernate加载配置文件的两种方法

(一):configuration.configure();(默认加载src下的hibernate.cfg.xml文件) 

1 private static SessionFactory sessionFactory = null;
2 
3 static{
4     Configuration configuration = new Configuration();
5     configuration.configure();
6     sessionFactory = configuration.buildSessionFactory();
7 }

如果使用了这种加载方法那么在hibernate.cfg.xml文件中就需要mapping XX.hbm.xml文件了

<session-factory>
    <mapping resource="domain/Students.hbm.xml"/>
    <mapping resource="domain/Techer.hbm.xml"/>
</session-factory>

(二):使用的是configuration.addClass(class);方法 

 1 private static SessionFactory sessionFactory = null;
 2 
 3 static {
 4 
 5     Configuration configuration = new Configuration();
 6     configuration.configure();
 7 
 8     configuration.addClass(Techer.class);
 9     configuration.addClass(Students.class);
10 
11     sessionFactory = configuration.buildSessionFactory();
12 }

 

如果使用了这种那么在hibernate.cfg.xml文件中就不需要mapping了。

如果既使用了addClass方法也在cfg.xml文件中添加了mapping就会如下错误:

 1 java.lang.ExceptionInInitializerError
 2  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 3  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
 4  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
 5  at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
 6  at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:209)
 7  at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:258)
 8  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
 9  at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:255)
10  at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
11  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
12  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
13  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
14  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
15  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
16  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
17  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
18  at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
19  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
20  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
21  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
22  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
23  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
24  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
25 Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource domain/Techer.hbm.xml
26  at org.hibernate.cfg.Configuration.addResource(Configuration.java:641)
27  at org.hibernate.cfg.Configuration.addClass(Configuration.java:688)
28  at dao.App.<clinit>(App.java:17)
29  ... 23 more
30 Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping domain.Techer
31  at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:2407)
32  at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:173)
33  at org.hibernate.cfg.Configuration.add(Configuration.java:771)
34  at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:606)
35  at org.hibernate.cfg.Configuration.addResource(Configuration.java:638)
36  ... 25 more

 

posted @ 2017-03-15 02:49  XH_Chiang  阅读(383)  评论(0编辑  收藏  举报