0
votes

i need help with hibernate mapping one-to-many i don´t know why this is wrong

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 + "]";
}
}

another class:

 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 + "]";
    }
    }

the 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.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>

another 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 configuration

    <?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>

this is the error:

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)

if anyone can help me with that i´m so grateful thanks!

2
Exception in thread "main" java.lang.NullPointerException at dao.DireccionDao.traerListaRubros(DireccionDao.java:128) What happens at line 128 of DireccionDao? - Jim Garrison

2 Answers

0
votes

If you are mapping one Direccion to many Personas, this is a one-to-many mapping. This also means that the Persona has a many-to-one relationship with Direccion. You have a List<Persona> variable under the Direccion class. Now you need to have a Direccion class variable for the Persona class.

Here is an example of hibernate configuration with xml. http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example/

0
votes

some times in Init-param and context-param you try to give same param-value that's why it is giving error.try to remove one.

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