You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Martin Vysny <mv...@whitestein.com> on 2009/01/08 16:02:35 UTC
unused persistence units
Hi guys,
we have several persistence.xml files in our application just for
testing purposes. They are placed in src/test/resources/... and thus not
packaged to the final product. However Eclipse places them on the
classpath which cause OpenEJB to see them. Can OpenEJB be configured to
somehow ignore particular persistence.xml files? The problem is that
OpenEJB initializes these persistence.xml files and it causes Hibernate
to create-drop the development database, and this is what I'd like to
avoid ;)
Thanks!
Sincerely,
Martin
Example of persistence.xml which ruins your database: ;)
<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="ls-bpm" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<mapping-file>META-INF/ls-bpm-entities.xml</mapping-file>
<mapping-file>META-INF/ls-bpm-entities-test.xml</mapping-file>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.connection.driver_class"
value="org.h2.Driver" />
<property name="hibernate.connection.url"
value="jdbc:h2:mem:acmtest" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>
Re: unused persistence units
Posted by David Blevins <da...@visi.com>.
On Jan 8, 2009, at 9:24 AM, Marcin Kwapisz wrote:
>> we have several persistence.xml files in our application just for
>> testing purposes. They are placed in src/test/resources/... and thus
>> not packaged to the final product. However Eclipse places them on the
>> classpath which cause OpenEJB to see them. Can OpenEJB be
>> configured to
>> somehow ignore particular persistence.xml files? The problem is that
>> OpenEJB initializes these persistence.xml files and it causes
>> Hibernate
>> to create-drop the development database, and this is what I'd like to
>> avoid ;)
> [Marcin Kwapisz]
> Hi Martin,
> If you can use maven, the following solution may work for you.
> 1. Use assembly plugin to build application, for tests
> assembly:directory-single is used to place all files in one directory.
> 2. Set that directory as classesDirectory in surefire-plugin
> 3. Place test resources wherever you want, outside /src/test
> directory. I use /src/alt/resources (see resources config).
> 4. In test code set classpath.include to */target/test-classes/ to
> make OpenEjb see test classes.
> 5. activate profile and test application
Inspired by this technique and Quintin's technique, I've added this as
a new feature so support for altDD (alternative deployment
descriptors) can be done in a test or other environment without so
much work.
Basically, just set the new "openejb.altdd.prefix" system property or
initialcontext property to something like "test", then any descriptors
in your META-INF/ directory that start with "test." will override the
regular descriptor. So for example with an app like this:
META-INF/ejb-jar.xml
META-INF/test.ejb-jar.xml
META-INF/persistence.xml
META-INF/test.env-entry.properties
Just initialize your test case like so:
Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory");
properties.setProperty("openejb.altdd.prefix", "test");
InitialContext initialContext = new InitialContext(properties);
The logical result will be:
META-INF/ejb-jar.xml (via test.ejb-jar.xml)
META-INF/persistence.xml
META-INF/env-entry.properties (via test.env-entry.properties)
This will work in any environment in which OpenEJB works (embedded,
standalone, tomcat, geronimo, etc.).
Feedback welcome, we can tweak and add as desired. I've published new
3.1.1-SNAPSHOT jars. Give a try and let us know how it goes.
-David
RE: unused persistence units
Posted by Marcin Kwapisz <mk...@zsk.p.lodz.pl>.
> we have several persistence.xml files in our application just for
> testing purposes. They are placed in src/test/resources/... and thus
> not packaged to the final product. However Eclipse places them on the
> classpath which cause OpenEJB to see them. Can OpenEJB be configured to
> somehow ignore particular persistence.xml files? The problem is that
> OpenEJB initializes these persistence.xml files and it causes Hibernate
> to create-drop the development database, and this is what I'd like to
> avoid ;)
[Marcin Kwapisz]
Hi Martin,
If you can use maven, the following solution may work for you.
1. Use assembly plugin to build application, for tests assembly:directory-single is used to place all files in one directory.
2. Set that directory as classesDirectory in surefire-plugin
3. Place test resources wherever you want, outside /src/test directory. I use /src/alt/resources (see resources config).
4. In test code set classpath.include to */target/test-classes/ to make OpenEjb see test classes.
5. activate profile and test application
The following profile is used to test toplink, but we have openjpa and hibernate profiles also. By changing profile I am able to switch my application to use different JPA provider.
<profile>
<id>TestEJB_Toplink</id>
<activation>
<property>
<name>env</name>
<value>testtoplink</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>toplink.essentials</groupId>
<artifactId>toplink-essentials-agent</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>toplink.essentials</groupId>
<artifactId>toplink-essentials</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-2</version>
<executions>
<execution>
<id>create-ejb-module</id>
<phase>compile</phase>
<goals>
<goal>directory-single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archiverConfig>
<duplicateBehavior>fail</duplicateBehavior>
</archiverConfig>
<descriptors>
<descriptor>src/main/assembly/ejbModule.xml</descriptor>
</descriptors>
<archive>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- <useSystemClassLoader>true</useSystemClassLoader>
<useManifestOnlyJar>false</useManifestOnlyJar>-->
<classesDirectory>${project.build.directory}/${project.build.finalName}.dir</classesDirectory>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/alt/resources</directory>
</resource>
</resources>
</build>
</profile>