diff options
-rw-r--r-- | .gitignore | 6 | ||||
-rw-r--r-- | pom.xml | 68 | ||||
-rw-r--r-- | src/main/java/nu/xss/jpa/dao/AbstractJpaDao.java | 40 | ||||
-rw-r--r-- | src/main/java/nu/xss/jpa/dao/Dao.java | 10 | ||||
-rw-r--r-- | src/main/java/nu/xss/jpa/dao/GenericJpaDao.java | 43 | ||||
-rw-r--r-- | src/main/java/nu/xss/jpa/entity/Entity.java | 5 |
6 files changed, 172 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c126559 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.classpath +.settings +.project +target +build +bin @@ -0,0 +1,68 @@ +<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/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>nu.xss</groupId> + <artifactId>xss-jpa</artifactId> + <version>0.0.1-SNAPSHOT</version> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <hibernate.version>4.1.4.Final</hibernate.version> + </properties> + + <dependencies> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-core</artifactId> + <version>${hibernate.version}</version> + </dependency> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-entitymanager</artifactId> + <version>${hibernate.version}</version> + </dependency> + </dependencies> + + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>2.5</version> + <configuration> + <source>1.7</source> + <target>1.7</target> + </configuration> + </plugin> + <!-- No dependencies in jar for helpers.. + <plugin> + <artifactId>maven-assembly-plugin</artifactId> + <configuration> + <descriptorRefs> + <descriptorRef>jar-with-dependencies</descriptorRef> + </descriptorRefs> + </configuration> + </plugin> + --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <version>2.8.1</version> + <configuration> + <stylesheetfile>${basedir}/src/main/javadoc/stylesheet.css</stylesheetfile> + <show>public</show> + </configuration> + </plugin> + </plugins> + </build> + + <distributionManagement> + <repository> + <id>nexus-deploy</id> + <name>Nexus Staging Repo</name> + <url>http://localhost:8081/nexus/content/repositories/snapshots/</url> + </repository> + </distributionManagement> + +</project>
\ No newline at end of file diff --git a/src/main/java/nu/xss/jpa/dao/AbstractJpaDao.java b/src/main/java/nu/xss/jpa/dao/AbstractJpaDao.java new file mode 100644 index 0000000..aef1aaf --- /dev/null +++ b/src/main/java/nu/xss/jpa/dao/AbstractJpaDao.java @@ -0,0 +1,40 @@ +package nu.xss.jpa.dao; + +import java.io.Serializable; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +public abstract class AbstractJpaDao<E, K> implements Dao<E, K>, Serializable { + + private static final long serialVersionUID = 4998055731089977476L; + + @PersistenceContext(unitName="mainPU") + protected EntityManager entityManager; + + protected Class<E> entity; + + public AbstractJpaDao(final Class<E> entity) { + this.entity = (Class<E>) entity; + } + + public void persist(E entity) { + entityManager.persist(entity); + + } + + public void remove(E entity) { + entityManager.remove(entity); + } + + public E findById(K id) { + return entityManager.find(entity, id); + } + + public List<E> findAll() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/main/java/nu/xss/jpa/dao/Dao.java b/src/main/java/nu/xss/jpa/dao/Dao.java new file mode 100644 index 0000000..b91c834 --- /dev/null +++ b/src/main/java/nu/xss/jpa/dao/Dao.java @@ -0,0 +1,10 @@ +package nu.xss.jpa.dao; + +import java.util.List; + +public interface Dao<E, K> { + void persist(E entity); + void remove(E entity); + E findById(K id); + List<E> findAll(); +} diff --git a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java new file mode 100644 index 0000000..5acfffc --- /dev/null +++ b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java @@ -0,0 +1,43 @@ +package nu.xss.jpa.dao; + +import java.io.Serializable; +import java.lang.reflect.ParameterizedType; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +public abstract class GenericJpaDao<E,K> implements Dao<E,K>, Serializable { + + private static final long serialVersionUID = 4998055731089977476L; + + @PersistenceContext + protected EntityManager entityManager; + + protected Class<E> entity; + + @SuppressWarnings("unchecked") + public GenericJpaDao() { + ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass(); + this.entity = (Class<E>) genericSuperclass.getActualTypeArguments()[1]; + } + + public void persist(E entity) { + entityManager.persist(entity); + + } + + public void remove(E entity) { + entityManager.remove(entity); + } + + public E findById(K id) { + return entityManager.find(entity, id); + } + + public List<E> findAll() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/main/java/nu/xss/jpa/entity/Entity.java b/src/main/java/nu/xss/jpa/entity/Entity.java new file mode 100644 index 0000000..1943e80 --- /dev/null +++ b/src/main/java/nu/xss/jpa/entity/Entity.java @@ -0,0 +1,5 @@ +package nu.xss.jpa.entity; + +public class Entity { + +} |