I have a persistence.xml file using JDBC, working fine, but the code updates the information in it as users change databases. What's best practice for reading out the current database from the javax.persistence.jdbc.url property? Parse the file as XML or can I load the information into some persistence unit object by passing the name to it?
You can access a persistence unit as follows:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("some_unit_name");
Related
We have an application which uses multiple databases to store the same data for different countries.
For example a Subscription object might be associated with Germany or Spain. If it's a German subscription, it needs to be stored in a different database to the Spanish subscriptions. The databases are identical in structure, but they have different contents.
We are running on jboss 5, and have a different datasource config (*ds.xml) file for each one, generated dynamically at startup. They are stored in JNDI - so we have DataSourceDE, DataSourceES, etc.
Here's how it should work: if a request comes in saying 'fetch subscription 17 for Germany' then I calculate that the datasource should be DataSourceDE and use JPA / hibernate to go fetch that object from the correct database. There will be a subscription 17 in the Spanish database too, which I don't want in this example.
I can generate the persistence.xml automatically to create the extra persistence units for the datasources, but the Subscription class is annotated with the following:
#PersistenceContext(unitName="core")
This is not going to work - how can I set the persistence context on the java object dynamically?
What you are trying to achieve is known as Multi-Tenancy. Here is a perfectly suitable tutorial for your question to make it work.
The main idea is to use a Stateless session bean which has a reference to both persistence units. Depending on what has to be done, this bean does a lookup to call the corresponding EntityManager. Furthermore here:
Multi-Tenancy With EJB 3.1 and JPA 2.0
You can change the persistence context for the EntityManager at runtime like this:
EntityManagerFactory emf =
Persistence.createEntityManagerFactory(persistenceUnitName);
EntityManager em = emf.createEntityManager();
JPA is pretty good for immutable tables but I can't handle dynamic table.
Then I decide to use JPA for handle al the unalterable tables (the core of the application) and dbutils from apache to handle all my the dynamic tables
Both kind of tables are in the same database and I want to define the configuration of the database only in a single file
I define the jpa in a property file:
javax.persistence.jdbc.driver=com.mysql.jdbc.Driver
javax.persistence.jdbc.user=root
javax.persistence.jdbc.password=*****
javax.persistence.jdbc.url=jdbc:mysql://localhost:3306/database
But dbutils use the class "datasource" to configurate.
I didn't find how to get a "Datasource" from de "EntityManagerFactory" of jpa.
Or to get a instance of "EntityManagerFactory" from a "Datasource".
An EMF can be created for a (javax.sql.)DataSource by specifying the persistence-unit "jta-data-source" or "non-jta-data-source" (the JNDI name for the DataSource). So create the DataSource, make it accessible via JNDI, and then you can use it in the EMF and this "dbutils".
I am using JPA and I use Entity XML mapping Files to config my persistence layer on the other hand I have multiple database schemas in an Oracle database
In the persistence configuration, i want to have only one EntityManagerFactory and in fact i want to have connection with database with only one schema(USER).
Statically, I can specify schema name one by one to my Entities in Mapping files like this:
<entity class="package.MyClass" name="MyClass">
<table name="MYTABLE" schema="mySchema"/>
My persistence provider in this project is: org.hibernate.ejb.HibernatePersistence
I want to use a placeholder for specifying schema and replace that when JPA scan mapping files to configure itself.
<entity class="package.MyClass" name="MyClass">
<table name="MYTABLE" schema="#placeholder"/>
at config time replace #placeholder with mySchema
Is anyway to do this?
Which part (class) in JPA scan XML mapping files and can i customize that class to do this replace for me?
I think you can use the bootstrap procedure to access the EntityManagerFactory and the EntityManager at runtime to override the properties.
Good day guys, I'm programming an OSGi bundle in charge of authenticating users. For design reasons I'm obligated to perform queries over multiple database schemas (these schemas can be created or deleted dynamically). I'm running MySQL as storage engine.
Somehow I need to be able to create on demand entity managers for those schemas, but I haven't been successful in my attempts. Here is what I have tried that has got me closer to what I need:
Creating a persistence unit (Eclipselink) using a JTA data source that can, in fact, establish a database connection to the default schema. However, when I try to override any property, say, the javax.persistence.jdbc.url. However, it keeps pointing to the default schema always.
I believe I'm not overriding correctly the property, or that JTA datasource properties cannot be modified from EntityManager to EntityManager. Here is how I create the EntityManagers:
Map<String, String> dbProps = new HashMap<String, String>();
dbProps.put("javax.persistence.jdbc.url","jdbc:mysql://mydomain:3306/mydynamicdb);
EntityManagerFactory fact = Persistence.createEntityManagerFactory("myPersistenceUnit", dbProps);
EntityManager myEM = fact.createEntityManager();
At the end, they all keep poiting to the default schema, so my questions are:
Is this an efficient approach for dynamic EntityManager handling? if so, how can I override effectively the schema property?
Is there any other alternative besides the overriding?
I thank you in advance for any guidance you may provide.
If you want to use EclipseLink in OSGi you must use the Gemini JPA project that wraps EclipseLink and create and register an EntityManagerFactory and an EntityManagerFactoryBuilder services for your PU bundle.
If want to share the jdbc connections between PUs you can use JDBC services provided by Gemini DBAccess
You should be able to acquire the EntityManagerFactory as an OSGi service, you can (LDAP) filter the correct one using the service property osgi.unit.name as in:
ServiceReference[] refs = null;
String filter = "(osgi.unit.name=myPersistenceUnit)";
ServiceReference[] refs = ctx.getServiceReferences(EntityManagerFactory.class.getName(), filter);
//Should only be one reference, check (throw exception etc)
return (EntityManagerFactory)ctx.getService(refs[0]);
You can check that all Persistence Units are present, by listing the EntityManagerFactory services (with properties) in your OSGi shell.
I'm developping a program that uses JPA, and I deployed it in a single jar. I wish to modify (or ask to the user, in an menĂº item) the configuration of the connection data with the database server.
It's possible to modify the data (user, password, ip) of the server "on the fly"?
(I apologize my bad english)
how about making a HashMap<String,Object> containing your options and passing it to Persistence.createEntityManagerFactory("unitName",map)?
From the Docs:
public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties)
Create and return an EntityManagerFactory for the named persistence unit using the given properties.
Source
The JPA spec doesn't allow for dynamic modification of persistence-units. Some implementations may provide an implementation-specific way of defining a persistence-unit dynamically. With DataNucleus JPA we do it as per the foot of this page