Hibernate加载配置文件的两种方法:
(一):configuration.configure();(默认加载src下的hibernate.cfg.xml文件)
代码:
private static SessionFactory sf = null;
static{
Configuration configuration = new Configuration();
configuration.configure();
sf = configuration.buildSessionFactory();
}
如果使用了这种加载方法那么在hibernate.cfg.xml文件中就需要mapping XX.hbm.xml文件了
代码:
<session-factory>
<mapping resource="domain/Students.hbm.xml"/>
<mapping resource="domain/Techer.hbm.xml"/>
</session-factory>
第二种方法使用的是con.addClass(class);方法
在test类中代码:
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了。
如果既使用了addClass方法也在cfg.xml文件中添加了mapping就会如下错误
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 more