How to get EntityManager in my app?
sherkan777 Apr 28, 2009 10:18 PMHi,
My app connects to many, over four dabases at now, but this will grow to over 20dbs.
I need to get EM from persistence.xml using JPA and RESOURCE LOCAL, on tomcat 6.
So I think injectin over 20 Entity Managers for each class (over 50 classes in my project) is wrong idea.I think to declare static EntityManagersFactories for each database and create entity manager for each transaction, but I've no idea how to do this?
Can anybody help me, show some source or idea how to do this?
Here is my persistence.xml
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="sguniAdmDatabase" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:comp/env/jdbc/sguni_adm</jta-data-source> <class>pl.administration.model.Adv_Links</class> <class>pl.administration.model.Adv_Emails</class> <class>pl.administration.model.News</class> <class>pl.administration.model.Pacc_Passwords</class> <class>pl.administration.model.Tutorial</class> <class>pl.administration.model.Filter</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="dialect" value="org.hibernate.dialect.PostgreSQLDialect" /> <!-- These are the default for JBoss EJB3, but not for HEM: --> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/> <property name="hibernate.cache.use_second_level_cache" value="false" /> <property name="hibernate.cache.use_query_cache" value="false"/> <property name="hibernate.cache.region_prefix" value="hibernate.test"/> <property name="hibernate.cache.use_minimal_puts" value="true"/> <property name="hibernate.cache.use_structured_entries" value="true"/> <property name="hibernate.jdbc.use_streams_for_binary" value="true"/> </properties> </persistence-unit>