Author: dblevins
Date: Tue Mar 4 21:28:11 2008
New Revision: 633773
URL: http://svn.apache.org/viewvc?rev=633773&view=rev
Log:
Example showing EJB3 and JPA with Hibernate
Added:
openejb/trunk/openejb3/examples/jpa-hibernate/
openejb/trunk/openejb3/examples/jpa-hibernate/pom.xml
openejb/trunk/openejb3/examples/jpa-hibernate/src/
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/injection/
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/injection/h3jpa/
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/injection/h3jpa/Movie.java
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/injection/h3jpa/Movies.java
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/injection/h3jpa/MoviesImpl.java
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/resources/
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/resources/META-INF/
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/resources/META-INF/ejb-jar.xml
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/resources/META-INF/persistence.xml
openejb/trunk/openejb3/examples/jpa-hibernate/src/test/
openejb/trunk/openejb3/examples/jpa-hibernate/src/test/java/
openejb/trunk/openejb3/examples/jpa-hibernate/src/test/java/org/
openejb/trunk/openejb3/examples/jpa-hibernate/src/test/java/org/superbiz/
openejb/trunk/openejb3/examples/jpa-hibernate/src/test/java/org/superbiz/injection/
openejb/trunk/openejb3/examples/jpa-hibernate/src/test/java/org/superbiz/injection/h3jpa/
openejb/trunk/openejb3/examples/jpa-hibernate/src/test/java/org/superbiz/injection/h3jpa/MoviesTest.java
Added: openejb/trunk/openejb3/examples/jpa-hibernate/pom.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/jpa-hibernate/pom.xml?rev=633773&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/jpa-hibernate/pom.xml (added)
+++ openejb/trunk/openejb3/examples/jpa-hibernate/pom.xml Tue Mar 4 21:28:11
2008
@@ -0,0 +1,86 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.superbiz</groupId>
+ <artifactId>jpa-hibernate</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0-SNAPSHOT</version>
+ <name>OpenEJB :: Examples :: JPA with Hibernate</name>
+ <build>
+ <defaultGoal>install</defaultGoal>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <repositories>
+ <repository>
+ <id>apache-m2-snapshot</id>
+ <name>Apache Snapshot Repository</name>
+ <url>http://people.apache.org/repo/m2-snapshot-repository/</url>
+ </repository>
+ </repositories>
+ <dependencies>
+
+ <!-- spec apis such as javax.persistence -->
+ <dependency>
+ <groupId>org.apache.openejb</groupId>
+ <artifactId>javaee-api</artifactId>
+ <version>5.0-1</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.4</version>
+ <scope>test</scope>
+ </dependency>
+
+ <!-- openejb container for running tests -->
+ <dependency>
+ <groupId>org.apache.openejb</groupId>
+ <artifactId>openejb-core</artifactId>
+ <version>3.0-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
+
+ <!-- hibernate dependencies -->
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate</artifactId>
+ <version>3.2.5.ga</version>
+ <exclusions>
+ <exclusion>
+ <artifactId>cglib</artifactId>
+ <groupId>cglib</groupId>
+ </exclusion>
+ </exclusions>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>cglib</groupId>
+ <artifactId>cglib-nodep</artifactId>
+ <version>2.1_3</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>asm</groupId>
+ <artifactId>asm</artifactId>
+ <version>2.2.3</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-entitymanager</artifactId>
+ <version>3.2.1.ga</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
\ No newline at end of file
Added:
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/injection/h3jpa/Movie.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/injection/h3jpa/Movie.java?rev=633773&view=auto
==============================================================================
---
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/injection/h3jpa/Movie.java
(added)
+++
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/injection/h3jpa/Movie.java
Tue Mar 4 21:28:11 2008
@@ -0,0 +1,66 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.injection.h3jpa;
+
+import javax.persistence.*;
+
[EMAIL PROTECTED]
+public class Movie {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.AUTO)
+ private long id;
+
+ private String director;
+ private String title;
+ private int year;
+
+ public Movie() {
+ }
+
+ public Movie(String director, String title, int year) {
+ this.director = director;
+ this.title = title;
+ this.year = year;
+ }
+
+ public String getDirector() {
+ return director;
+ }
+
+ public void setDirector(String director) {
+ this.director = director;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public int getYear() {
+ return year;
+ }
+
+ public void setYear(int year) {
+ this.year = year;
+ }
+
+
+}
Added:
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/injection/h3jpa/Movies.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/injection/h3jpa/Movies.java?rev=633773&view=auto
==============================================================================
---
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/injection/h3jpa/Movies.java
(added)
+++
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/injection/h3jpa/Movies.java
Tue Mar 4 21:28:11 2008
@@ -0,0 +1,32 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.injection.h3jpa;
+
+import org.superbiz.injection.h3jpa.Movie;
+
+import java.util.List;
+
+/**
+ * @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23 -0800 (Thu, 27 Dec
2007) $
+ */
+public interface Movies {
+ void addMovie(Movie movie) throws Exception ;
+
+ void deleteMovie(Movie movie) throws Exception ;
+
+ List<Movie> getMovies() throws Exception ;
+}
Added:
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/injection/h3jpa/MoviesImpl.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/injection/h3jpa/MoviesImpl.java?rev=633773&view=auto
==============================================================================
---
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/injection/h3jpa/MoviesImpl.java
(added)
+++
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/java/org/superbiz/injection/h3jpa/MoviesImpl.java
Tue Mar 4 21:28:11 2008
@@ -0,0 +1,48 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.injection.h3jpa;
+
+import org.superbiz.injection.h3jpa.Movie;
+import org.superbiz.injection.h3jpa.Movies;
+
+import javax.ejb.Stateful;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.Query;
+import javax.persistence.PersistenceContextType;
+import java.util.List;
+
[EMAIL PROTECTED](name = "Movies")
+public class MoviesImpl implements Movies {
+
+ @PersistenceContext(unitName = "movie-unit", type =
PersistenceContextType.EXTENDED)
+ private EntityManager entityManager;
+
+ public void addMovie(Movie movie) throws Exception {
+ entityManager.persist(movie);
+ }
+
+ public void deleteMovie(Movie movie) throws Exception {
+ entityManager.remove(movie);
+ }
+
+ public List<Movie> getMovies() throws Exception {
+ Query query = entityManager.createQuery("SELECT m from Movie as m");
+ return query.getResultList();
+ }
+
+}
Added:
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/resources/META-INF/ejb-jar.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/jpa-hibernate/src/main/resources/META-INF/ejb-jar.xml?rev=633773&view=auto
==============================================================================
---
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/resources/META-INF/ejb-jar.xml
(added)
+++
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/resources/META-INF/ejb-jar.xml
Tue Mar 4 21:28:11 2008
@@ -0,0 +1 @@
+<ejb-jar/>
Added:
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/resources/META-INF/persistence.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/jpa-hibernate/src/main/resources/META-INF/persistence.xml?rev=633773&view=auto
==============================================================================
---
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/resources/META-INF/persistence.xml
(added)
+++
openejb/trunk/openejb3/examples/jpa-hibernate/src/main/resources/META-INF/persistence.xml
Tue Mar 4 21:28:11 2008
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<persistence version="1.0"
+ 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">
+ <persistence-unit name="movie-unit">
+ <provider>org.hibernate.ejb.HibernatePersistence</provider>
+ <jta-data-source>movieDatabase</jta-data-source>
+ <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source>
+ <properties>
+ <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
+ <property name="hibernate.transaction.manager_lookup_class"
+ value="org.apache.openejb.hibernate.TransactionManagerLookup"/>
+ </properties>
+ </persistence-unit>
+</persistence>
Added:
openejb/trunk/openejb3/examples/jpa-hibernate/src/test/java/org/superbiz/injection/h3jpa/MoviesTest.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/jpa-hibernate/src/test/java/org/superbiz/injection/h3jpa/MoviesTest.java?rev=633773&view=auto
==============================================================================
---
openejb/trunk/openejb3/examples/jpa-hibernate/src/test/java/org/superbiz/injection/h3jpa/MoviesTest.java
(added)
+++
openejb/trunk/openejb3/examples/jpa-hibernate/src/test/java/org/superbiz/injection/h3jpa/MoviesTest.java
Tue Mar 4 21:28:11 2008
@@ -0,0 +1,63 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.injection.h3jpa;
+
+import junit.framework.TestCase;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import java.util.Properties;
+import java.util.List;
+
+import org.superbiz.injection.h3jpa.Movie;
+import org.superbiz.injection.h3jpa.Movies;
+
+/**
+ * @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23 -0800 (Thu, 27 Dec
2007) $
+ */
+public class MoviesTest extends TestCase {
+
+ public void test() throws Exception {
+ Properties p = new Properties();
+ p.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory");
+ p.put("movieDatabase", "new://Resource?type=DataSource");
+ p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
+ p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
+
+ p.put("movieDatabaseUnmanaged", "new://Resource?type=DataSource");
+ p.put("movieDatabaseUnmanaged.JdbcDriver", "org.hsqldb.jdbcDriver");
+ p.put("movieDatabaseUnmanaged.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
+ p.put("movieDatabaseUnmanaged.JtaManaged", "false");
+
+ Context context = new InitialContext(p);
+
+ Movies movies = (Movies) context.lookup("MoviesLocal");
+
+ movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs",
1992));
+ movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
+ movies.addMovie(new Movie("Joel Coen", "The Big Lebowski", 1998));
+
+ List<Movie> list = movies.getMovies();
+ assertEquals("List.size()", 3, list.size());
+
+ for (Movie movie : list) {
+ movies.deleteMovie(movie);
+ }
+
+ assertEquals("Movies.getMovies()", 0, movies.getMovies().size());
+ }
+}