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 1051241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:54:01+00:00 2026-05-16T16:54:01+00:00

I am investigating using JPA for my Data Access code. I am trying to

  • 0

I am investigating using JPA for my Data Access code. I am trying to write the business layer and data access layer so it will work in a web application and a Java SE application. Therefore I cannot use container managed persistence context. Most of my seraches on using JPA show examples in a container managed enviroment.

At the moment I get the EntityManagerFactory everytime I create a new instace of the service class. For every operation (ie add, get, etc) I open an EntityManager, start a transaction perform operations, commit and then close the EntityManager and the EntityManagerFactory. I would like the benifits of having a manged persistence context outside of the Java EE environment.

Are there any best practices when not using a container managed context? Are there any Java EE independent persistence context managers? Are there any recommended patterns?

Thanks,

Al

Update

Thank you eveyone for the info. Everything was very useful.

  • 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-16T16:54:02+00:00Added an answer on May 16, 2026 at 4:54 pm

    I’m not sure about the best practices surrounding this, but I have spent a lot of time trying to make something like this work.

    Basically you will need something to construct an EntityManager with. I’ve always used Spring for this. Their documentation has a big section on this. You can choose to use a LocalEntityManagerFactoryBean, in which case the markup would look like (from aforementioned documentation):

    <bean id="myEmf" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
      <property name="persistenceUnitName" value="myPersistenceUnit"/>
    </bean>
    

    This is, in principle, not encouraged, because it comes back to haunt you when you try to change data sources later on. However, I find it’s very unlikely you’ll run into the limitations of this for most webapps up to a certain size.

    Configuration of your datasource can then be done through hibernate specific properties in your persistence unit (persistence.xml in META-INF/ directory):

    <property name="hibernate.connection.driver_class" value="com.company.driver" />
    <property name="hibernate.connection.url" value="some:jdbc@string" />
    <property name="hibernate.connection.username" value="myuser" />
    <property name="hibernate.connection.password" value="mypassword" />
    

    To use this, if you’re not using spring already, you can just grab an instance of the EntityManagerFactory from an application context and go from there (i.e. context.getBean("myEmf")).

    More control is possible with LocalContainerEntityManagerFactoryBean, this one allows you to configure a data source. In principle the example from the docs should work but I found when I did this I had to specify the Hibernate persistence provider. You need a persistence.xml but it really only needs a default persistence unit and very basic configuration (perhaps identifying the dialect, e.g. if you’re using hibernate with oracle 10g):

    <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="myDatasource" />
        <property name="persistenceProvider">
            <bean class="org.hibernate.ejb.HibernatePersistence" />
        </property>
    </bean>
    

    There’s examples elsewhere in the spring docs on how to configure a BasicDataSource from Apache dbcp, which will give you pooling of connections as well.

    As for best practices, JPA really isn’t easy to work with outside of a full application server environment. There’s all sorts of issues when trying to tune performance too where you’ll find yourself salivating over Hibernate features that aren’t available to you in JPA, and chances are your queries will end up not strictly compliant with the JPA specs anyway. If you’re not using container managed, it would probably be a lot easier and saner to just use the Hibernate APIs directly.

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

Sidebar

Related Questions

I am using YourKit Java profiler for my web application which is hosted on
I'm investigating ways of using GPU in order to process streaming data. I had
I am investigating a Java issue (using IBM JVM 1.4.2 64-bit) on Red Hat
We are using Entity Framework Code First with Foreign Key relationships. We investigating on
I'm investigating using Kohana for my next project. The site will consist of user
I am currently investigating using the selenium-on-rails plug-in for testing an upcoming web app
I'm investigating using ActiveMQ as an embedded in-process message queue in my application, but
I'm investigating a serious performance problem in my OSGi-based application which is using Eclipselink
I'm investigating using hosted subversion for a new project at work and was wondering
I have a decent working web application (Java/Servlet/Jsp) that I would like to improve

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.