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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:36:25+00:00 2026-05-13T10:36:25+00:00

Is there a way to initialize the EntityManager without a persistence unit defined? Can

  • 0

Is there a way to initialize the EntityManager without a persistence unit defined? Can you give all the required properties to create an entity manager? I need to create the EntityManager from the user’s specified values at runtime. Updating the persistence.xml and recompiling is not an option.

Any idea on how to do this is more than welcomed!

  • 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-13T10:36:25+00:00Added an answer on May 13, 2026 at 10:36 am

    Is there a way to initialize the EntityManager without a persistence unit defined?

    You should define at least one persistence unit in the persistence.xml deployment descriptor.

    Can you give all the required properties to create an Entitymanager?

    • The name attribute is required. The other attributes and elements are optional. (JPA specification). So this should be more or less your minimal persistence.xml file:
    <persistence>
        <persistence-unit name="[REQUIRED_PERSISTENCE_UNIT_NAME_GOES_HERE]">
            SOME_PROPERTIES
        </persistence-unit>
    </persistence>
    

    In Java EE environments, the jta-data-source and non-jta-data-source elements are used to specify the global JNDI name of the JTA and/or non-JTA data source to be used by the persistence provider.

    So if your target Application Server supports JTA (JBoss, Websphere, GlassFish), your persistence.xml looks like:

    <persistence>
        <persistence-unit name="[REQUIRED_PERSISTENCE_UNIT_NAME_GOES_HERE]">
            <!--GLOBAL_JNDI_GOES_HERE-->
            <jta-data-source>jdbc/myDS</jta-data-source>
        </persistence-unit>
    </persistence>
    

    If your target Application Server does not support JTA (Tomcat), your persistence.xml looks like:

    <persistence>
        <persistence-unit name="[REQUIRED_PERSISTENCE_UNIT_NAME_GOES_HERE]">
            <!--GLOBAL_JNDI_GOES_HERE-->
            <non-jta-data-source>jdbc/myDS</non-jta-data-source>
        </persistence-unit>
    </persistence>
    

    If your data source is not bound to a global JNDI (for instance, outside a Java EE container), so you would usually define JPA provider, driver, url, user and password properties. But property name depends on the JPA provider. So, for Hibernate as JPA provider, your persistence.xml file will looks like:

    <persistence>
        <persistence-unit name="[REQUIRED_PERSISTENCE_UNIT_NAME_GOES_HERE]">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <class>br.com.persistence.SomeClass</class>
            <properties>
                <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.ClientDriver"/>
                <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/EmpServDB;create=true"/>
                <property name="hibernate.connection.username" value="APP"/>
                <property name="hibernate.connection.password" value="APP"/>
            </properties>
        </persistence-unit>
    </persistence>
    

    Transaction Type Attribute

    In general, in Java EE environments, a transaction-type of RESOURCE_LOCAL assumes that a non-JTA datasource will be provided. In a Java EE environment, if this element is not specified, the default is JTA. In a Java SE environment, if this element is not specified, a default of RESOURCE_LOCAL may be assumed.

    • To insure the portability of a Java SE application, it is necessary to explicitly list the managed persistence classes that are included in the persistence unit (JPA specification)

    I need to create the EntityManager from the user’s specified values at runtime

    So use this:

    Map addedOrOverridenProperties = new HashMap();
    
    // Let's suppose we are using Hibernate as JPA provider
    addedOrOverridenProperties.put("hibernate.show_sql", true);
    
    Persistence.createEntityManagerFactory(<PERSISTENCE_UNIT_NAME_GOES_HERE>, addedOrOverridenProperties);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 296k
  • Answers 296k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As performance is an issue, you might find an article… May 13, 2026 at 7:08 pm
  • Editorial Team
    Editorial Team added an answer Try this: var removeThis:Object = mylist.dataProvider[selIndex]; mylist.dataProvider.removeItem(removeThis); The reason your… May 13, 2026 at 7:08 pm
  • Editorial Team
    Editorial Team added an answer If you really only care about searching for an <l>… May 13, 2026 at 7:08 pm

Related Questions

I'm using DRb within a Rails application to offload an expensive task outside the
I know the class name, say MyClass and want to retrieve the Class object,
I have a python module written in C, and I would like to add
in my Django application I've got a form with a ChoiceField that normally allows
In the following class: public class Example<T> where T : IComparable { private T

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.