Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4381192
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:37:07+00:00 2026-05-21T12:37:07+00:00

I have a J2SE program that I’m moving over to use JPA. The existing

  • 0

I have a J2SE program that I’m moving over to use JPA. The existing program talks to anywhere from 1 to 4 databases. The database it talks to is dependent on a deployment-specific mapping of table -> database. When more than 1 database is used, it is because some tables have been shifted to another database (horizontal sharding on a database level, not table level). This is configured on a per-deployment basis – it’s up to the user to decide how they wish to structure their database setup.

Existing code looks like

Connection con = getConnectionBasedOnTable(domainObject.getClass());
// execute statement, etc...

With (J2SE) JPA I wish to use something like:

EntityManager em = getEntityManagerBasedOnTable(domainObject.getClass());
// persist, etc...

The reason for the indirection is because different users have different requirements. A small user may only have a tiny database, so a single server is enough. A large organisation may have huge databases, so we address this by placing high performance tables on high performance servers, and so on. Also used by some users to keep sensitive data in a (costly) high security environment while keeping non-sensitive data in a (cheap) medium security environment. So there’s no one size fits all database approach. Hence indirection. Also note that the tables that are eligible for separation don’t have any foreign key constraint issues.

Now that I’m trying to move to JPA, how can I allow end users to define their database structure? Is there a way to programmatically configure persistence units? Including defining new persistence units not specified in persistence.xml? How can I achieve my goal?

I can think of the following:

  • Predefine 100 or so persistence units in persistence.xml: “db1”, “db2”, etc… and have the program read in an external configuration file which applies properties to them (Persistence.createEntityManagerFactory(name, properties)). Ugly, limiting.
  • Move to a non-JPA solution (I really wish to avoid this. We want to use JPA because of its standards based nature).
  • Deploy user-specific JARs, or build scripts that can edit/modify the JAR’s persistence.xml to use their setup. A bit of a hack for sure.

For what it’s worth, we are using Hibernate’s JPA implementation. I have searched for a way to configure Hibernate’s JPA and haven’t had any success.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T12:37:08+00:00Added an answer on May 21, 2026 at 12:37 pm

    I’m closing this question because no suitable answer. For those that are interested, I ended up opting for predefining all possible combinations of persistence units in the XML file.

    Edit: It’s now 2013 and I needed to revisit this problem, and turns out it is possible to do this. See Is persistence.xml requied when working with Spring and Hibernate? – you don’t need to use Spring, but the general point of using HibernatePersistence.createContainerEntityManagerFactory() is the key.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using JPA+Hibernate with a PostGre SQL database in a J2SE project. I have
I have a really simple J2SE app that consists of a couple of classes.
I'm using EclipseLink in my J2SE project. I'm using mysql and JPA. I have
Have you refactored from an ActiveRecord to a DataMapper pattern? What conditions prompted the
I have J2SE application running in 1.5 java VM in RHEL OS. One of
I have J2SE application running in linux. I have stop application script in which
I have a problem with an J2ME client app, that sends data to an
I have issue with Win2003 that Java Applet Closes IE for Users not Admins.
I have a wcf serivce I want to consume from a j2me application (blackberry).
From http://java.sun.com/developer/technicalArticles/J2SE/security/#3 : Note: These two system properties are ignored when applications run on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.