hibernate加載配置文件的兩種方法:java
(一):configuration.configure();(默認加載src下的hibernate.cfg.xml文件)session
代碼:app
private static SessionFactory sf = null; static{ Configuration configuration = new Configuration(); configuration.configure(); sf = configuration.buildSessionFactory(); }
若是使用了這種加載方法那麼在hibernate.cfg.xml文件中就須要<mapping>XX.hbm.xml文件了dom
代碼:eclipse
<mapping resource="domain/Students.hbm.xml"/> <mapping resource="domain/Techer.hbm.xml"/> </session-factory>
第二種方法使用的是con.addClass(class);方法ui
在test類中代碼:lua
private static SessionFactory sf = null; static{ Configuration configuration = new Configuration(); configuration.configure(); configuration.addClass(Techer.class); configuration.addClass(Students.class); sf = configuration.buildSessionFactory(); }
若是使用了這種那麼在hibernate.cfg.xml文件中就不須要<mapping>了。hibernate
若是既使用了addClass方法也在cfg.xml文件中添加了mapping就會以下錯誤code
java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:209)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:258)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:255)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource domain/Techer.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:641)
at org.hibernate.cfg.Configuration.addClass(Configuration.java:688)
at dao.App.<clinit>(App.java:17)
... 23 more
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping domain.Techer
at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:2407)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:173)
at org.hibernate.cfg.Configuration.add(Configuration.java:771)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:606)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:638)
... 25 morexml