OSGI JPA Persistence - Handling multiple model bundles persistence.xml - jpa

In my project i have a persistence bundle which will perform the create/update/delete operations and multiple model bundles.
persistence bundle:
PersistenceService.java - Exposed as a service.
PersistenceServiceImpl.java
persistence.xml - contains:
a: jta-data-source details
b: dialect properties.
Now i have multiple model bundles where i have used annotations for declaring entities.
My query is, how i can specify the different model bundles details in the above mentioned persistence bundle's persistence.xml. so that the i can maintain a single persistence.xml file and "PersistenceUnitInfo" will not be repeated.
is it possible? or is there any other best practices?

Related

Packaging JPA entities in a jar inside a Spring Boot application

I am refactoring a JEE REST (using JAX-RS 2.0) application as a Spring Boot application. My old app is packaged in a .war and has a jar file with entities and the persistence.xml configuration file for JPA. This jar is copied into WEB-INF/lib directory. I know Spring JPA works a different way and I don't use persistence.xml now but I wonder if I can package my JPA entity classes in a jar and include them in my Spring Boot apps just like I am doing now. This way I can easily reuse that jar in different Spring Boot Applications.
I'm pretty certain you can do this since I have done the same on one of my projects very recently. The only thing you need to do is make sure that you add an #EntityScan annotation on your main Spring Boot config class with the base package of your entities in the JAR.
#EntityScan("my.external.jar.entity.package")
Spring Boot doesn't really care whether the JPA entities are packages as a separate jar or included into the application. Its a runtime framework and in runtime classes can be loaded from the jar (it should reside in BOOT-INF/lib or 'directly' from the *.class files in the spring boot artifact.
Now there is a rule in spring boot, that says that it will scan for beans (including entities) only in the package where your "main" class resides or under it. This is done in order to avoid long process of analysis of, say, third-party classes that you might use. These third-party classes are usually not spring aware at all, at certainly do not contain any spring beans.
Example:
Say, you place your "main" class (the one annotated with #SpringBootApplication) in the package: com.mycompany.myapp
In this case, the following packages will be scanned (just a couple of examples):
com.mycompany.myapp
com.mycompany.myapp.web
com.mycompany.myapp.services.bl
com.mycompany.myapp.whatever.doesnt.matter
...
The following packages won't be scanned however (again, examples, not the full list):
com.mycompany
com.anothercompany
org.hibernate
If you want to to "alter" this default rule and place the entities in the package that doesn't adhere this convention, for example com.mycompany.jpa.entities then you should indeed use #EntityScan annotation as our colleagues have already suggested.
You can read about this topic here. You might also need to get familiar with #EnableJpaRepositories if you're using spring data but, while related, its a different topic.
In my case I had this problem, and after importing the library in the application's pom.xml, in the SpringBoot Project Main class, insert an #EntityScan annotation with the first package and *. Like this: #EntityScan ("br.*")

It is possible to override properties in a persistence.xml that is located in a jar dependency

I have a java-ee web application that uses a persistence unit that is packaged as a jar dependency (Entity classes, EJB repositories, persistence.xml).
In order to get some acceptance tests running for the web application i need to override a property in the packaged persistence.xml. To be specific i need to disable the by default active eclipselink shared object cache by setting the following property.
<property name="eclipselink.cache.shared.default" value="false"/>
This is necessary because the acceptance tests are directly prepare/cleanup the database with dbunit. These modifications will put the eclipselink cache in a stale state (because the persistence unit is not involved in these modifications).
Is there a way in java-ee (or glassfish specific) to override properties in a persistence.xml that is located in a jar (starting from the web application war file, that is deployed when running my tests)?
There may be other ways, for example building the jar dependency specific for a test deployment, but this route seems complicated to me for only override one property in my persistence.xml.
You can pass a properties map to Persistence.createEntityManagerFactory(). To do this you must manage your persistence context yourself (will not be able to inject it).
Another option is to set the property as a Java system property (-D=), this will not override an existing property in the persistence.xml, but with work if the property is not in the persistence.xml.
Another option is to put a SessionCustomizer or a SessionTuner in your persistence.xml to allow your own code to modify the configuration at runtime.

Value of provider in Persistence.xml

What is the value to be set in provider XML element of persistence.xml if the Oracle database is used? Which jars need to included to write a simple JPA application?.
I have currently included only ejb3-persistence.jar.
When the application is run, below error is seen,
javax.persistence.PersistenceException: No Persistence provider for EntityManage
Decide which JPA implementation you're going to use :- some to choose from DataNucleus, OpenJPA, Hibernate, EclipseLink all of which support persistence to Oracle RDBMS, and there are likely others. Each has a "provider" class name, so you use that in persistence.xml

How to do Application specific Configuration for a Framework

i want to write a Java EE framework for a generic type of applications.
I'm looking for a way to handle application specific config values in my framework.
To give an example:
A component supplies a stateless session bean that handles persistence and i want to configure the name of the datasource that is used in a config file in my application (for example in the web-inf folder of the applications ear).
Now i have X>1 Applications that want to configure X different datasources for their specific persistence management.
Can anybody give me an example how to do that?
Greetings,
Alexander
You should use the JNDI provider thats bundled with the application container. One of its purposes it to access resources, data sources in your case, in a highly dynamic fashion.
For JBoss, setting up a data source involves the following steps:
deploy a *-ds.xml configuration file to the JBoss server's deploy directory. This defines the global JNDI name of the resources. There should be plenty of examples for a lot of databases available on the internet.
add a resource-ref to the jboss-web.xml of any WAR or to the jboss.xml of any EJB jar for any bean that needs the resource. This defines the global JNDI name to local/component JNDI name mapping.
add a resource-ref using the local JNDI name to the web.xml of any WAR or to the ejb-jar.xml of any EJB jar for any bean that needs the resource.
Once those things are in place, you can perform JNDI lookups to access the configured resource.
You can do these things to configure multiple data sources in JBoss and then make one or more of these data sources available to the web applications and EJBs.

EclipseLink, EntityManager with two persistence units needed

I have one jar library A (or project in eclipse), which has it's own persistence unit (META-INF/persistence.xml) and some entity classes, and another project (B) using this one. In project B there is also persistence unit and entity classes.
In project B I need to use both entity classes from project A and B. But if I set "A" as persistence unit name, EntityManager cannot create named query if this query is in entity from project B. If I set "B" as persistence unit name, it cannot create named queries from entities from project A. Error message is:
NamedQuery of name: MyEntityName.myQueryName not found.
Can persistence units somehow include other persistence units? Or is there any other way to solve this problem?
EclipseLink 2.3 introduced Composite Persistence Units, which allows you to create a persistence unit that essentially acts only as a container for two or more actual persistence units. You are then able to use this single composite persistence unit in your application as if you had only one persistence unit. This should meet your goals of keeping your persistence.xml files clean for easy synchronization of your model to database. Pretty cool stuff.
You can list your classes needed by A in one persistence unit, and classes needed you B in an other:
<persistence ...>
<persistence-unit name="projectA" ...>
....
<class>a.Class1</class>
<class>a.Class2</class>
<class>a.Class3</class>
</persistence-unit>
<persistence-unit name="projectB" ...>
...
<class>a.Class1</class>
<class>a.Class2</class>
<class>a.Class3</class>
<class>b.Class1</class>
<class>b.Class2</class>
<class>b.Class3</class>
</persistence-unit>
</persistence>
Alternatively, you can use the <jar-file> element, quoting from the JPA spec (6.2.1.6): "If specified, these JAR files will be searched for managed persistence classes, and any mapping metadata annotations found on them will be processed, or they will be mapped using the mapping annotation defaults defined by this specification. Such JAR files are specified relative to the root of the persistence unit (e.g., utils/myUtils.jar)."
<persistence ...>
<persistence-unit name="projectA" ...>
...
<jar-file>relative/path/to/your/library.jar</jar-file>
</persistence-unit>
</persistence>