From f641fe9baf1c9e3ed14606a222675883c9de6e67 Mon Sep 17 00:00:00 2001 From: insanity Date: Thu, 25 May 2017 16:30:53 +0900 Subject: [PATCH] jpa_base_dao impl --- .gitignore | 73 +++++++++++++++++++ pom.xml | 35 +++++++++ .../overflow/commons/dao/JPABaseDAO.java | 50 +++++++++++++ src/main/resources/_ | 0 src/test/java/com/loafle/AppTest.java | 14 ++++ src/test/resources/logback.xml | 17 +++++ 6 files changed, 189 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/loafle/overflow/commons/dao/JPABaseDAO.java create mode 100644 src/main/resources/_ create mode 100644 src/test/java/com/loafle/AppTest.java create mode 100644 src/test/resources/logback.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa78a66 --- /dev/null +++ b/.gitignore @@ -0,0 +1,73 @@ +# Created by .ignore support plugin (hsz.mobi) +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties +### Java template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +.idea/ +*.iml +target/ + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..f114562 --- /dev/null +++ b/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + + + com.loafle + maven_parent_jar + 1.0.0-RELEASE + + + com.loafle + jpa_base_dao + jar + 1.0.0-RELEASE + com.loafle.jpa_base_dao + + + + com.loafle + overflow_base_dao + 1.0.0-RELEASE + + + + + org.hibernate + hibernate-entitymanager + 5.2.10.Final + + + + + \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/commons/dao/JPABaseDAO.java b/src/main/java/com/loafle/overflow/commons/dao/JPABaseDAO.java new file mode 100644 index 0000000..53524a8 --- /dev/null +++ b/src/main/java/com/loafle/overflow/commons/dao/JPABaseDAO.java @@ -0,0 +1,50 @@ +package com.loafle.overflow.commons.dao; + +import javax.persistence.EntityManager; +import javax.persistence.Persistence; +import javax.persistence.PersistenceContext; +import java.lang.reflect.ParameterizedType; + +/** + * Created by insanity on 17. 5. 25. + */ +public class JPABaseDAO implements BaseDAO { + + @PersistenceContext + private EntityManager entityManager; + private final Class entityType; + + public JPABaseDAO() { + this.entityManager = Persistence.createEntityManagerFactory("overflow").createEntityManager(); + this.entityType = ((Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]); + } + + protected EntityManager getEntityManager() { + return this.entityManager; + } + + public T find(T entity) + { + return this.entityManager.find(entityType, entity); + } + + public T create(T entity) { + this.entityManager.getTransaction().begin(); + this.entityManager.persist(entity); + this.entityManager.getTransaction().commit(); + return entity; + } + + public T update(T entity) { + this.entityManager.getTransaction().begin(); + T result = this.entityManager.merge(entity); + this.entityManager.getTransaction().commit(); + return result; + } + + public void delete(T entity) { + this.entityManager.getTransaction().begin(); + this.entityManager.remove(entityManager.contains(entity) ? entity : entityManager.merge(entity)); + this.entityManager.getTransaction().commit(); + } +} diff --git a/src/main/resources/_ b/src/main/resources/_ new file mode 100644 index 0000000..e69de29 diff --git a/src/test/java/com/loafle/AppTest.java b/src/test/java/com/loafle/AppTest.java new file mode 100644 index 0000000..15c2fbc --- /dev/null +++ b/src/test/java/com/loafle/AppTest.java @@ -0,0 +1,14 @@ + +package com.loafle; +import static org.junit.Assert.*; + +import org.junit.Ignore; +import org.junit.Test; + +@Ignore +public class AppTest { + @Test + public void testSum() { + fail("Not yet implemented"); + } +} \ No newline at end of file diff --git a/src/test/resources/logback.xml b/src/test/resources/logback.xml new file mode 100644 index 0000000..084e93a --- /dev/null +++ b/src/test/resources/logback.xml @@ -0,0 +1,17 @@ + + + jpa_base_dao + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n + + + + + + + + + \ No newline at end of file