我在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
请告知错误以及如何更正此无效的映射异常
答案 0 :(得分:0)
您将文件命名为pkfk.hbm.xml
,Hibernate尝试阅读samePK.hbm.xml
。您的主配置文件必须是错误的。
答案 1 :(得分:0)
如果有人在使用spring和hibernate集成时遇到Invalid Mapping Exception,请仔细检查springbean.xml文件中的拼写错误(无论你给出的是什么)然后仔细检查hibernate.hbm.xml文件bcs作为我的意见大多数时候我们面临的问题只是拼写错误的原因。 1)变量名正确对应hbm文件; 2)所有的getter / setter都在那里。 3)当您使用 ClassPathXmlApplicationContext 在构造函数中传递时加载springbean.xml文件,然后检查其拼写名称。 4)id标签必须是。键入String或int,long any k k
这是我与你分享的经验。我希望这对所有人都有用。