Incluir migración MySQL a MongoDB
This commit is contained in:
parent
e322e8f4ca
commit
25e9e5e2ce
13
pom.xml
13
pom.xml
@ -6,6 +6,19 @@
|
||||
|
||||
|
||||
<dependencies>
|
||||
<!-- Hibernate Core -->
|
||||
<dependency>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>7.1.14.Final</version>
|
||||
</dependency>
|
||||
|
||||
<!-- MySQL Connector/J -->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<version>9.5.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Source: https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-sync -->
|
||||
<dependency>
|
||||
|
||||
@ -4,6 +4,7 @@ import java.util.List;
|
||||
|
||||
import es.palomafp.aadd.inm.dao.IContinenteDAO;
|
||||
import es.palomafp.aadd.inm.dao.IPaisDAO;
|
||||
import es.palomafp.aadd.inm.dao.hbm.PaisDaoHibernate;
|
||||
import es.palomafp.aadd.inm.dao.mgdb.ContinenteDaoMongoDB;
|
||||
import es.palomafp.aadd.inm.dao.mgdb.PaisDaoMongoDB;
|
||||
import es.palomafp.aadd.inm.vo.Continente;
|
||||
@ -24,23 +25,43 @@ public class AppMapaMundi {
|
||||
|
||||
// app.probarCRUDContinente();
|
||||
|
||||
app.probarCRUDPais();
|
||||
// app.probarCRUDPais();
|
||||
|
||||
app.migrarPaisesMySQLToMongoDB();
|
||||
|
||||
}
|
||||
|
||||
private void migrarPaisesMySQLToMongoDB() {
|
||||
IPaisDAO paisDAO = new PaisDaoHibernate();
|
||||
List<Pais> listaPaises = paisDAO.obtenerListaPaises();
|
||||
|
||||
if(listaPaises!=null) {
|
||||
paisDAO = new PaisDaoMongoDB();
|
||||
for (Pais pais : listaPaises) {
|
||||
Pais paisBBDD= paisDAO.obtenerPaisPorID(pais.getIdentificador());
|
||||
|
||||
if (paisBBDD==null) {
|
||||
paisDAO.crearPais(pais);
|
||||
}else {
|
||||
paisDAO.actualizarPais(pais);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void probarCRUDPais() {
|
||||
IPaisDAO paisDAO = new PaisDaoMongoDB();
|
||||
|
||||
String codigoContinente= "01";
|
||||
String codigoContinente= "02";
|
||||
Continente continente = new Continente();
|
||||
continente.setCodigo(codigoContinente);
|
||||
continente.setNombreContinente("América");
|
||||
continente.setNombreContinente("Europa");
|
||||
|
||||
Pais pais = new Pais();
|
||||
int identificador= 213;
|
||||
pais.setIdentificador(identificador);
|
||||
pais.setNombrePais("De las maravillas");
|
||||
pais.setCapital("Centro de la maravilla");
|
||||
pais.setNombrePais("Déslibai");
|
||||
pais.setCapital("Blotretico");
|
||||
pais.setContinente(continente);
|
||||
|
||||
|
||||
@ -57,7 +78,7 @@ public class AppMapaMundi {
|
||||
List<Pais> listaPais= paisDAO.obtenerListaPaises();
|
||||
System.out.println(listaPais);
|
||||
|
||||
paisDAO.borrarPais(identificador);
|
||||
//paisDAO.borrarPais(identificador);
|
||||
|
||||
listaPais= paisDAO.obtenerListaPaises();
|
||||
System.out.println(listaPais);
|
||||
|
||||
@ -0,0 +1,58 @@
|
||||
package es.palomafp.aadd.inm.dao.hbm;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.query.SelectionQuery;
|
||||
|
||||
import es.palomafp.aadd.inm.dao.IContinenteDAO;
|
||||
import es.palomafp.aadd.inm.gestor.GestorSesionesHibernate;
|
||||
import es.palomafp.aadd.inm.vo.Continente;
|
||||
|
||||
/**
|
||||
*
|
||||
* ContinenteDaoJDBC: Clase que implementa el interfaz IContinenteDAO
|
||||
*
|
||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||
* @date 31 oct 2025
|
||||
*/
|
||||
public class ContinenteDaoHibernate implements IContinenteDAO {
|
||||
|
||||
@Override
|
||||
public List<Continente> obtenerListaContientes() {
|
||||
List<Continente> listaContinentes = null;
|
||||
String sentenciaHQL = """
|
||||
SELECT c
|
||||
FROM Continente c
|
||||
""";
|
||||
// try con recursos "cerrables": Session
|
||||
try (Session sesion = GestorSesionesHibernate.getSession();) {
|
||||
SelectionQuery<Continente> sentenciaConsulta = sesion.createSelectionQuery(sentenciaHQL, Continente.class);
|
||||
listaContinentes = sentenciaConsulta.getResultList();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return listaContinentes;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void crearContinente(Continente continente) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Continente obtenerContinentePorID(String codigo) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actualizarContinente(Continente continente) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void borrarContinente(String codigo) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,62 @@
|
||||
package es.palomafp.aadd.inm.dao.hbm;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.query.SelectionQuery;
|
||||
|
||||
import es.palomafp.aadd.inm.dao.IPaisDAO;
|
||||
import es.palomafp.aadd.inm.gestor.GestorSesionesHibernate;
|
||||
import es.palomafp.aadd.inm.vo.Pais;
|
||||
|
||||
/**
|
||||
*
|
||||
* PaisDaoJDBC: Clase que implementa el interfaz IPaisDAO
|
||||
*
|
||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||
* @date 31 oct 2025
|
||||
*/
|
||||
public class PaisDaoHibernate implements IPaisDAO {
|
||||
|
||||
@Override
|
||||
public List<Pais> obtenerListaPaises() {
|
||||
List<Pais> listaPaises = null;
|
||||
String sentenciaHQL = """
|
||||
SELECT p
|
||||
FROM Pais p
|
||||
""";
|
||||
|
||||
// try con recursos "cerrables": Session
|
||||
try (Session sesion = GestorSesionesHibernate.getSession();) {
|
||||
|
||||
SelectionQuery<Pais> sentenciaConsulta = sesion.createSelectionQuery(sentenciaHQL, Pais.class);
|
||||
listaPaises = sentenciaConsulta.getResultList();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return listaPaises;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actualizarPais(Pais pais) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pais obtenerPaisPorID(int identificador) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void crearPais(Pais pais) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void borrarPais(int identificador) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -70,15 +70,13 @@ public class PaisDaoMongoDB implements IPaisDAO {
|
||||
// Insertar UN DOCUMENTO en la colección
|
||||
Document documentoPais = new Document();
|
||||
|
||||
|
||||
documentoPais.append("_id", pais.getIdentificador())
|
||||
.append("nombre", pais.getNombrePais())
|
||||
.append("capital", pais.getCapital());
|
||||
documentoPais.append("_id", pais.getIdentificador()).append("identificador", pais.getIdentificador())
|
||||
.append("nombre", pais.getNombrePais()).append("capital", pais.getCapital());
|
||||
|
||||
// Se carga la información del continente
|
||||
Document documentoContinente = new Document();
|
||||
documentoContinente.append("codigo", pais.getContinente().getCodigo())
|
||||
.append("nombre", pais.getContinente().getNombreContinente());
|
||||
documentoContinente.append("codigo", pais.getContinente().getCodigo()).append("nombre",
|
||||
pais.getContinente().getNombreContinente());
|
||||
|
||||
documentoPais.append("continente", documentoContinente);
|
||||
|
||||
@ -90,12 +88,22 @@ public class PaisDaoMongoDB implements IPaisDAO {
|
||||
public void actualizarPais(Pais pais) {
|
||||
// Obtener o crear una colección MongoDB
|
||||
MongoCollection<Document> coleccionMDb = GestorConexionMongoDB.getMongoDatabase().getCollection(COLECCION_PAIS);
|
||||
/*
|
||||
|
||||
// Actualizar UN Continente en la colección
|
||||
Bson filtro = Filters.eq("_id", continente.getCodigo());
|
||||
Bson nuevoContinente = Updates.set("nombre", continente.getNombreContinente());
|
||||
coleccionMDb.updateOne(filtro, nuevoContinente);
|
||||
*/
|
||||
Bson filtro = Filters.eq("_id", pais.getIdentificador());
|
||||
|
||||
// Contenido del documento con el que se va a actualizar el documento país
|
||||
// están todos los cmapos excepto _id e identificador.
|
||||
Document docPaisActualizado = new Document();
|
||||
docPaisActualizado.append("nombre", pais.getNombrePais())
|
||||
.append("capital", pais.getCapital());
|
||||
Document docContinente = new Document();
|
||||
docContinente.append("codigo", pais.getContinente().getCodigo())
|
||||
.append("nombre", pais.getContinente().getNombreContinente());
|
||||
docPaisActualizado.append("continente", docContinente);
|
||||
|
||||
coleccionMDb.updateOne(filtro, new Document("$set", docPaisActualizado));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -111,7 +119,8 @@ public class PaisDaoMongoDB implements IPaisDAO {
|
||||
|
||||
private Pais getPaisFromDocumentoPais(Document documentoPais) {
|
||||
Pais pais = null;
|
||||
int identificador = documentoPais.getInteger("_id");
|
||||
int idMongoDB = documentoPais.getInteger("_id");
|
||||
int identificador = documentoPais.getInteger("identificador");
|
||||
String nombrePais = documentoPais.getString("nombre");
|
||||
String capital = documentoPais.getString("capital");
|
||||
|
||||
|
||||
@ -0,0 +1,46 @@
|
||||
package es.palomafp.aadd.inm.gestor;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
|
||||
/**
|
||||
*
|
||||
* GestorSesionesHibernate: Clase que realiza ....
|
||||
*
|
||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||
* @date 4 dic 2025
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
public class GestorSesionesHibernate {
|
||||
private static SessionFactory sessionFactory = null;
|
||||
|
||||
private GestorSesionesHibernate() {// Constructor privado para evitar instanciación
|
||||
}
|
||||
|
||||
// Carga la configuración desde hibernate.cfg.xml
|
||||
static {
|
||||
try {
|
||||
sessionFactory = new Configuration().configure().buildSessionFactory();
|
||||
} catch (Throwable ex) {
|
||||
System.err.println("Error en SessionFactory: " + ex);
|
||||
throw new ExceptionInInitializerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static Session getSession() {
|
||||
return sessionFactory.openSession();
|
||||
}
|
||||
|
||||
public static Session getCurrentSession() {
|
||||
return sessionFactory.getCurrentSession();
|
||||
}
|
||||
|
||||
public static void cerrarFactoria() {
|
||||
if (sessionFactory != null) {
|
||||
sessionFactory.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,18 +1,27 @@
|
||||
package es.palomafp.aadd.inm.vo;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
/**
|
||||
*
|
||||
* Continente: Clase que se usa para mapear la tabla de continentes
|
||||
*
|
||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||
* @date 5 dic 2025
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
@Entity
|
||||
@Table(name="T_CONTINENTE")
|
||||
public class Continente {
|
||||
@Id
|
||||
@Column(name="codigo", columnDefinition = "char(2)")
|
||||
private String codigo;
|
||||
|
||||
@Column(name="nombre_continente", length=30, nullable=true)
|
||||
private String nombreContinente;
|
||||
|
||||
public String getCodigo() {
|
||||
@ -36,3 +45,4 @@ public class Continente {
|
||||
return "Continente [codigo=" + codigo + ", nombreContinente=" + nombreContinente + "]\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,13 @@
|
||||
package es.palomafp.aadd.inm.vo;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
/**
|
||||
*
|
||||
* Continente: Clase que se usa para mapear la tabla de Países
|
||||
@ -8,13 +16,22 @@ package es.palomafp.aadd.inm.vo;
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
@Entity
|
||||
@Table(name="T_PAIS")
|
||||
public class Pais {
|
||||
@Id
|
||||
@Column(name="identificador")
|
||||
private int identificador;
|
||||
|
||||
@Column(name="nombre_pais", length=50)
|
||||
private String nombrePais;
|
||||
|
||||
@Column(name="capital", length=20)
|
||||
private String capital;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
@JoinColumn(name="cod_continente")
|
||||
private Continente continente;
|
||||
|
||||
public int getIdentificador() {
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
url.conexion.mongodb=mongodb://localhost:27017
|
||||
url.conexion.mongodb=mongodb://admin:mongodb_123@172.16.0.181:27017
|
||||
bbdd.mongodb=MapaMundi
|
||||
26
src/main/resources/hibernate.cfg.xml
Normal file
26
src/main/resources/hibernate.cfg.xml
Normal file
@ -0,0 +1,26 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<!DOCTYPE hibernate-configuration PUBLIC
|
||||
"-//Hibernate/Hibernate Configuration DTD//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
<hibernate-configuration>
|
||||
<session-factory>
|
||||
<property name="connection.url">jdbc:mysql://172.16.0.181:3306/Mapa_Mundi</property>
|
||||
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
|
||||
|
||||
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
|
||||
|
||||
<property name="connection.username">root</property>
|
||||
<property name="connection.password">mysql_123</property>
|
||||
|
||||
<!-- DB schema will be updated if needed -->
|
||||
<property name="hbm2ddl.auto">none</property>
|
||||
<property name="show_sql">false</property>
|
||||
<property name="format_sql">false</property>
|
||||
|
||||
<!-- Mapeo de clases -->
|
||||
<mapping class="es.palomafp.aadd.inm.vo.Continente"></mapping>
|
||||
<mapping class="es.palomafp.aadd.inm.vo.Pais"></mapping>
|
||||
|
||||
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
||||
Loading…
Reference in New Issue
Block a user