org.hibernate.InvalidMappingException:无法读取XML

时间:2014-06-21 21:09:43

标签: java hibernate hibernate-mapping

我需要帮助hibernate映射一对多 我不知道为什么这是错的

public class Direccion {

private long id;
private String calle;
private int numero;
private List<Persona> habitantes;


public Direccion(){
}
public Direccion(long id, String calle, int numero, List<Persona> habitante) {
    super();
    this.id = id;
    this.calle = calle;
    this.numero = numero;
    this.habitantes = habitante;
}
public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
public String getCalle() {
    return calle;
}
public void setCalle(String calle) {
    this.calle = calle;
}
public int getNumero() {
    return numero;
}
public void setNumero(int numero) {
    this.numero = numero;
}
public List<Persona> getHabitantes() {
    return habitantes;
}
public void setHabitantes(List<Persona> habitantes) {
    this.habitantes = habitantes;
}
@Override
public String toString() {
    return "Direccion [id=" + id + ", calle=" + calle + ", numero="
            + numero + ", habitantes=" + habitantes + "]";
}
}

另一堂课:

 public class Persona {

    private long id;
    private long nombre;


    public Persona(){   
    }
    public Persona(long id, long nombre) {
        super();
        this.id = id;
        this.nombre = nombre;
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public long getNombre() {
        return nombre;
    }
    public void setNombre(long nombre) {
        this.nombre = nombre;
    }
    @Override
    public String toString() {
        return "Persona [id=" + id + ", nombre=" + nombre + "]";
    }
    }

映射

<?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="datos.Direccion" table="direccion">
        <id column="id" name="id">
            <generator class="identity">
              </generator>
        </id>
        <property column="calle" name="calle" type="string" />
        <property name="numero" column="numero" type="int" />

        <set name="habitantes" table="persona" inverse="true" lazy="true" fetch="select">
        <key>
         <column name="idPersona" not-null="true" />
        </key>

        <one-to-many class="datos.Persona" />

    </class>
   </hibernate-mapping>

另一个映射:

    <?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="datos.Persona" table="persona">
        <id column="id" name="id">
            <generator class="identity"/>
        </id>
        <property column="nombre" name="nombre" type="string" />

        <many-to-one name="direccion" class="datos.Direccion" column="idPersona" not-null="true"/>

 </set>
    </class>
    </hibernate-mapping>

hibernate配置

    <?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="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/repasohibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.pool_size">1</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property> <!-- en true muestra hql en consola -->
        <!--Mapeo Entidades -->
        <mapping resource="mapeos/persona.hbm.xml" />
        <mapping resource="mapeos/direccion.hbm.xml" />
    </session-factory>
   </hibernate-configuration>

这是错误:

jun 21, 2014 6:11:51 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
jun 21, 2014 6:11:51 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.9.Final}
jun 21, 2014 6:11:51 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
jun 21, 2014 6:11:51 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
jun 21, 2014 6:11:51 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
jun 21, 2014 6:11:51 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
jun 21, 2014 6:11:51 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
jun 21, 2014 6:11:51 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: mapeos/persona.hbm.xml
jun 21, 2014 6:11:51 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
ERROR en la inicialización de la SessionFactory: org.hibernate.InvalidMappingException: Unable to read XML
Exception in thread "main" java.lang.NullPointerException
    at dao.DireccionDao.traerListaRubros(DireccionDao.java:128)
    at prueba.prueba.main(prueba.java:12)

如果有人能帮助我,我会非常感激 谢谢!

2 个答案:

答案 0 :(得分:0)

如果要将一个Direccion映射到多个Personas,这是一对多映射。这也意味着Persona与Direccion有多对一的关系。你在Direccion类下有一个List<Persona>变量。现在你需要为Persona类设置一个Direccion类变量。

以下是使用xml进行hibernate配置的示例。 http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example/

答案 1 :(得分:0)

在Init-param和context-param中有时会尝试给出相同的param值,这就是为什么它会给出error.try来删除它。

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <display-name>Sample Spring Maven Project</display-name>

<context-param>
        <param-name>log4jConfigLocation</param-name>
            <param-value>/WEB-INF/spring-config.xml,
            /WEB-INF/applicationContext-datasource.xml,
            /WEB-INF/applicationContext-hibernate.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <!--    
I have removed this one.
<init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-config.xml,
            /WEB-INF/applicationContext-datasource.xml,
            /WEB-INF/applicationContext-hibernate.xml</param-value>
        </init-param> -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>