1.报错信息:(1)初始化错误,(2)映射异常,(3)属性找不到
信息: Mapping class: com.ever.compomer.Car -> t_car
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:526)
at org.junit.internal.runners.JUnit4ClassRunner.createTest(JUnit4ClassRunner.java:72)
at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:79)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
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:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/ever/compomer/Car.hbm.xml
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3951)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3940)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3928)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1368)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1826)
at com.ever.compomer.App.<clinit>(App.java:14)
... 17 more
Caused by: org.hibernate.PropertyNotFoundException: field [Wheel] not found on com.ever.compomer.Car
at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:182)
at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:174)
at org.hibernate.property.DirectPropertyAccessor.getGetter(DirectPropertyAccessor.java:197)
at org.hibernate.util.ReflectHelper.getter(ReflectHelper.java:241)
at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:229)
at org.hibernate.cfg.HbmBinder.reflectedPropertyClass(HbmBinder.java:1780)
at org.hibernate.cfg.HbmBinder.bindComponent(HbmBinder.java:1892)
at org.hibernate.cfg.HbmBinder.createClassProperties(HbmBinder.java:2184)
at org.hibernate.cfg.HbmBinder.createClassProperties(HbmBinder.java:2134)
at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:409)
at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:323)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:174)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3948)
... 22 more
************************************************************以下是正确的代码**************************************************
Car实体类:
package com.ever.component;
public class Car {
private int id;
private String name;
private Wheel wheel;
public Wheel getWheel() {
return wheel;
}
public void setWheel(Wheel wheel) {
this.wheel = wheel;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Wheel实体类:
package com.ever.component;
public class Wheel {
private int count;
private int size;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
映射配置文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Nov 9, 2006 6:27:53 PM by Hibernate Tools 3.2.0.beta7 -->
<hibernate-mapping package="com.ever.component">
<class name="Car" table="t_car">
<id name="id">
<generator class="native"/>
</id>
<property name="name" length="20"></property>
<component name="wheel">
<property name="size"></property>
<property name="count"></property>
</component>
</class>
</hibernate-mapping>
数据库连接配置:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库 连接的配置-->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///day16</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 显示mysql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 语句格式化 -->
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- 加载所有映射 -->
<!-- <mapping resource="com/ever/compositekey/user.hbm.xml"></mapping> -->
</session-factory>
</hibernate-configuration>
测试类:
package com.ever.component;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class App {
private static SessionFactory sf;
static{
sf = new Configuration()
.addClass(Car.class)
.configure()
.buildSessionFactory();
}
@Test
public void getSave(){
Session session = sf.openSession();
session.beginTransaction();
Wheel wheel = new Wheel();
wheel.setCount(4);
wheel.setSize(38);
Car car = new Car();
car.setName("BMW");
car.setWheel(wheel);
session.save(car);
session.getTransaction().commit();
session.close();
}
}
解决方法:把组件类在整体类中实现setter和getter方法即可,即添加wheel属性,然后实现setWheel和getWheel方法