读书人

使用hbiernate出现的Error reading re

发布时间: 2012-01-21 21:31:43 作者: rapoo

使用hbiernate出现的Error reading resource
有两个表,student(person id,addressid),address(addressid)

StudentPo类:
package com.lovo.po;
public class StudentPo {
private int personid;
private AddressPo address;

public int getPersonid() {
return personid;
}
public void setPersonid(int personid) {
this.personid = personid;
}
public AddressPo getAddress() {
return address;
}
public void setAddress(AddressPo address) {
this.address = address;
}
}

AddressPo类:
package com.lovo.po;
public class AddressPo {

private int addressid;

public int getAddressid() {
return addressid;
}

public void setAddressid(int addressid) {
this.addressid = addressid;
}
}

StudentPo.hbm.xml:

<?xml version= "1.0 " encoding= "UTF-8 "?>
<!DOCTYPE RootElement PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN " "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
<hibernate-mapping>
<class name= "com.lovo.po.StudentPo " table= "student ">
<id name= "personid " column= "personid ">
<generator class = "increment "/>
</id>
<one-to-one name= "address " class= "AddressPo " cascade= "all "> </one-to-one>
</class>
</hibernate-mapping>

AddressPo.hbm.xml:
<?xml version= "1.0 " encoding= "UTF-8 "?>
<!DOCTYPE RootElement PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN " "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
<hibernate-mapping>
<class name= "com.lovo.po.AddressPo " table= "address ">
<id name= "addressid " column= "addressid ">
<generator class= "foreign ">
<param name= "property "> student </param>
</generator>
</id>
<one-to-one name= "student " constrained= "true "> </one-to-one>
</class>
</hibernate-mapping>

hibernate.cfg.xml:
<?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 ">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name= "connection.username "> root </property>


<property name= "connection.url "> jdbc:mysql://127.0.0.1:3306/test </property>
<property name= "dialect "> org.hibernate.dialect.MySQLDialect </property>
<property name= "myeclipse.connection.profile "> Mysql </property>
<property name= "connection.password "> 123 </property>
<property name= "connection.driver_class "> com.mysql.jdbc.Driver </property>
<mapping resource= "StudentPo.hbm.xml "/>
<mapping resource= "AddressPo.hbm.xml "/>
</session-factory>

</hibernate-configuration>

我是想用一对一主键关联

写了个测试类
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.lovo.po.AddressPo;
import com.lovo.po.StudentPo;


public class demo
{
public static void main(String[]args)
{
Configuration config = new Configuration().configure();
SessionFactory sf = config.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();

StudentPo sp = new StudentPo();
sp.setPersonid(1);
AddressPo ap = new AddressPo();
ap.setAddressid(2);
tx.commit();

session.save(ap);
session.close();

}
}

在运行的事后报错:
Exception in thread "main " org.hibernate.MappingException: Error reading resource: StudentPo.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:452)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1263)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1235)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1217)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1184)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1112)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1098)
at demo.main(demo.java:14)
Caused by: org.hibernate.MappingException: invalid mapping
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:399)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:449)
... 7 more
Caused by: org.xml.sax.SAXParseException: Document root element "hibernate-mapping ", must match DOCTYPE root "RootElement ".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)


at org.apache.xerces.impl.dtd.XMLDTDValidator.rootElementSpecified(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:398)
... 8 more


[解决办法]
在AddressPo类中,要提供parm参数student的get,set方法,另外在one-to-one中class要用全名的吧

读书人网 >J2SE开发

热点推荐