Conectar ao mesmo tempo em duas bases

0 respostas
gilvanandre

Seguinte, estou trabalhando em um sistema para importação de dados. A base antiga é em PostgreSql, e a nova base também.

Para conectar com a base nova eu uso o seguinte código:

public static EntityManagerFactory getEmf() { synchronized (HibernateUtil.class) { if (emf == null) { try { emf = Persistence.createEntityManagerFactory("F2ConversorPU2", getConf()); } catch (RuntimeException ex) { System.out.println(ex); throw ex; } } } return emf; }

private static Map getConf() {
       
        //cria um Hashmap, q recebe todas as propriedades necessarias para a conexao com o BD
        Map prop = new HashMap();
 prop.put("hibernate.connection.url","jdbc:postgresql://localhost:5432/f2erpfurla");
        prop.put("hibernate.connection.username", "f2erp-web");
        prop.put("hibernate.connection.password", "123");
        prop.put("hibernate.connection.driver_class", "org.postgresql.Driver");
        prop.put("hibernate.connection.dialect", "org.hibernate.dialect.PostgreSQLDialect");

        //System.out.println("Leu configurações");

        return prop;
    }

Na mesma classe, eu utilizo o seguinte código para conectar com a base antiga:

public static EntityManagerFactory getEmfDesk() {
        synchronized (HibernateUtil.class) {
            if (emfDesk == null) {
                try {
                    emfDesk = Persistence.createEntityManagerFactory("F2ConversorPU", getConfDesck());
                } catch (RuntimeException ex) {
                    System.out.println(ex);
                    throw ex;
                }
            }
        }
        return emfDesk;
    }
private static Map getConfDesck() {
        Map prop = new HashMap();
        prop.put("hibernate.connection.url", "jdbc:postgresql://localhost:5432/f2erp");
        prop.put("hibernate.connection.username", "f2erp");
        prop.put("hibernate.connection.password", "123");
        prop.put("hibernate.connection.driver_class", "org.postgresql.Driver");
        prop.put("hibernate.connection.dialect", "org.hibernate.dialect.PostgreSQLDialect");

        //System.out.println("Leu configurações");

        return prop;
    }

Sempre utilizei dessa forma, mas nunca com duas ao mesmo tempo, agora me retorna o seguinte erro:

Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: F2ConversorPU2] Unable to configure EntityManagerFactory
	at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:265)
	at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:125)
	at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
	at negocio.HibernateUtil.getEmf(HibernateUtil.java:26)
	at negocio.EmpresaJpaController.<init>(EmpresaJpaController.java:23)
	at f2conversor.Converte.pesquisaEmpresa(Converte.java:73)
	at f2conversor.Converte.inicia(Converte.java:2663)
	at f2conversor.F2Conversor.main(F2Conversor.java:27)
Caused by: org.hibernate.AnnotationException: Use of the same entity name twice: Setor
	at org.hibernate.cfg.annotations.EntityBinder.bindEntity(EntityBinder.java:305)
	at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:566)
	at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:534)
	at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:286)
	at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115)
	at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1225)
	at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:159)
	at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:854)
	at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:191)
	at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:253)
	... 7 more
Caused by: org.hibernate.DuplicateMappingException: duplicate import: Setor refers to both desktop.modeloDesck.Setor and modelo.Setor (try using auto-import="false")
	at org.hibernate.cfg.Mappings.addImport(Mappings.java:141)
	at org.hibernate.cfg.annotations.EntityBinder.bindEntity(EntityBinder.java:298)
	... 16 more
Java Result: 1

Obrigado

Criado 24 de junho de 2013
Respostas 0
Participantes 1