commit inicial
This commit is contained in:
commit
5fc6bf3198
40
.classpath
Normal file
40
.classpath
Normal file
@ -0,0 +1,40 @@
|
||||
<?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="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="test" 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/JavaSE-1.8">
|
||||
<attributes>
|
||||
<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_concesionario</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>
|
||||
8
.settings/org.eclipse.jdt.core.prefs
Normal file
8
.settings/org.eclipse.jdt.core.prefs
Normal file
@ -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
|
||||
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
|
||||
45
pom.xml
Normal file
45
pom.xml
Normal file
@ -0,0 +1,45 @@
|
||||
<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>org.lapaloma.concesionario</groupId>
|
||||
<artifactId>aadd_concesionario</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>
|
||||
<!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-core -->
|
||||
<dependency>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>7.1.11.Final</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 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>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<version>9.5.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>42.7.8</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@ -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<Coche> 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<Coche> 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
64
src/main/java/org/lapaloma/concesionario/vo/Coche.java
Normal file
64
src/main/java/org/lapaloma/concesionario/vo/Coche.java
Normal file
@ -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 + "]";
|
||||
}
|
||||
|
||||
}
|
||||
67
src/main/resources/META-INF/persistence.xml
Normal file
67
src/main/resources/META-INF/persistence.xml
Normal file
@ -0,0 +1,67 @@
|
||||
<?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_CONCESIONARIO_ODB" transaction-type="RESOURCE_LOCAL">
|
||||
<!-- Proveedor de persistencia JPA -->
|
||||
<provider>com.objectdb.jpa.Provider</provider>
|
||||
|
||||
<!-- Clases a "mapear" -->
|
||||
<class>org.lapaloma.concesionario.vo.Coche</class>
|
||||
|
||||
<!-- Configuración de propiedades del SGDB (ObjectDB)-->
|
||||
<properties>
|
||||
<property name="jakarta.persistence.jdbc.url" value="objectdb://localhost/Cocensionario.odb" />
|
||||
<property name="jakarta.persistence.jdbc.user" value="admin"/>
|
||||
<property name="jakarta.persistence.jdbc.password" value="admin"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
<persistence-unit name="UP_CONCESIONARIO_MYSQL" transaction-type="RESOURCE_LOCAL">
|
||||
<!-- Proveedor de persistencia JPA -->
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
|
||||
<!-- Clases a "mapear" -->
|
||||
<class>org.lapaloma.concesionario.vo.Coche</class>
|
||||
|
||||
<!-- Configuración de propiedades del SGDB (MySQL) -->
|
||||
<properties>
|
||||
<property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://192.168.1.36:3306/Concesionario"/>
|
||||
<property name="jakarta.persistence.jdbc.user" value="root"/>
|
||||
<property name="jakarta.persistence.jdbc.password" value="mysql_123"/>
|
||||
<property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
|
||||
|
||||
<!-- Configuración Hibernate -->
|
||||
<property name="hibernate.hbm2ddl.auto" value="none"/>
|
||||
<property name="hibernate.show_sql" value="true"/>
|
||||
</properties>
|
||||
|
||||
</persistence-unit>
|
||||
|
||||
<persistence-unit name="UP_CONCESIONARIO_POSTGRESQL" transaction-type="RESOURCE_LOCAL">
|
||||
<!-- Proveedor de persistencia JPA -->
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
|
||||
<!-- Clases a "mapear" -->
|
||||
<class>org.lapaloma.concesionario.vo.Coche</class>
|
||||
|
||||
<!-- Configuración de propiedades del SGDB (PostgreSQL) -->
|
||||
<properties>
|
||||
<property name="jakarta.persistence.jdbc.url" value="jdbc:postgresql://192.168.1.36:5432/Concesionario"/>
|
||||
<property name="jakarta.persistence.jdbc.user" value="vdlp"/>
|
||||
<property name="jakarta.persistence.jdbc.password" value="postgresql_123"/>
|
||||
<property name="jakarta.persistence.jdbc.driver" value="org.postgresql.Driver"/>
|
||||
|
||||
<!-- Configuración Hibernate -->
|
||||
<property name="hibernate.hbm2ddl.auto" value="none"/>
|
||||
<property name="hibernate.show_sql" value="true"/>
|
||||
</properties>
|
||||
|
||||
</persistence-unit>
|
||||
|
||||
|
||||
</persistence>
|
||||
24
src/main/resources/hibernate.cfg.xml
Normal file
24
src/main/resources/hibernate.cfg.xml
Normal file
@ -0,0 +1,24 @@
|
||||
<?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/Concesionario</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">true</property>
|
||||
|
||||
<mapping class="org.lapaloma.concesionario.vo.Coche"></mapping>
|
||||
|
||||
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
||||
Loading…
Reference in New Issue
Block a user