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!
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.
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.
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.
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
?
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
andnon-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 ofRESOURCE_LOCAL
may be assumed.
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);
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With