From 5fc6bf3198726e700cf25797e9b618640cd6db3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isidoro=20Nevares=20Mart=C3=ADn?= Date: Fri, 9 Jan 2026 12:15:38 +0100 Subject: [PATCH] commit inicial --- .classpath | 40 ++++++++++ .gitignore | 3 + .project | 23 ++++++ .settings/org.eclipse.jdt.core.prefs | 8 ++ .settings/org.eclipse.m2e.core.prefs | 4 + pom.xml | 45 +++++++++++ .../concesionario/AppConcesionario.java | 80 +++++++++++++++++++ .../conectores/GestorEntityManagerJPA.java | 52 ++++++++++++ .../conectores/GestorSesionesHibernate.java | 46 +++++++++++ .../org/lapaloma/concesionario/vo/Coche.java | 64 +++++++++++++++ src/main/resources/META-INF/persistence.xml | 67 ++++++++++++++++ src/main/resources/hibernate.cfg.xml | 24 ++++++ 12 files changed, 456 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 .settings/org.eclipse.m2e.core.prefs create mode 100644 pom.xml create mode 100644 src/main/java/org/lapaloma/concesionario/AppConcesionario.java create mode 100644 src/main/java/org/lapaloma/concesionario/conectores/GestorEntityManagerJPA.java create mode 100644 src/main/java/org/lapaloma/concesionario/conectores/GestorSesionesHibernate.java create mode 100644 src/main/java/org/lapaloma/concesionario/vo/Coche.java create mode 100644 src/main/resources/META-INF/persistence.xml create mode 100644 src/main/resources/hibernate.cfg.xml diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..f7e4a1d --- /dev/null +++ b/.classpath @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..93e00af --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/bin/ +*.class +/target/ diff --git a/.project b/.project new file mode 100644 index 0000000..87dcf71 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + aadd_concesionario + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..2f5cc74 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,8 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..bed34ee --- /dev/null +++ b/pom.xml @@ -0,0 +1,45 @@ + + 4.0.0 + org.lapaloma.concesionario + aadd_concesionario + 0.0.1-SNAPSHOT + + + + objectdb + ObjectDB Repository + https://m2.objectdb.com + + + + + + + org.hibernate.orm + hibernate-core + 7.1.11.Final + + + + + com.objectdb + objectdb-jk + 2.9.4 + + + + + com.mysql + mysql-connector-j + 9.5.0 + + + + + org.postgresql + postgresql + 42.7.8 + + + + \ No newline at end of file diff --git a/src/main/java/org/lapaloma/concesionario/AppConcesionario.java b/src/main/java/org/lapaloma/concesionario/AppConcesionario.java new file mode 100644 index 0000000..3762d79 --- /dev/null +++ b/src/main/java/org/lapaloma/concesionario/AppConcesionario.java @@ -0,0 +1,80 @@ +package org.lapaloma.concesionario; + +import java.util.List; + +import org.hibernate.Session; +import org.lapaloma.concesionario.conectores.GestorEntityManagerJPA; +import org.lapaloma.concesionario.conectores.GestorSesionesHibernate; +import org.lapaloma.concesionario.vo.Coche; + +import jakarta.persistence.EntityManager; + +/** + * + * AppConcesionario: Clase que realiza el tratamiento de los coches de un + * concesionario. + * + * @author Isidoro Nevares Martín - IES Virgen de la Paloma + * @date 4 dic 2025 + * + * + */ + +public class AppConcesionario { + + public static void main(String[] args) { + AppConcesionario app = new AppConcesionario(); + try { + app.realizarMigracionMySQLAObjectDB(); + + app.realizarMigracionMySQLAPostgreSQLConJPA(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void realizarMigracionMySQLAObjectDB() throws Exception { + // Obtener los coches de MySQL para guardarlos en ObjectDB + Session sesionH = GestorSesionesHibernate.getSession(); + List listaCoches = sesionH.createQuery("FROM Coche", Coche.class).getResultList(); + for (Coche coche : listaCoches) { + System.out.println(coche); + + EntityManager em = GestorEntityManagerJPA.getEntityManager(); + Coche cocheODB = em.find(Coche.class, coche.getIdentificador()); + // Si el coche no existe en ObjectDB, lo persisto; si existe, lo actualizo + if (cocheODB == null) { + em.getTransaction().begin(); + em.persist(coche); + em.getTransaction().commit(); + } else { + em.getTransaction().begin(); + em.merge(coche); + em.getTransaction().commit(); + } + } + } + + private void realizarMigracionMySQLAPostgreSQLConJPA() { + // Obtener los coches de MySQL para guardarlos en PostreSQL + EntityManager emMySQL = GestorEntityManagerJPA.getEntityManagerMySQL(); + List listaCoches = emMySQL.createQuery("FROM Coche", Coche.class).getResultList(); + for (Coche coche : listaCoches) { + System.out.println(coche); + + EntityManager emPostgreSQL = GestorEntityManagerJPA.getEntityManagerMySQL(); + Coche cochePostgreSQL = emPostgreSQL.find(Coche.class, coche.getIdentificador()); + // Si el coche no existe en ObjectDB, lo persisto; si existe, lo actualizo + if (cochePostgreSQL == null) { + emPostgreSQL.getTransaction().begin(); + emPostgreSQL.persist(coche); + emPostgreSQL.getTransaction().commit(); + } else { + emPostgreSQL.getTransaction().begin(); + emPostgreSQL.merge(coche); + emPostgreSQL.getTransaction().commit(); + } + } + + } +} diff --git a/src/main/java/org/lapaloma/concesionario/conectores/GestorEntityManagerJPA.java b/src/main/java/org/lapaloma/concesionario/conectores/GestorEntityManagerJPA.java new file mode 100644 index 0000000..65575d4 --- /dev/null +++ b/src/main/java/org/lapaloma/concesionario/conectores/GestorEntityManagerJPA.java @@ -0,0 +1,52 @@ +package org.lapaloma.concesionario.conectores; + +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 static EntityManagerFactory entityManagerFactoryMySQL = null; + private static EntityManagerFactory entityManagerFactoryPostgreSQKL = null; + + private GestorEntityManagerJPA() { // Constructor privado para evitar instanciación + } + + // Carga la configuración desde META-INF/persistence.xml + static { + try { + entityManagerFactory = Persistence.createEntityManagerFactory("UP_CONCESIONARIO_ODB"); + entityManagerFactoryMySQL = Persistence.createEntityManagerFactory("UP_CONCESIONARIO_MYSQL"); + entityManagerFactoryPostgreSQKL = Persistence.createEntityManagerFactory("UP_CONCESIONARIO_POSTGRESQL"); + } catch (Throwable ex) { + System.err.println("Error en EntityManagerFactory: " + ex); + throw new ExceptionInInitializerError(ex); + } + } + + public static EntityManager getEntityManager() { + return entityManagerFactory.createEntityManager(); + } + + public static EntityManager getEntityManagerMySQL() { + return entityManagerFactoryMySQL.createEntityManager(); + } + + public static EntityManager getEntityManagerPostgreSQL() { + return entityManagerFactoryPostgreSQKL.createEntityManager(); + } + + public static void closeEntityManagerFactory() { + entityManagerFactory.close(); + } +} diff --git a/src/main/java/org/lapaloma/concesionario/conectores/GestorSesionesHibernate.java b/src/main/java/org/lapaloma/concesionario/conectores/GestorSesionesHibernate.java new file mode 100644 index 0000000..27dc236 --- /dev/null +++ b/src/main/java/org/lapaloma/concesionario/conectores/GestorSesionesHibernate.java @@ -0,0 +1,46 @@ +package org.lapaloma.concesionario.conectores; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.cfg.Configuration; + +/** + * + * GestorSesionesHibernate: Clase que realiza la gestión de sesiones Hibernate. + * + * @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 closeFactoria() { + if (sessionFactory != null) { + sessionFactory.close(); + } + } +} diff --git a/src/main/java/org/lapaloma/concesionario/vo/Coche.java b/src/main/java/org/lapaloma/concesionario/vo/Coche.java new file mode 100644 index 0000000..4366a1c --- /dev/null +++ b/src/main/java/org/lapaloma/concesionario/vo/Coche.java @@ -0,0 +1,64 @@ +package org.lapaloma.concesionario.vo; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; + +/** + * + * Coche: Clase de persistencia que representa un coche de un concesionario. + * + * @author Isidoro Nevares Martín - IES Virgen de la Paloma + * @date 4 dic 2025 + * + * + */ +@Entity +@Table(name = "T_COCHE") +public class Coche { + @Id + private int identificador; + + private String marca; + private String modelo; + private double cilindrada; + + public int getIdentificador() { + return identificador; + } + + public void setIdentificador(int identificador) { + this.identificador = identificador; + } + + public String getMarca() { + return marca; + } + + public void setMarca(String marca) { + this.marca = marca; + } + + public String getModelo() { + return modelo; + } + + public void setModelo(String modelo) { + this.modelo = modelo; + } + + public double getCilindrada() { + return cilindrada; + } + + public void setCilindrada(double cilindrada) { + this.cilindrada = cilindrada; + } + + @Override + public String toString() { + return "Coche [identificador=" + identificador + ", marca=" + marca + ", modelo=" + modelo + ", cilindrada=" + + cilindrada + "]"; + } + +} diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..7d2cf4c --- /dev/null +++ b/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,67 @@ + + + + + + com.objectdb.jpa.Provider + + + org.lapaloma.concesionario.vo.Coche + + + + + + + + + + + + org.hibernate.jpa.HibernatePersistenceProvider + + + org.lapaloma.concesionario.vo.Coche + + + + + + + + + + + + + + + + + + org.hibernate.jpa.HibernatePersistenceProvider + + + org.lapaloma.concesionario.vo.Coche + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml new file mode 100644 index 0000000..ded6dcc --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,24 @@ + + + + + jdbc:mysql://192.168.1.36:3306/Concesionario + com.mysql.cj.jdbc.Driver + + org.hibernate.dialect.MySQLDialect + + root + mysql_123 + + + none + false + true + + + + + + \ No newline at end of file