Persistence units in persistence.xml are created during building the application. As I want to change the database url at runtime, is there any way to modify the persistence
Keep the persistence unit file (Persistence.xml) as it's. You can override the properties in it as follows.
EntityManagerFactory managerFactory = null;
Map persistenceMap = new HashMap();
persistenceMap.put("javax.persistence.jdbc.url", "");
persistenceMap.put("javax.persistence.jdbc.user", "");
persistenceMap.put("javax.persistence.jdbc.password", "");
persistenceMap.put("javax.persistence.jdbc.driver", "");
managerFactory = Persistence.createEntityManagerFactory("", persistenceMap);
manager = managerFactory.createEntityManager();