Preciso consultar um banco no MySQL e pegar uma tabela de lá para gravar em um banco no PostgreSQL pelo Hibernate/JPA.
Configurei o persistence.xml
da seguinte forma:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="postgreSQL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/tfProjeto" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="admin" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.CharSet" value="UTF-8" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="10" />
<property name="hibernate.c3p0.timeout" value="1800" />
<property name="hibernate.c3p0.max_statements" value="50" />
</properties>
</persistence-unit>
<persistence-unit name="mySQL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql:/localhost:3306/meuProjeto?zeroDateTimeBehavior=convertToNull" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="admin" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.connection.CharSet" value="UTF-8" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="10" />
<property name="hibernate.c3p0.timeout" value="1800" />
<property name="hibernate.c3p0.max_statements" value="50" />
</properties>
</persistence-unit>
E minha EntityFactory está assim:
@RequestScoped
public class EntityFactory implements Serializable {
private static final long serialVersionUID = 1L;
public static EntityManagerFactory entityManagerFactory;
public static EntityManagerFactory entityManagerFactoryMySQL;
public static void initializeEntityManager() {
if ((entityManagerFactory == null) || (!entityManagerFactory.isOpen())) {
entityManagerFactory = Persistence.createEntityManagerFactory("postgreSQL");
}
}
public static void initializeEntityManagerMySql() {
if ((entityManagerFactoryMySQL == null) || (!entityManagerFactoryMySQL.isOpen())) {
entityManagerFactoryMySQL = Persistence.createEntityManagerFactory("mySQL");
}
}
}
Porém, quando gero as classes, o Hibernate cria em ambos os bancos as duas tabelas. Ou seja, preciso pegar da users
(MySQL) e gravar em usuarios
(PostgreSQL), porém o Hibernate cria as tabelas users
e usuarios
no MySQL e as tabelas users e usuarios
no PostgreSQL também, e o que quero é apenas criar a usuarios
apenas na base do PostgreSQL.
Alguma solução?
users
vai recuperar os dados de lá, trata conforme tu precisa e salva emusuarios
usando a EM do PostgreSQL.