Modify persistence.xml on runtime

落花浮王杯 提交于 2020-06-27 14:57:17

问题


I'm developping a program that uses JPA, and I deployed it in a single jar. I wish to modify (or ask to the user, in an menú item) the configuration of the connection data with the database server.

It's possible to modify the data (user, password, ip) of the server "on the fly"?

(I apologize my bad english)


回答1:


The JPA spec doesn't allow for dynamic modification of persistence-units. Some implementations may provide an implementation-specific way of defining a persistence-unit dynamically. With DataNucleus JPA we do it as per the foot of this page




回答2:


how about making a HashMap<String,Object> containing your options and passing it to Persistence.createEntityManagerFactory("unitName",map)?

From the Docs:

public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties)

Create and return an EntityManagerFactory for the named persistence unit using the given properties.

Source



来源:https://stackoverflow.com/questions/9614385/modify-persistence-xml-on-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!