Java – dynamic change persistence unit – JPA
•
Java
persistence. The persistence unit in XML is created during the construction of the application Since I want to change the database URL at run time, is there any way to modify the persistence unit at run time? I should use a different database instead of a pre - bound database
I use eclipse link (JPA 2.1)
Solution
Keep the persistence unit file (persistence. XML) You can override the properties as follows
EntityManagerFactory managerFactory = null; Map<String,String> persistenceMap = new HashMap<String,String>(); persistenceMap.put("javax.persistence.jdbc.url","<url>"); persistenceMap.put("javax.persistence.jdbc.user","<username>"); persistenceMap.put("javax.persistence.jdbc.password","<password>"); persistenceMap.put("javax.persistence.jdbc.driver","<driver>"); managerFactory = Persistence.createEntityManagerFactory("<current persistence unit>",persistenceMap); manager = managerFactory.createEntityManager();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码