commit inicial
This commit is contained in:
commit
092f192fe1
41
.classpath
Normal file
41
.classpath
Normal file
@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="optional" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="test" value="true"/>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||
<attributes>
|
||||
<attribute name="test" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="optional" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk-25">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
||||
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
/bin/
|
||||
*.class
|
||||
/target/
|
||||
23
.project
Normal file
23
.project
Normal file
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>aadd_act4_1</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
2
.settings/org.eclipse.core.resources.prefs
Normal file
2
.settings/org.eclipse.core.resources.prefs
Normal file
@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
||||
13
.settings/org.eclipse.jdt.core.prefs
Normal file
13
.settings/org.eclipse.jdt.core.prefs
Normal file
@ -0,0 +1,13 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=24
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=24
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||
org.eclipse.jdt.core.compiler.release=disabled
|
||||
org.eclipse.jdt.core.compiler.source=24
|
||||
4
.settings/org.eclipse.m2e.core.prefs
Normal file
4
.settings/org.eclipse.m2e.core.prefs
Normal file
@ -0,0 +1,4 @@
|
||||
activeProfiles=
|
||||
eclipse.preferences.version=1
|
||||
resolveWorkspaceProjects=true
|
||||
version=1
|
||||
48
pom.xml
Normal file
48
pom.xml
Normal file
@ -0,0 +1,48 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>aadd_act2_3</groupId>
|
||||
<artifactId>aadd_act2_3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<!-- Repositorio propio de ObjectDB -->
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>objectdb</id>
|
||||
<name>ObjectDB Repository</name>
|
||||
<url>https://m2.objectdb.com</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Dependencia de ObjectDB buscada en el repositorio propio de ObjectDB -->
|
||||
<dependency>
|
||||
<groupId>com.objectdb</groupId>
|
||||
<artifactId>objectdb-jk</artifactId>
|
||||
<version>2.9.4</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Hibernate Core -->
|
||||
<dependency>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>7.1.14.Final</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Jakarta Persistence API (requerida por Hibernate 7) -->
|
||||
<dependency>
|
||||
<groupId>jakarta.persistence</groupId>
|
||||
<artifactId>jakarta.persistence-api</artifactId>
|
||||
<version>3.2.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- MySQL Connector/J -->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<version>9.5.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
85
src/main/java/es/palomafp/aadd/inm/AppMapaMundi.java
Normal file
85
src/main/java/es/palomafp/aadd/inm/AppMapaMundi.java
Normal file
@ -0,0 +1,85 @@
|
||||
package es.palomafp.aadd.inm;
|
||||
|
||||
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.ContinenteDaoHibernate;
|
||||
import es.palomafp.aadd.inm.dao.hbm.PaisDaoHibernate;
|
||||
import es.palomafp.aadd.inm.dao.jpa.ContinenteDaoJPA;
|
||||
import es.palomafp.aadd.inm.dao.jpa.PaisDaoJPA;
|
||||
import es.palomafp.aadd.inm.vo.Continente;
|
||||
import es.palomafp.aadd.inm.vo.Pais;
|
||||
|
||||
/**
|
||||
*
|
||||
* GestorMapaMundi: Clase que accede a información de la BBDD MapaMundi.
|
||||
*
|
||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||
* @date 26 sept 2025
|
||||
*/
|
||||
|
||||
public class AppMapaMundi {
|
||||
|
||||
public static void main(String[] args) {
|
||||
AppMapaMundi app = new AppMapaMundi();
|
||||
|
||||
app.migrarMapaMundiDeMySQLaObjectDB();
|
||||
}
|
||||
|
||||
private void migrarMapaMundiDeMySQLaObjectDB() {
|
||||
// Se migra primeramente la información de los continentes
|
||||
migrarContinentes();
|
||||
|
||||
// A continuación, se migra la información de los países
|
||||
migrarPaises();
|
||||
}
|
||||
|
||||
private void migrarContinentes() {
|
||||
// Obtener la lista de continentes desde la BBDD MySQL (Hibernate)
|
||||
IContinenteDAO iContinenteDAO = new ContinenteDaoHibernate();
|
||||
List<Continente> listadoContinentes = iContinenteDAO.obtenerListaContientes();
|
||||
|
||||
if (listadoContinentes != null) {
|
||||
|
||||
// Guardar la lista de continentes en la BBDD ObjectDB
|
||||
iContinenteDAO = new ContinenteDaoJPA();
|
||||
|
||||
for (Continente continente : listadoContinentes) {
|
||||
// Comprobar si el continente ya existe en la BBDD ObjectDB
|
||||
Continente continenteODB = iContinenteDAO.obtenerContinentePorID(continente.getCodigo());
|
||||
|
||||
// Si no existe, crearlo; si existe, actualizarlo
|
||||
if (continenteODB == null) {
|
||||
iContinenteDAO.crearContinente(continente);
|
||||
} else {
|
||||
iContinenteDAO.actualizarContinente(continente);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private void migrarPaises() {
|
||||
// Obtener la lista de Paises desde la BBDD MySQL (Hibernate)
|
||||
IPaisDAO iPaisDAO = new PaisDaoHibernate();
|
||||
List<Pais> listaPaises = iPaisDAO.obtenerListaPaises();
|
||||
|
||||
if (listaPaises != null) {
|
||||
|
||||
// Guardar la lista de Paises en la BBDD ObjectDB
|
||||
iPaisDAO = new PaisDaoJPA();
|
||||
|
||||
for (Pais pais : listaPaises) {
|
||||
// Comprobar si el país ya existe en la BBDD ObjectDB
|
||||
Pais paisODB = iPaisDAO.obtenerPaisPorID(pais.getIdentificador());
|
||||
|
||||
// Si no existe, crearlo; si existe, actualizarlo
|
||||
if (paisODB == null) {
|
||||
iPaisDAO.crearPais(pais);
|
||||
} else {
|
||||
iPaisDAO.actualizarPais(pais);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
23
src/main/java/es/palomafp/aadd/inm/dao/IContinenteDAO.java
Normal file
23
src/main/java/es/palomafp/aadd/inm/dao/IContinenteDAO.java
Normal file
@ -0,0 +1,23 @@
|
||||
package es.palomafp.aadd.inm.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import es.palomafp.aadd.inm.vo.Continente;
|
||||
|
||||
/**
|
||||
*
|
||||
* IContinenteDAO: Interfaz que define las operaciones de acceso a datos para
|
||||
* Continente.
|
||||
*
|
||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||
* @date 31 oct 2025
|
||||
*/
|
||||
public interface IContinenteDAO {
|
||||
Continente obtenerContinentePorID(String codigo);
|
||||
|
||||
List<Continente> obtenerListaContientes();
|
||||
|
||||
void crearContinente(Continente continente);
|
||||
|
||||
void actualizarContinente(Continente continente);
|
||||
}
|
||||
22
src/main/java/es/palomafp/aadd/inm/dao/IPaisDAO.java
Normal file
22
src/main/java/es/palomafp/aadd/inm/dao/IPaisDAO.java
Normal file
@ -0,0 +1,22 @@
|
||||
package es.palomafp.aadd.inm.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import es.palomafp.aadd.inm.vo.Pais;
|
||||
|
||||
/**
|
||||
*
|
||||
* IPaisDAO: Interfaz que define las operaciones de acceso a datos para País.
|
||||
*
|
||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||
* @date 31 oct 2025
|
||||
*/
|
||||
public interface IPaisDAO {
|
||||
Pais obtenerPaisPorID(int identificador);
|
||||
|
||||
List<Pais> obtenerListaPaises();
|
||||
|
||||
void crearPais(Pais pais);
|
||||
|
||||
void actualizarPais(Pais pais);
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
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) {
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,90 @@
|
||||
package es.palomafp.aadd.inm.dao.jpa;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import es.palomafp.aadd.inm.dao.IContinenteDAO;
|
||||
import es.palomafp.aadd.inm.gestor.GestorEntityManagerJPA;
|
||||
import es.palomafp.aadd.inm.vo.Continente;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityTransaction;
|
||||
|
||||
/**
|
||||
*
|
||||
* ContinenteDaoJDBC: Clase que implementa el interfaz IContinenteDAO
|
||||
*
|
||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||
* @date 31 oct 2025
|
||||
*/
|
||||
public class ContinenteDaoJPA implements IContinenteDAO {
|
||||
|
||||
@Override
|
||||
public Continente obtenerContinentePorID(String codigo) {
|
||||
Continente continente = null;
|
||||
|
||||
// try con recursos "cerrables": Session
|
||||
try (EntityManager gestorEntidades = GestorEntityManagerJPA.getEntityManager()) {
|
||||
continente = gestorEntidades.find(Continente.class, codigo);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return continente;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void crearContinente(Continente continente) {
|
||||
EntityManager gestorEntidades= null;
|
||||
EntityTransaction transaccion = null;
|
||||
|
||||
try {
|
||||
gestorEntidades = GestorEntityManagerJPA.getEntityManager();
|
||||
transaccion = gestorEntidades.getTransaction();
|
||||
transaccion .begin();
|
||||
|
||||
gestorEntidades.persist(continente);
|
||||
|
||||
transaccion.commit();
|
||||
} catch (Exception e) {
|
||||
if (transaccion != null && transaccion.isActive()) {
|
||||
transaccion.rollback();
|
||||
}
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (gestorEntidades != null) {
|
||||
gestorEntidades.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actualizarContinente(Continente continente) {
|
||||
EntityManager gestorEntidades= null;
|
||||
EntityTransaction transaccion = null;
|
||||
|
||||
try {
|
||||
gestorEntidades = GestorEntityManagerJPA.getEntityManager();
|
||||
transaccion = gestorEntidades.getTransaction();
|
||||
transaccion .begin();
|
||||
|
||||
if (!gestorEntidades.contains(continente))
|
||||
gestorEntidades.merge(continente);
|
||||
|
||||
transaccion.commit();
|
||||
} catch (Exception e) {
|
||||
if (transaccion != null && transaccion.isActive()) {
|
||||
transaccion.rollback();
|
||||
}
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (gestorEntidades != null) {
|
||||
gestorEntidades.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Continente> obtenerListaContientes() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
93
src/main/java/es/palomafp/aadd/inm/dao/jpa/PaisDaoJPA.java
Normal file
93
src/main/java/es/palomafp/aadd/inm/dao/jpa/PaisDaoJPA.java
Normal file
@ -0,0 +1,93 @@
|
||||
package es.palomafp.aadd.inm.dao.jpa;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import es.palomafp.aadd.inm.dao.IPaisDAO;
|
||||
import es.palomafp.aadd.inm.gestor.GestorEntityManagerJPA;
|
||||
import es.palomafp.aadd.inm.vo.Pais;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityTransaction;
|
||||
|
||||
/**
|
||||
*
|
||||
* PaisDaoJPA: Clase que implementa el interfaz IPaisDAO
|
||||
*
|
||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||
* @date 31 oct 2025
|
||||
*/
|
||||
public class PaisDaoJPA implements IPaisDAO {
|
||||
|
||||
@Override
|
||||
public Pais obtenerPaisPorID(int identificador) {
|
||||
Pais pais = null;
|
||||
|
||||
// try con recursos "cerrables": Session
|
||||
try (EntityManager gestorEntidades = GestorEntityManagerJPA.getEntityManager()) {
|
||||
pais = gestorEntidades.find(Pais.class, identificador);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return pais;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void crearPais(Pais pais) {
|
||||
EntityManager gestorEntidades = null;
|
||||
EntityTransaction transaccion = null;
|
||||
|
||||
try {
|
||||
gestorEntidades = GestorEntityManagerJPA.getEntityManager();
|
||||
transaccion = gestorEntidades.getTransaction();
|
||||
transaccion.begin();
|
||||
|
||||
gestorEntidades.persist(pais);
|
||||
|
||||
transaccion.commit();
|
||||
} catch (Exception e) {
|
||||
if (transaccion != null && transaccion.isActive()) {
|
||||
transaccion.rollback();
|
||||
}
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (gestorEntidades != null) {
|
||||
gestorEntidades.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actualizarPais(Pais pais) {
|
||||
EntityManager gestorEntidades = null;
|
||||
EntityTransaction transaccion = null;
|
||||
|
||||
try {
|
||||
gestorEntidades = GestorEntityManagerJPA.getEntityManager();
|
||||
transaccion = gestorEntidades.getTransaction();
|
||||
transaccion.begin();
|
||||
|
||||
if (!gestorEntidades.contains(pais))
|
||||
gestorEntidades.merge(pais);
|
||||
|
||||
transaccion.commit();
|
||||
} catch (Exception e) {
|
||||
if (transaccion != null && transaccion.isActive()) {
|
||||
transaccion.rollback();
|
||||
}
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (gestorEntidades != null) {
|
||||
gestorEntidades.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pais> obtenerListaPaises() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
package es.palomafp.aadd.inm.gestor;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.Persistence;
|
||||
|
||||
/**
|
||||
*
|
||||
* GestorEntityManagerJPA: Clase que realiza la gestión de EntityManagers de JPA.
|
||||
*
|
||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||
* @date 4 dic 2025
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
public class GestorEntityManagerJPA {
|
||||
private static EntityManagerFactory entityManagerFactory = null;
|
||||
|
||||
private GestorEntityManagerJPA() { // Constructor privado para evitar instanciación
|
||||
}
|
||||
|
||||
// Carga la configuración desde META-INF/persistence.xml
|
||||
static {
|
||||
try {
|
||||
entityManagerFactory = Persistence.createEntityManagerFactory("UP_MAPAMUNDI_ODB");
|
||||
} catch (Throwable ex) {
|
||||
System.err.println("Error en EntityManagerFactory: " + ex);
|
||||
throw new ExceptionInInitializerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static EntityManager getEntityManager() {
|
||||
return entityManagerFactory.createEntityManager();
|
||||
}
|
||||
|
||||
|
||||
public static void closeEntityManagerFactory() {
|
||||
entityManagerFactory.close();
|
||||
}
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
48
src/main/java/es/palomafp/aadd/inm/vo/Continente.java
Normal file
48
src/main/java/es/palomafp/aadd/inm/vo/Continente.java
Normal file
@ -0,0 +1,48 @@
|
||||
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() {
|
||||
return codigo;
|
||||
}
|
||||
|
||||
public void setCodigo(String codigo) {
|
||||
this.codigo = codigo;
|
||||
}
|
||||
|
||||
public String getNombreContinente() {
|
||||
return nombreContinente;
|
||||
}
|
||||
|
||||
public void setNombreContinente(String nombreContinente) {
|
||||
this.nombreContinente = nombreContinente;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Continente [codigo=" + codigo + ", nombreContinente=" + nombreContinente + "]\n";
|
||||
}
|
||||
}
|
||||
|
||||
75
src/main/java/es/palomafp/aadd/inm/vo/Pais.java
Normal file
75
src/main/java/es/palomafp/aadd/inm/vo/Pais.java
Normal file
@ -0,0 +1,75 @@
|
||||
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
|
||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||
* @date 5 dic 2025
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
@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() {
|
||||
return identificador;
|
||||
}
|
||||
|
||||
public void setIdentificador(int identificador) {
|
||||
this.identificador = identificador;
|
||||
}
|
||||
|
||||
public String getNombrePais() {
|
||||
return nombrePais;
|
||||
}
|
||||
|
||||
public void setNombrePais(String nombrePais) {
|
||||
this.nombrePais = nombrePais;
|
||||
}
|
||||
|
||||
public String getCapital() {
|
||||
return capital;
|
||||
}
|
||||
|
||||
public void setCapital(String capital) {
|
||||
this.capital = capital;
|
||||
}
|
||||
|
||||
public Continente getContinente() {
|
||||
return continente;
|
||||
}
|
||||
|
||||
public void setContinente(Continente continente) {
|
||||
this.continente = continente;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Pais [identificador=" + identificador + ", nombrePais=" + nombrePais + ", capital=" + capital
|
||||
+ ", continente=" + continente + "]\n";
|
||||
}
|
||||
}
|
||||
|
||||
25
src/main/resources/META-INF/persistence.xml
Normal file
25
src/main/resources/META-INF/persistence.xml
Normal file
@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="
|
||||
http://xmlns.jcp.org/xml/ns/persistence
|
||||
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
|
||||
version="2.1">
|
||||
|
||||
<persistence-unit name="UP_MAPAMUNDI_ODB" transaction-type="RESOURCE_LOCAL">
|
||||
<!-- Proveedor de persistencia JPA -->
|
||||
<provider>com.objectdb.jpa.Provider</provider>
|
||||
|
||||
<!-- Clases a "mapear" -->
|
||||
<class>es.palomafp.aadd.inm.vo.Continente</class>
|
||||
<class>es.palomafp.aadd.inm.vo.Pais</class>
|
||||
|
||||
<!-- Configuración de propiedades del SGDB (ObjectDB)-->
|
||||
<properties>
|
||||
<property name="jakarta.persistence.jdbc.url" value="objectdb://localhost/Mapa_Mundi.odb" />
|
||||
<property name="jakarta.persistence.jdbc.user" value="admin"/>
|
||||
<property name="jakarta.persistence.jdbc.password" value="admin"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
</persistence>
|
||||
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://192.168.1.36: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