Can I have two persistence.xml files in my spring maven project - jpa

Is it possible for me to have two different persistence.xml files under META-INF eg. persistence-one.xml and persistence-two.xml and then somehow use <property name="persistenceXmlLocation" value="${db.persistence.file.name}"/> in my spring-context.xml to use the appropriate one using property files ?
I am doing this because I have two separate environments with different configurations - providers/dialects etc.
If I do above changes then I get Caused by: java.io.FileNotFoundException: and it's not able to read the appropriate file.
How I can make this work ?
Spring 3, Hibernate 5.3

Whoaa... spring 3 with hibernate 5 is going to be... challenging.
In any case, I'd try using <property name="persistenceProvider" value="org.hibernate.jpa.HibernatePersistenceProvider" /> instead of jpaVendorAdapter.
You might want to take a look at the bean's javadoc to see which other properties might be relevant (alternatively, you can use the persistenceXmlLocation property and load all the properties from either persistence_dev.xml or persistence_prod.xml)

Related

OpenJPA > Is it possible to define persistence xml file somewhere else than META-INF with name persistence.xml

I am a new OpenJPA 2.2.2 user. I noticed that the database configuration has to be defined in META-INF/persistence.xml. This sounds to be too inflexible. Moreover, I found from the OpenJPA 2.2.2 documentation:
The OpenJPA runtime includes a comprehensive system of configuration defaults and overrides:
OpenJPA first looks for an optional openjpa.xml resource. OpenJPA searches for this resource in each top-level directory of your CLASSPATH. OpenJPA will also find the resource if you place it within a META-INF directory in any top-level directory of the CLASSPATH. The openjpa.xml resource contains property settings in JPA's XML format.
You can customize the name or location of the above resource by specifying the correct resource path in the openjpa.properties System property.
On base the introduction above, it seems that the database configuration file name is only possible to be persistence.xml. Moreover, I tried to place it in the top-level directory of my CLASSPATH, i.e., not inside the META-INF directory, it didn't work at all! Is there is any way to define this database persistence xml more flexibly, say in somewhere else than META-INF and with other name than persistence.xml?
First, there is no replacement for persistence.xml (p.xml for short). This is needed no matter what. Furthermore, the location of this file is clearly defined in the JPA specification (e.g. see "8.2 Persistence Unit Packaging" in JPA 2.0 spec). You MUST follow those rules on packaging. OpenJPA first and foremost follows the rules of the spec in regards to this file and its settings. No JPA provider is going to ignore the rules w.r.t p.xml file.
The section of text you copy/pasted is from here in the OpenJPA documentation:
http://openjpa.apache.org/builds/2.2.2/apache-openjpa/docs/manual#ref_guide_conf_specify
As you can see, this section describes an optional file named openjpa.xml. This file does not replace a p.xml file! It adds to it. And as the documentation states, it is simply used to add properties. That is, you can not define your persistence unit here, the only thing you can define are properties (i.e. ). If you define a persistence unit here, it will not be used. As an example, lets look at this openjpa.xml file:
<?xml version="1.0"?>
<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="">
<properties>
<property name="openjpa.jdbc.Schema" value="MySchema" />
<property name="openjpa.jdbc.DBDictionary" value="db2" />
</properties>
</persistence-unit>
This file defines two properties. Notice that the persistence-unit name is blank. That is intentional because, as I mentioned above, we can't define a persistence unit in this file. Finally, you can place this file in the META-INF on your classpath, as mentioned in the documentation. If you put this at the same spot as your p.xml file it will be found. As an example, in my JSE JUnit test, I place it on my hard drive in the directory e:/openjpaConfig/META-INF/openjpa.xml. I then put a classpath entry in my JUnit test to point to the directory e:/openjpaConfig. The classpath could point to a jar which contains this file in its META-INF directory. In a Java EE environment, you could place the file in a .jar file and place the .jar file in the lib directory of an ear.

Eclipse: open declaration/resource for spring beans and contexts

While watching one of my colleagues work in IntelliJ, I was jealous to note that he could Ctrl/Command+Click on the name of a spring bean, context, or resource in either a Java annotation or a Spring context file and IntelliJ would open the backing declaration or file.
I've installed the STS suite of plugins for Eclipse, but this has only given me the ability to open declarations for beans referred to in a spring context file. E.g., command+click only works in my context file for: <bean ... ref="bean-name"/>.
Specifically, things I can't open the declaration/resource for automatically are:
In my context file: <import resource="classpath:config/anotherContext.xml"/>, I cannot open the resource anotherContext.xml. The open resource dialogue helps, but doesn't tell me which one my classpath is actually giving me.
In my Java file: #ContextConfiguration({"classpath:/config/yetAnotherContext.xml"}), I cannot automatically open the resource of yetAnotherContext.xml.
In my Java file: #Resource(name = "another-bean-name"), I cannot open the bean declaration. This one is especially difficult to discover in Eclipse, I haven't figured out an easy way to discover the bean declaration short of full search.
Surely there's a way in Eclipse to automatically discover declarations or resources for spring beans and contexts as they are used in both Java annotations and context files?
I found a solution for my first example, specifically, finding a spring resource imported into a spring context file in this StackOverflow question. Specifically, in the project properties under Spring->Beans Support, select the checkbox for Enable support for <import /> element in configuration files.
This also gives the ability to open bean declarations for beans defined in imported spring resource files.
I have yet to discover similar functionality for Java files that wire in Spring resources and beans via annotations.

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.

Griffon & JPA : environment configuration in persistence.xml

Using Griffon 1.2 and JPA is there any way to configure the persistence.xml with environmental properties - so I can have a different jdbc.url for dev/test/prod ?
i.e. conf/metainf/persistence.xml
<property name="javax.persistence.jdbc.url" value="${javax.persistence.jdbc.url}" />
I was hoping something like this would work in JpaConfig.groovy
environments {
development {
persistenceUnit {
entityManager {
// EntityManager properties go here
javax.persistence.jdbc.url = 'jdbc:h2:mem:sample'
}
}
}...
I could create multiple duplicate persistence xml files and I believe I can specify the active persistence unit in JpaConfig.groovy. Or is there some other way to parameterize the JPA configuration per environment?
Thanks
I'm afraid parameterizable persistence.xml files are not supported at the moment because those files are never touched by the plugin; however this would be a nice addition to the plugin. Would you care to register a ticket at http://jira.codehaus.org/browse/griffon ?
In the meantime, setting all relevant properties in JpaConfig.groovy is the only way to achieve this.

Transforming XSD Import for WSDL in Spring WS

I'm tring to extend the example in chapter 5 of the Spring WS guide. I'm using Spring WS 1.5.9 .
I've added ...
<import namespace="http://myco.com/schemas/promotion/v1_2"
schemaLocation="http://localhost:8080/ordersService/Promotion_1_2.xsd" /> ...
But spring doesn't appear to be transforming the location of the import like it does for the port. So on my company website it still shows "localhost:8080"
I have the WSDL generation defined as such
<bean id="orders" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
<property name="schema" ref="schema" />
<property name="portTypeName" value="Orders" />
<property name="locationUri" value="http://localhost:8080/ordersService/" />
Does/Can Spring Framework transform the imports some way ???
UPDATE: Some more background....
We use the same XSD with the maven JaxB2 plugin to build our response schema objects. That all works great, and we use the Catalog resolver to actually find the addional business domain XSDs in an included JAR file. We have 100's of XSDs that describe our business domain.
SO i'm trying not to break that.
What I would like to see is a more detailed example. An example where JaxB2 is used and XSD -> WSDL functionality.
Should I refactor the XSDs ?
Should I use a WSDL and not an XSD
How do I properly idenitfy the XSDs to spring ?
Do I use "classpath:My_file.xsd" ? Will I have to list possibly 100's of XSDs to schemaCollection ?
The spring Docs are great but I would like a practical example that matches.
I want to add detail on the solution we ended up with. We have been moving to Spring WS 2x. Here's an updated link...
http://static.springsource.org/spring-ws/site/reference/html/server.html#server-automatic-wsdl-exposure
The trick / key was in that section it says... "If you want to use multiple schemas, either by includes or imports, you will want to put Commons XMLSchema"
So that was the final answer to add that to our project.
<dependency>
<groupId>org.apache.ws.commons.schema</groupId>
<artifactId>XmlSchema</artifactId>
<version>1.4.7</version>
</dependency>
Now spring enables additional functionality to allow classpath resolution to work.
Spring-WS can automagically inline all schema elements directly into the WSDL, so that they appear as a single document. This avoids the problem of inaccessible <import> URLs.
See the section of the Spring WS manual which talks about CommonsXsdSchemaCollection.