0

我在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

请告知问题出在哪里以及如何纠正这个无效的映射异常

4

2 回答 2

0

您命名了您的文件pkfk.hbm.xml,Hibernate 尝试读取samePK.hbm.xml。您的主配置文件一定是错误的。

于 2012-07-29T12:21:52.150 回答
0

如果有人在使用 spring 和 hibernate 集成时遇到无效映射异常,请仔细检查 springbean.xml 文件中的拼写错误(无论你给出什么)然后仔细检查 hibernate.hbm.xml 文件 bcs,因为我认为大多数时候我们遇到的问题只是拼写错误的原因。1)hbm文件对应的变量名正确;2)所有getter/setter是否存在。3)当你使用ClassPathXmlApplicationContext加载 springbean.xml 文件时,在构造函数中传递,然后检查其拼写名称。4)id标签必须是。它的类型是String或者int,long任意一个k

这是我与你分享的经验。我希望这对所有人有用。

于 2013-06-16T08:03:02.407 回答