Johnnycube
Johnnycube

Reputation: 1060

Switching between embedded Databases in Java with JPA

Im currently working my way towards JPA 2.0 and I start of liking how easy it is to maintain persistent data.

What I'm currently trying to accomplish is using JPA in a basic desktop application. The application should allow me to open embedded databases which are on my file system. I chose H2 databases for now, but I can really live switching to JavaDB or anything else.

What Im trying to accomplish is, that one can open the database file without previously define a persistence-unit in the persistence.xml file. I can easily define a unit and persist objects, but it needs to be configured first.

I want to write some sort of database browser which allows opening without preconfiguration and recompiling.

http://www.objectdb.com/java/jpa/start/connection

I saw that ObjectDB allows access for this type of PersistenceFactory creation, but I was not able to transfer this example to other databases.

Am I totally wrong with the way I approach this probblem? Is JPA not designed with on-the-fly database access?

Thank you for your help, Johannes

Upvotes: 2

Views: 831

Answers (3)

James
James

Reputation: 18379

You can pass a Map of properties to createEntityManagerFactory() call that defines the database connection info, etc. The property names are the same as in the persistence.xml. I assume most JPA providers support this, EclipseLink does.

You will still need to define the set of classes for the database and map them.

If you do not have any classes either, than you could look into EclipseLink's dynamic support,

http://wiki.eclipse.org/EclipseLink/Examples/JPA/Dynamic

Upvotes: 2

DataNucleus
DataNucleus

Reputation: 15577

Not part of the JPA standard. Some implementations may offer their own API to do it. For example with DataNucleus if you go to this page http://www.datanucleus.org/products/accessplatform_3_0/jpa/persistence_unit.html at the end you can create dynamic persistence-units (and hence EMFs), and that implementation obviously allows persistence to the widest range of datastores you'll get anywhere

Upvotes: 2

Foumpie
Foumpie

Reputation: 1555

If you want to make a database browser accessing different databases, you can't use a PU/Entity Manager (imo).

You'll need a dialogue asking a user for the IP/Port of the database, the username/password, the database name to access, and the type of database.

Then all you need to do is create a socket, send requests over the socket, and parse the response into a view. Since both the request and the response are database specific, the user has to select the proper database driver.

Upvotes: 1

Related Questions