jpa_base_dao impl

This commit is contained in:
insanity 2017-05-25 16:30:53 +09:00
commit f641fe9baf
6 changed files with 189 additions and 0 deletions

73
.gitignore vendored Normal file
View File

@ -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/

35
pom.xml Normal file
View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>com.loafle</groupId>
<artifactId>maven_parent_jar</artifactId>
<version>1.0.0-RELEASE</version>
</parent>
<groupId>com.loafle</groupId>
<artifactId>jpa_base_dao</artifactId>
<packaging>jar</packaging>
<version>1.0.0-RELEASE</version>
<name>com.loafle.jpa_base_dao</name>
<dependencies>
<dependency>
<groupId>com.loafle</groupId>
<artifactId>overflow_base_dao</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.10.Final</version>
</dependency>
</dependencies>
</project>

View File

@ -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<T> implements BaseDAO<T> {
@PersistenceContext
private EntityManager entityManager;
private final Class<T> entityType;
public JPABaseDAO() {
this.entityManager = Persistence.createEntityManagerFactory("overflow").createEntityManager();
this.entityType = ((Class<T>) ((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();
}
}

0
src/main/resources/_ Normal file
View File

View File

@ -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");
}
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="3 seconds">
<contextName>jpa_base_dao</contextName>
<!-- TRACE > DEBUG > INFO > WARN > ERROR -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n
</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
<logger name="com.loafle" level="ALL" />
</configuration>