Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create JPA EntityManager without persistence.xml configuration file

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!

like image 242
javydreamercsw Avatar asked Jan 01 '10 21:01

javydreamercsw


People also ask

Does Spring data JPA require persistence xml?

persistence. xml is needed when you're using Hibernate through JPA, even though you're using Spring JPA. If you're using Hibernate directly, then persistence. xml isn't needed.

Why do we need persistence xml?

The Java Persistence XML configuration file allows you to define a Persistence Unit configuration that you can later bootstrap using Java EE or Spring. Knowing all the persistence. xml file configuration options is very important as it allows you to address a great variety of mapping requirements.

Where is persistence xml created?

Create a persistence. xml file that resides in the META-INF folder. Another option is to set the packagesToScan property in Spring's config, like this: <code> <bean id="entityManagerFactory" class="org.


2 Answers

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); 
like image 98
Arthur Ronald Avatar answered Sep 29 '22 18:09

Arthur Ronald


Yes you can without using any xml file using spring like this inside a @Configuration class (or its equivalent spring config xml):

@Bean public LocalContainerEntityManagerFactoryBean emf(){     properties.put("javax.persistence.jdbc.driver", dbDriverClassName);     properties.put("javax.persistence.jdbc.url", dbConnectionURL);     properties.put("javax.persistence.jdbc.user", dbUser); //if needed      LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();     emf.setPersistenceProviderClass(org.eclipse.persistence.jpa.PersistenceProvider.class); //If your using eclipse or change it to whatever you're using     emf.setPackagesToScan("com.yourpkg"); //The packages to search for Entities, line required to avoid looking into the persistence.xml     emf.setPersistenceUnitName(SysConstants.SysConfigPU);     emf.setJpaPropertyMap(properties);     emf.setLoadTimeWeaver(new ReflectiveLoadTimeWeaver()); //required unless you know what your doing     return emf; } 
like image 24
Frank Orellana Avatar answered Sep 29 '22 16:09

Frank Orellana