Hibernate报错Could not parse mapping document from resource

J2EE 码拜 10年前 (2015-04-17) 1771次浏览 0个评论
 

报错信息:
Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Could not parse mapping document from resource cn/jbit/pojo/User.hbm.xml

user实体类

public class User {
   private Integer id;
   private String username;
   private String password;
   private Date   birthday;
   private Integer age;
  
   private Address address;
public Address getAddress() {
	return address;
}
public void setAddress(Address address) {
	this.address = address;
}
public Integer getId() {
	return id;
}
public void setId(Integer 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 Date getBirthday() {
	return birthday;
}
public void setBirthday(Date birthday) {
	this.birthday = birthday;
}
public Integer getAge() {
	return age;
}
public void setAge(Integer age) {
	this.age = age;
}


   
}

Address实体类

package cn.jbit.pojo;
public class Address {
	private User user;
    public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	private Integer id;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	private String country;
    private String city;
    
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
}

user映射文件 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  
	<class name="cn.jbit.pojo.User" table="userinfo" >
	<id name="id" type="java.lang.Integer" >
	<column name="id"></column>
	<generator class="sequence">
	<param name="sequence">seq1</param>
	</generator>
	</id>
	<property name="userName"></property>
	<property name="password"></property>
	<property name="birthday" type="java.util.Date"></property>
	<property name="age" type="java.lang.Integer"></property>

	<one-to-one name="address" class="cn.jbit.pojo.Address" property-ref="user">
	</one-to-one>
	</class>
</hibernate-mapping>

Address映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    
	<class name="cn.jbit.pojo.Address" table="Address" >
	<id name="id" type="java.lang.Integer" >
	<generator class="assigned">
	</generator>
	</id>
	<property name="country"></property>
	<property name="city"></property>
	<many-to-one name="user" class="cn.jbit.pojo.User" unique="true">
	<column name="userid"></column>
	</many-to-one>
	</class>
</hibernate-mapping>

配置文件

<?xml version=""1.0"" encoding=""utf-8""?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
	<property name="hibernate.connection.url">
		jdbc:oracle:thin:@localhost:1521:ZXC
	</property>
	<property name="hibernate.connection.username">test</property>
	<property name="hibernate.connection.password">test</property>
	<property name="hibernate.connection.driver_class">
		oracle.jdbc.driver.OracleDriver
	</property>
	<property name="show_sql">true</property>
	<!--<property name="dialect">org.hibernate.dialect.HSQLDialect</property>          
	-->
	<mapping resource="cn/jbit/pojo/User.hbm.xml" />
	<mapping resource="cn/jbit/pojo/Address.hbm.xml" />
</session-factory>
</hibernate-configuration>

Hibernate报错Could not parse mapping document from resource
20分
User.hbm.xml  配置出了问题 

我觉得是你那个one-to-one 不对劲吧,你address配置的是many-to-one

Hibernate报错Could not parse mapping document from resource
还有,看看你的userinfo的表名,是不是这个影响了,你自己看看吧,反正是User.hbm.xml有问题就对了
Hibernate报错Could not parse mapping document from resource
User.hbm.xml  里面的id怎么还套了个column标签,不对吧,貌似要写到id标签里面,试试看。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Hibernate报错Could not parse mapping document from resource
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!