Working jpa example with hibernate
authorChristian Schneider <chris@die-schneider.net>
Wed, 27 Apr 2016 12:15:01 +0000 (14:15 +0200)
committerChristian Schneider <chris@die-schneider.net>
Wed, 27 Apr 2016 12:15:01 +0000 (14:15 +0200)
samples/jpa/src/main/java/sample/jpa/Task.java
starters/karaf-boot-starter-blueprint/pom.xml
starters/karaf-boot-starter-jpa/pom.xml
starters/karaf-boot-starter-jpa/src/main/java/org/apache/karaf/boot/jpa/Hibernate.java
starters/karaf-boot-starter-jpa/src/main/java/org/apache/karaf/boot/jpa/impl/JpaProcessor.java

index cd431a7..2391d2a 100644 (file)
@@ -18,20 +18,21 @@ package sample.jpa;
 
 import javax.persistence.Entity;
 import javax.persistence.Id;
-import javax.xml.bind.annotation.XmlRootElement;
 
 import org.apache.karaf.boot.jpa.Hibernate;
 import org.apache.karaf.boot.jpa.PersistentUnit;
 import org.apache.karaf.boot.jpa.Property;
 import org.apache.karaf.boot.jpa.Provider;
 
-@PersistentUnit(name = "tasklist", provider = Provider.Hibernate, properties = {
-        @Property(name = "hibernate.hbm2ddl.auto", value = "create-drop")
-})
+@PersistentUnit( //
+    name = "task", provider = Provider.Hibernate, properties = //
+    {
+     @Property(name = "hibernate.hbm2ddl.auto", value = "create-drop"),
+     @Property(name = "javax.persistence.jdbc.driver", value = "org.apache.derby.jdbc.EmbeddedDriver"),
+     @Property(name = "javax.persistence.jdbc.url", value = "jdbc:derby:memory:DSFTEST;create=true")
+    })
 @Hibernate.Dialect(Hibernate.Dialect.Value.HSQL)
-@Hibernate.Hbm2DdlAuto(Hibernate.Hbm2DdlAuto.Value.CreateDrop)
 @Entity
-@XmlRootElement
 public class Task {
     @Id
     Integer id;
@@ -40,7 +41,6 @@ public class Task {
     public Task() {
     }
 
-
     public Task(Integer id, String title) {
         super();
         this.id = id;
index 14db02e..e958058 100644 (file)
             <version>1.0.0</version>
         </dependency>
         <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>osgi.cmpn</artifactId>
+            <version>${osgi.version}</version>
+        </dependency>
+        <dependency>
             <groupId>org.ops4j.pax.cdi</groupId>
             <artifactId>pax-cdi-api</artifactId>
-            <version>0.5.0</version>
+            <version>${pax.cdi.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>javax.enterprise</groupId>
+            <artifactId>cdi-api</artifactId>
+            <version>1.2</version>
         </dependency>
         <dependency>
-            <groupId>javax.inject</groupId>
-            <artifactId>javax.inject</artifactId>
-            <version>1</version>
+            <groupId>org.apache.geronimo.specs</groupId>
+            <artifactId>geronimo-atinject_1.0_spec</artifactId>
+            <version>1.0</version>
         </dependency>
     </dependencies>
 
index 7a89d90..6db476f 100644 (file)
 
     <dependencies>
         <dependency>
-            <groupId>org.apache.geronimo.specs</groupId>
-            <artifactId>geronimo-jpa_2.0_spec</artifactId>
-            <version>1.1</version>
+            <groupId>org.hibernate.javax.persistence</groupId>
+            <artifactId>hibernate-jpa-2.1-api</artifactId>
+            <version>1.0.0.Final</version>
         </dependency>
         <dependency>
+            <groupId>org.apache.aries.jpa</groupId>
+            <artifactId>org.apache.aries.jpa.api</artifactId>
+            <version>2.3.0</version>
+        </dependency>
+        
+        <!-- For the stax indenting -->
+        <dependency>
             <groupId>net.java.dev.stax-utils</groupId>
             <artifactId>stax-utils</artifactId>
             <version>20070216</version>
index ae4dab3..0944c20 100644 (file)
@@ -22,13 +22,19 @@ public interface Hibernate {
     @interface Hbm2DdlAuto {
         Value value();
         enum Value {
-            Validate,
-            Update,
-            Create,
-            CreateDrop;
+            Validate("validate"),
+            Update("update"),
+            Create("create"),
+            CreateDrop("create-drop");
+            
+            private String value;
+
+            private Value(String value) {
+                this.value = value;
+            }
 
             public String toString() {
-                return super.toString().toLowerCase();
+                return value;
             }
         }
     }
index f160fec..622643b 100644 (file)
@@ -32,6 +32,8 @@ import javanet.staxutils.IndentingXMLStreamWriter;
 
 public class JpaProcessor extends AbstractProcessor {
 
+    private boolean useHibernate;
+
     public JpaProcessor() {
     }
 
@@ -59,7 +61,11 @@ public class JpaProcessor extends AbstractProcessor {
                 processingEnv.getMessager().printMessage(Kind.ERROR, "Error: " + e.getMessage());
             }
             try (PrintWriter w = appendResource("META-INF/org.apache.karaf.boot.bnd")) {
-                w.println("Private-Package: META-INF");
+                w.println("Private-Package: META-INF.*");
+                w.println("Meta-Persistence: META-INF/persistence.xml");
+                if (useHibernate) {
+                    w.println("Import-Package: org.hibernate.proxy, javassist.util.proxy");
+                }
             } catch (Exception e) {
                 processingEnv.getMessager().printMessage(Kind.ERROR, "Error writing to META-INF/org.apache.karaf.boot.bnd: " + e.getMessage());
             }
@@ -164,6 +170,7 @@ public class JpaProcessor extends AbstractProcessor {
         if (pu.provider() != null) {
             switch (pu.provider()) {
             case Hibernate:
+                useHibernate = true;
                 return "org.hibernate.jpa.HibernatePersistenceProvider";
             default:
                 // TODO