0

I develop a webApp which is connecting to many similar database. The target databases are set by the final user in an administration GUI. They work with differents database engine.

I use JPA and Eclipselink 2.6.4 to query theses databases. Actually I've no other choice than writing a persistence.xml file on the fly and to use it to create an EntityManagerFactory.

pros.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, persistenceFilesPath + "/" + persistenceFileName);
Persistence.createEntityManagerFactory(envCode, pros);

I would like to bypass this step to get directly an EntityManagerFactory without writing an persitence.xml file.

I've read some things on PersistenceUnitInfo and createContainerEntityManagerFactory but nothing really concrete.

I'm looking for ideas to reach my goal. I hope you will have somes.

Thanks

sleray
  • 11
  • 4
  • If you don't have a persistence.xml, what would be in the EntityManagerFactory? You can change the properties you pass when creating an EntityManagerFactory to set things like the database credentials etc, but the classes themselves should mostly be static right? (if not see EclipseLink's support for dynamic entities) The persistence.xml is needed to help find the entities, so unless you are using Spring, it will be a requirement. Other files, such as orm.xml and entity metadata can be dynamic or loaded by other means. – Chris Jan 17 '17 at 21:09
  • Thanks for your reply. I've a persistence.xml for my admin database which contains informations to connect to others databases. All theses databases have a common structure. – sleray Jan 17 '17 at 21:50
  • @Chris Eclipselink has dynamic entiiies, but like most non-standard extensions words dont have the same meaning, and question has (in my opion) nothing to – Jacek Cz Jan 18 '17 at 13:52
  • @sleray You can build persistence.xml with properties, close primary PU, replace properties on runtime an open new PU. Some difficulties with concurrency, sessions but lets try – Jacek Cz Jan 18 '17 at 13:52
  • @sleray what is the purpose of the other databases? How do you know which one(s) you need to connect to for CRUD operations? More information is needed. EclipseLink has options built into it, but databases are usually known upfront. You might try creating a persistence unit for each potential connection you'll need to make, and then just pass in the actual connection details to the EMF when it is created. Using a single EMF and switching between databases is also an option, but if you disconnect an EMF it disconnects any EMs - instead you would want to switch connections at the EM level. – Chris Jan 18 '17 at 14:47
  • I'm working for an editor on test plateform administration. I'm trying to build a tool to offer scalable tests platefoms. At this time I've 45 quite similar databases used for tests. This could increase or decrease quickly. That's why I want an administration GUI. – sleray Jan 18 '17 at 15:20
  • I've seen some examples like this one, http://stackoverflow.com/questions/19773258/how-to-modify-properties-after-create-entity-manager-factory-from-persistence-xml. But each time, it's a way to customize an existing persistence-unit. I've seen nothing create a persistence-unit from scratch. – sleray Jan 18 '17 at 15:22
  • because you can't - the persistence.xml is the root of the persistence unit and tells the provider where to look for classes, and even what provider to use. Spring I believe has its own way of doing it, acting like a container, but for the most part, just have a single persitence.xml and change the connection as would be done for multitenant solutions. see http://stackoverflow.com/a/29603656/496099 – Chris Jan 20 '17 at 00:42

0 Answers0