Mapping the same entity class to two banks

Asked

Viewed 314 times

1

Good afternoon Earthlings,

I am a beginner in java and I have the following situation. I have two databases, where I will transfer the records from one database to the other. There is the possibility of having a class of Entity mapped by the two banks?

Remarks:

  • The application is using Entitymanagerfactory and the same self instancio um Entitymanger.
  • Your question is too broad. Well, I think the answer to this problem lies in this study: http://blog.caelum.com.br/accessingmultiplos-bancos-data-com-jpa-e-cdi/

  • Vlw @Marcos. I’ll read the article

1 answer

2

Yes. In JPA you use persistence units, which represent specific banks. If you want to specify more than one bank just declare more than one persistence unit (PU) and create the Entity managers for each of them. The persistence.xml file declaring Pus would look like this:

<persistence-unit name="pu1">
<properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/banco1" />
        <property name="javax.persistence.jdbc.user" value="admin" />
        <property name="javax.persistence.jdbc.password" value="123456" />

<persistence-unit name="pu2">
<properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/banco2" />
        <property name="javax.persistence.jdbc.user" value="admin" />
        <property name="javax.persistence.jdbc.password" value="123456" />

There to reference an entity that is in the bank 1 you only need to instantiate the pu1 in the creation of the Entity manager:

EntityManager emPU1 = Persistence.createEntityManagerFactory("pu1").createEntityManager();

For the entity that is in Bank 2 just refer to pu2:

EntityManager emPU2 = Persistence.createEntityManagerFactory("pu2").createEntityManager();

After consulting both entities, you can do your DE-PARA and persist as you wish the final result. I hope to have helped ^^

  • So I did it this way before asking the question, but I thought I was doing it wrong. But I saw that I’m on the right track...

  • That’s the way it is when you use JPA :)

  • Wanting to take advantage of the post to ask a basic question for you, but dfícil for me rsrs. So you could ask me a question regarding how to create xml + Beans in Spring ?

  • Yes I can, ask the question I answer there!

  • http://answall.com/questions/150518/d%C3%Bavidas-sobre-o-arquivo-xml-do-spring

Browser other questions tagged

You are not signed in. Login or sign up in order to post.