我在hibernate中开发了一个应用程序,下面是pojo,但是我遇到了映射异常,请告知
这是我的人pojo..
package mypack;
public class Person
{
private int personId;
private String name;
private Address address;
public Person()
{
super();
}
public Person(String name, Address address)
{
super();
this.name = name;
this.address = address;
}
public int getPersonId() {
return personId;
}
public void setPersonId(int personId) {
this.personId = personId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
地址 pojo 是
package mypack;
public class Address
{
private int id;
private String city,state;
public Address()
{
super();
}
public Address(String city, String state)
{
super();
this.city = city;
this.state = state;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
对应的hbm映射文件是pkfk.hbm.xml ..
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-mapping>
<class name="mypack.Person">
<id name="personId" type="int">
<generator class="increment"/>
</id>
<property name="name"/>
<many-to-one name="address" class="mypack.Address" column="addressId" unique="true" cascade="all"/>
</class>
<class name="mypack.Address">
<id name="id" column="addressId" type="int">
<generator class="increment"/>
</id>
<property name="city"/>
<property name="state"/>
</class>
</hibernate-mapping>
我的主程序文件是......
package mypack;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class TestProgram
{
public static void main(String[] args)
{
try
{
SessionFactory sesfac=new Configuration().configure().buildSessionFactory();
Session ses=sesfac.openSession();
Person p=new Person("Anupam",new Address("Noida","U.P"));
Person p1=new Person("Rekha Singh",new Address("Kanpur","U.P"));
System.out.println("Persisting Object...");
Transaction tx=ses.beginTransaction();
ses.save(p);
ses.save(p1);
tx.commit();
System.out.println("Successfully Persisted.");
ses.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
在执行主文件时,我收到以下错误..
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.InvalidMappingException: Could not parse mapping document from resource samePK.hbm.xml
请告知问题出在哪里以及如何纠正这个无效的映射异常