[Schmitzm-commits] r765 - in trunk: . src/skrueger src/skrueger/orm src/skrueger/orm/accessor

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Mar 15 17:42:39 CET 2010


Author: alfonx
Date: 2010-03-15 17:42:35 +0100 (Mon, 15 Mar 2010)
New Revision: 765

Added:
   trunk/src/skrueger/orm/
   trunk/src/skrueger/orm/AbstractEntity.java
   trunk/src/skrueger/orm/PersistentEntity.java
   trunk/src/skrueger/orm/accessor/
   trunk/src/skrueger/orm/accessor/AbstractEntityAccessor.java
   trunk/src/skrueger/orm/accessor/EntityAccessorException.java
Modified:
   trunk/pom.xml
Log:
hilfsklannen fuer hibernate

Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml	2010-03-11 19:33:39 UTC (rev 764)
+++ trunk/pom.xml	2010-03-15 16:42:35 UTC (rev 765)
@@ -53,7 +53,31 @@
 		<url>http://www.wikisquare.de</url>
 	</organization>
 	<dependencies>
+	
 		<dependency>
+			<!-- Hibernate  -->
+			<groupId>org.hibernate</groupId>
+			<artifactId>hibernate-commons-annotations</artifactId>
+			<version>3.3.0.ga</version>
+			<scope>compile</scope>
+		</dependency>
+
+		<dependency>
+			<!-- Hibernate  -->
+			<groupId>org.hibernate</groupId>
+			<artifactId>hibernate-entitymanager</artifactId>
+			<version>3.4.0.GA</version>
+			<scope>compile</scope>
+		</dependency>
+	
+	
+		<dependency>
+			<groupId>org.hibernate</groupId>
+			<artifactId>hibernate-annotations</artifactId>
+			<version>3.4.0.GA</version>
+		</dependency>
+	
+		<dependency>
 			<groupId>org.geotools</groupId>
 			<artifactId>gt-render</artifactId>
 			<version>${geotools.version}</version>

Added: trunk/src/skrueger/orm/AbstractEntity.java
===================================================================
--- trunk/src/skrueger/orm/AbstractEntity.java	2010-03-11 19:33:39 UTC (rev 764)
+++ trunk/src/skrueger/orm/AbstractEntity.java	2010-03-15 16:42:35 UTC (rev 765)
@@ -0,0 +1,36 @@
+package skrueger.orm;
+
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.MappedSuperclass;
+
+/**
+ *
+ * @author Dr. Spock (spock at dev.java.net)
+ */
+ at MappedSuperclass
+public abstract class AbstractEntity implements PersistentEntity<Long> {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Long id;
+    
+    public Long getId() {
+        return this.id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    @Override
+    public abstract boolean equals(Object object);
+
+    @Override
+    public abstract int hashCode();
+
+    @Override
+    public abstract String toString();
+    
+}

Added: trunk/src/skrueger/orm/PersistentEntity.java
===================================================================
--- trunk/src/skrueger/orm/PersistentEntity.java	2010-03-11 19:33:39 UTC (rev 764)
+++ trunk/src/skrueger/orm/PersistentEntity.java	2010-03-15 16:42:35 UTC (rev 765)
@@ -0,0 +1,14 @@
+package skrueger.orm;
+
+
+import java.io.Serializable;
+
+/**
+ *
+ * @author Dr. Spock (spock at dev.java.net)
+ */
+public interface PersistentEntity<PK extends Serializable> extends Serializable {
+
+    PK getId();
+
+}

Added: trunk/src/skrueger/orm/accessor/AbstractEntityAccessor.java
===================================================================
--- trunk/src/skrueger/orm/accessor/AbstractEntityAccessor.java	2010-03-11 19:33:39 UTC (rev 764)
+++ trunk/src/skrueger/orm/accessor/AbstractEntityAccessor.java	2010-03-15 16:42:35 UTC (rev 765)
@@ -0,0 +1,96 @@
+package skrueger.orm.accessor;
+
+
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.annotation.Resource;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceUnit;
+import javax.transaction.Status;
+import javax.transaction.UserTransaction;
+
+/**
+ *
+ * @author Dr. Spock (spock at dev.java.net)
+ */
+public abstract class AbstractEntityAccessor {
+
+    @PersistenceUnit
+    private EntityManagerFactory emf;
+    @Resource
+    private UserTransaction userTransaction;
+
+    private AtomicInteger count;
+
+    public AbstractEntityAccessor() {
+        count = new AtomicInteger(0);
+    }
+
+    protected final <T> T doInTransaction(PersistenceAction<T> action) throws EntityAccessorException {
+        EntityManager em = emf.createEntityManager();
+        try {
+            int status = 0;
+            if (Status.STATUS_ACTIVE != (status = userTransaction.getStatus())){
+                count.incrementAndGet();
+                userTransaction.begin();
+            }
+            T result = action.execute(em);
+            if (Status.STATUS_ACTIVE == (status = userTransaction.getStatus())) {
+                if (0 == count.decrementAndGet()) {
+                    userTransaction.commit();
+                }
+            }
+            return result;
+        } catch (Exception e) {
+            try {
+                userTransaction.rollback();
+            } catch (Exception ex) {
+                Logger.getLogger(AbstractEntityAccessor.class.getName()).log(Level.SEVERE, null, ex);
+            }
+            throw new EntityAccessorException(e);
+        } finally {
+            em.close();
+        }
+
+    }
+
+    protected final void doInTransaction(PersistenceActionWithoutResult action) throws EntityAccessorException {
+        EntityManager em = emf.createEntityManager();
+        try {
+            int status = 0;
+            if (Status.STATUS_ACTIVE != (status = userTransaction.getStatus())){
+                count.incrementAndGet();
+                userTransaction.begin();
+            }
+            action.execute(em);
+            if (Status.STATUS_ACTIVE == (status = userTransaction.getStatus())) {
+                if (0 == count.decrementAndGet()) {
+                    userTransaction.commit();
+                }
+            }
+        } catch (Exception e) {
+            try {
+                userTransaction.rollback();
+            } catch (Exception ex) {
+                Logger.getLogger(AbstractEntityAccessor.class.getName()).log(Level.SEVERE, null, ex);
+            }
+            throw new EntityAccessorException(e);
+        } finally {
+            em.close();
+        }
+    }
+
+    protected static interface PersistenceAction<T> {
+
+        T execute(EntityManager em);
+    }
+
+    protected static interface PersistenceActionWithoutResult {
+
+        void execute(EntityManager em);
+    }
+
+}

Added: trunk/src/skrueger/orm/accessor/EntityAccessorException.java
===================================================================
--- trunk/src/skrueger/orm/accessor/EntityAccessorException.java	2010-03-11 19:33:39 UTC (rev 764)
+++ trunk/src/skrueger/orm/accessor/EntityAccessorException.java	2010-03-15 16:42:35 UTC (rev 765)
@@ -0,0 +1,14 @@
+package skrueger.orm.accessor;
+
+public class EntityAccessorException extends Exception {
+
+    private static final long serialVersionUID = 1L;
+
+    public EntityAccessorException(Throwable cause) {
+        super(cause);
+    }
+
+    public EntityAccessorException(String message, Throwable cause) {
+        super(message, cause);
+    }
+}



More information about the Schmitzm-commits mailing list