读书人

Hibernate 映射有关问题

发布时间: 2012-11-14 10:12:19 作者: rapoo

Hibernate 映射问题
Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/sh/ideal/pojo/Customer.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:616)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1635)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1603)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1582)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1556)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
at com.sh.ideal.test.BusinessService.addCustomer(BusinessService.java:36)
at com.sh.ideal.test.BusinessService.addCustomerAndOrder(BusinessService.java:26)
at com.sh.ideal.test.BusinessService.main(BusinessService.java:18)
Caused by: org.hibernate.MappingException: class Customer not found while looking for property: id
at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:232)
at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:302)
at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:423)
at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:356)
at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:295)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:166)
at org.hibernate.cfg.Configuration.add(Configuration.java:716)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:551)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:613)
... 9 more
Caused by: java.lang.ClassNotFoundException: Customer
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:192)
at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:228)
... 17 more

Customer类
public class Customer {

private String id;
private String userName;
private String password;
private String realName;
private String address;
private String mobile;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getRealName() {
return realName;
}

public void setRealName(String realName) {
this.realName = realName;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

public Customer(String userName, String password,
String realName, String address, String mobile) {

this.userName = userName;
this.password = password;
this.realName = realName;
this.address = address;
this.mobile = mobile;
}
}

Customer.hbm.xml
<class name="Customer" table="CUSTOMER">



<id name="id" column="ID">

<generator class="native"></generator>
</id>

<property name="userName" column="USERNAME" type="String"
not-null="true"></property>

<property name="password" column="PASSWORD" type="String"></property>

<property name="realName" column="REALNAME" type="String"></property>

<property name="address" column="ADDRESS" type="String"></property>

<property name="mobile" column="MOBILE" type="String"></property>


test.java测试类中:
public static void addCustomerAndOrder() {
System.out.println("111111111111111");
Customer customer = new Customer("zhangsan", "123456", "张三", "杭州",
"13012394853");

我用debug测试 到Customer这一行,创建对象就失败了。。。不知道为什么?请大神指教

[解决办法]
Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/sh/ideal/pojo/Customer.hbm.xml
找不到类,无法将将文档映射。
Caused by: java.lang.ClassNotFoundException: Customer

最好放在同一个目录下。
比如都防止entiry包下。。
[解决办法]
Could not parse mapping document from resource com/sh/ideal/pojo/Customer.hbm.xml

Caused by: java.lang.ClassNotFoundException: Customer

就是不能加载映射这个配置文件。一是你把文件写错了,读取不了,二是路径错误!你的这个问题就是路径错了

1.在这个里面属性name把包名加上后面<class name="Customer" table="CUSTOMER">

2.用package申明包路径

读书人网 >Java Web开发

热点推荐