Hibernate + Maven, Mapping files with XML

Continuing with the Hibernate and Maven topic.
Now let’s map the DB tables using xml files.
Attached also a sample of a DB that we will also develop gradually.BD1
We create a class in a package that I call pojo, there I create the class “Alumno”

/**
*
*/
package org.apb.pojo;

import java.sql.Timestamp;

/**
* @author Alvaro Peredo
*
*/
public class Alumno {

public int idAlumno;
public String nombre;
public String apellidoPaterno;
public Timestamp fechaAlta;
/**
* @param nombre
* @param apellidoPaterno
* @param fechaAlta
*/
public Alumno(String nombre, String apellidoPaterno, Timestamp fechaAlta) {

this.nombre = nombre;
this.apellidoPaterno = apellidoPaterno;
this.fechaAlta = fechaAlta;
}
/**
*
*/
public Alumno() {
// TODO Auto-generated constructor stub
}
/**
* @return the idAlumno
*/
public int getIdAlumno() {
return idAlumno;
}
/**
* @param idAlumno the idAlumno to set
*/
public void setIdAlumno(int idAlumno) {
this.idAlumno = idAlumno;
}
/**
* @return the nombre
*/
public String getNombre() {
return nombre;
}
/**
* @param nombre the nombre to set
*/
public void setNombre(String nombre) {
this.nombre = nombre;
}
/**
* @return the apellidoPaterno
*/
public String getApellidoPaterno() {
return apellidoPaterno;
}
/**
* @param apellidoPaterno the apellidoPaterno to set
*/
public void setApellidoPaterno(String apellidoPaterno) {
this.apellidoPaterno = apellidoPaterno;
}
/**
* @return the fechaAlta
*/
public Timestamp getFechaAlta() {
return fechaAlta;
}
/**
* @param fechaAlta the fechaAlta to set
*/
public void setFechaAlta(Timestamp fechaAlta) {
this.fechaAlta = fechaAlta;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return “Alumno [idAlumno=” + idAlumno + “, nombre=” + nombre + “, apellidoPaterno=” + apellidoPaterno
+ “, fechaAlta=” + fechaAlta + “]”;
}

}

We must write an xml file, which refers to this class, so we can build an xml with Eclipse or install from the market Jboss Tools or Red Hat JBoss Developer Studio, I installed this add-on and I will use it to exemplify its use.mapping file xmlmapping file xml1mapping file xml2.PNG

Once pressed the button “Finish” will show us the following code that has generatedmapping file xml3The file is named: “Alumno.hbm.xml”

We created a class (test) in the package org.apb.test to insert data in the table “Students” and in another table that I have in the DB, you can simply comment the line of the insertion of the other table to execute this code.mapping file xml4

Before running this class we must add this line <mapping resource = “org/apb/pojo/ Alumno.hbm.xml” /> before the </ session-factory> to the hibernate.cfg.xml file.

If you run the “test” class and check your BD you will see that a record was inserted.

This project can be found in this link to download it.

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s