Commit edc957518703dc473423f1b30bf6bf2015b00ecb

Authored by Benjamin Rühl
1 parent aad7d7b1

Add DAO layer including implementation for user entity

services/Common/src/main/java/de/bht/beuthbot/dataAccess/GenericDAO.java 0 → 100644
  1 +package de.bht.beuthbot.dataAccess;
  2 +
  3 +import de.bht.beuthbot.model.entities.Entity;
  4 +
  5 +import java.io.Serializable;
  6 +import java.util.List;
  7 +
  8 +/**
  9 + * Created by Benjamin Rühl on 19.12.2017.
  10 + */
  11 +public interface GenericDAO<T extends Entity, ID extends Serializable> {
  12 +
  13 + T findById(ID id);
  14 +
  15 + List<T> findAll();
  16 +
  17 + T saveOrUpdate(T entity);
  18 +
  19 + void delete(T entity);
  20 +}
... ...
services/Common/src/main/java/de/bht/beuthbot/dataAccess/UserDAO.java 0 → 100644
  1 +package de.bht.beuthbot.dataAccess;
  2 +
  3 +import de.bht.beuthbot.model.entities.User;
  4 +
  5 +import javax.ejb.Remote;
  6 +
  7 +/**
  8 + * Created by Benjamin Rühl on 19.12.2017.
  9 + */
  10 +@Remote
  11 +public interface UserDAO extends GenericDAO<User, Long> {
  12 +}
... ...
services/Global/src/main/java/de/bht/beuthbot/daos/AppUserDAO.java 0 → 100644
  1 +package de.bht.beuthbot.daos;
  2 +
  3 +import de.bht.beuthbot.dataAccess.UserDAO;
  4 +import de.bht.beuthbot.entities.AppUser;
  5 +import de.bht.beuthbot.model.entities.User;
  6 +
  7 +import javax.ejb.Stateless;
  8 +
  9 +/**
  10 + * Created by Benjamin Rühl on 19.12.2017.
  11 + */
  12 +@Stateless
  13 +public class AppUserDAO extends GenericHibernateDAO<User, Long> implements UserDAO {
  14 +
  15 + public AppUserDAO() {
  16 + setEntityClass(AppUser.class);
  17 + }
  18 +}
... ...
services/Global/src/main/java/de/bht/beuthbot/daos/GenericHibernateDAO.java 0 → 100644
  1 +package de.bht.beuthbot.daos;
  2 +
  3 +import de.bht.beuthbot.dataAccess.GenericDAO;
  4 +import de.bht.beuthbot.model.entities.Entity;
  5 +import org.hibernate.Criteria;
  6 +import org.hibernate.Session;
  7 +import org.hibernate.criterion.Criterion;
  8 +
  9 +import javax.persistence.EntityManager;
  10 +import javax.persistence.PersistenceContext;
  11 +import java.io.Serializable;
  12 +import java.lang.reflect.ParameterizedType;
  13 +import java.util.List;
  14 +
  15 +/**
  16 + * Created by Benjamin Rühl on 19.12.2017.
  17 + */
  18 +public class GenericHibernateDAO<T extends Entity, ID extends Serializable> implements GenericDAO<T, ID> {
  19 +
  20 + private Class<? extends T> entityClass;
  21 + private Session session;
  22 +
  23 + @PersistenceContext
  24 + private EntityManager em;
  25 +
  26 + @SuppressWarnings("unchecked cast")
  27 + public GenericHibernateDAO() {
  28 + entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
  29 + setSession( (Session)em.getDelegate() );
  30 + }
  31 +
  32 + public void setSession(Session s) {
  33 + this.session = s;
  34 + }
  35 +
  36 + protected Session getSession() {
  37 + if (session == null)
  38 + throw new IllegalStateException("Session has not been set on DAO before usage");
  39 + return session;
  40 + }
  41 +
  42 + /**
  43 + * Use this in subclasses to set entityClass to derived type of T
  44 + */
  45 + protected void setEntityClass(Class<? extends T> entityClass) {
  46 + this.entityClass = entityClass;
  47 + }
  48 +
  49 + public Class<? extends T> getEntityClass() {
  50 + return entityClass;
  51 + }
  52 +
  53 + @Override
  54 + public T findById(ID id) {
  55 + return getSession().load(getEntityClass(), id);
  56 + }
  57 +
  58 + public List<T> findAll() {
  59 + return findByCriteria();
  60 + }
  61 +
  62 + @Override
  63 + public T saveOrUpdate(T entity) {
  64 + getSession().saveOrUpdate(entity);
  65 + return entity;
  66 + }
  67 +
  68 + @Override
  69 + public void delete(T entity) {
  70 + getSession().delete(entity);
  71 + }
  72 +
  73 + public void flush() {
  74 + getSession().flush();
  75 + }
  76 +
  77 + public void clear() {
  78 + getSession().clear();
  79 + }
  80 +
  81 + /**
  82 + * Use this inside subclasses as a convenience method.
  83 + */
  84 + @SuppressWarnings("unchecked")
  85 + protected List<T> findByCriteria(Criterion... criterion) {
  86 + Criteria crit = getSession().createCriteria(getEntityClass());
  87 + for (Criterion c : criterion) {
  88 + crit.add(c);
  89 + }
  90 + return crit.list();
  91 + }
  92 +}
... ...
services/Global/src/main/java/de/bht/beuthbot/entities/User.java renamed to services/Global/src/main/java/de/bht/beuthbot/entities/AppUser.java
... ... @@ -5,10 +5,12 @@ import javax.persistence.Table;
5 5  
6 6 /**
7 7 * Created by Benjamin Rühl on 19.11.2017.
  8 + * Note: This class must not be named "User" except a different table name is provided.
  9 + * Postgres does not support creating a table named "user".
8 10 */
9 11 @Entity
10   -@Table(name = "user_entity")
11   -public class User extends EntityBase implements de.bht.beuthbot.model.entities.User {
  12 +@Table
  13 +public class AppUser extends EntityBase implements de.bht.beuthbot.model.entities.User {
12 14  
13 15 private String facebookUserId;
14 16 private String telegramUserId;
... ...
services/Global/src/main/java/de/bht/beuthbot/persistence/UserManager.java deleted 100644 → 0
1   -package de.bht.beuthbot.persistence;
2   -
3   -import de.bht.beuthbot.entities.User;
4   -
5   -import javax.ejb.Stateless;
6   -import javax.persistence.EntityManager;
7   -import javax.persistence.PersistenceContext;
8   -
9   -//@Stateless
10   -public class UserManager {
11   -
12   - /*@PersistenceContext(unitName="PostgreSQLDS")
13   - protected EntityManager entityManager;
14   -
15   - public void createUser() {
16   - User user = new User();
17   - user.setFacebookUserId("FacebookUserId");
18   - user.setTelegramUserId("TelegramUserId");
19   -
20   - entityManager.persist(user);
21   - }*/
22   -}
services/Global/src/main/resources/META-INF/persistence.xml
... ... @@ -4,18 +4,17 @@
4 4 http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
5 5 version="2.1">
6 6  
7   - <persistence-unit name="PostgreSQLDS" transaction-type="RESOURCE_LOCAL">
  7 + <persistence-unit name="PostgreSQLDS" transaction-type="JTA">
8 8  
9 9 <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
10 10  
11 11 <jta-data-source>java:jboss/datasources/PostgreSQLDS</jta-data-source>
12 12  
13   - <class>de.bht.beuthbot.entities.User</class>
14   -
15 13 <properties>
16 14  
17 15 <!--<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>--> <!-- DB Dialect -->
18 16 <property name="hibernate.dialect" value="de.bht.beuthbot.persistence.ExtendedPostgreSQLDialect"/>
  17 + <!--<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>-->
19 18 <property name="hibernate.hbm2ddl.auto" value="create-drop" /> <!-- create / create-drop / update / none -->
20 19 <property name="hibernate.archive.autodetection" value="class"/>
21 20  
... ...
services/Global/src/main/webapp/META-INF/persistence.xml deleted 100644 → 0
1   -<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
2   - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3   - xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
4   - http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
5   - version="2.1">
6   -
7   - <persistence-unit name="de.bht.beuthbot.jpa" transaction-type="RESOURCE_LOCAL">
8   -
9   - <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
10   -
11   - <properties>
12   - <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" /> <!-- DB Driver -->
13   - <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/beuthbot" /> <!-- BD Name -->
14   - <property name="javax.persistence.jdbc.user" value="beuthbot_app" /> <!-- DB User -->
15   - <property name="javax.persistence.jdbc.password" value="VhS7WPVpdYEHYLpf" /> <!-- DB Password -->
16   -
17   - <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect"/> <!-- DB Dialect -->
18   - <property name="hibernate.hbm2ddl.auto" value="create-drop" /> <!-- create / create-drop / update / none -->
19   - <property name="hibernate.archive.autodetection" value="class"/>
20   -
21   - <property name="hibernate.show_sql" value="true" /> <!-- Show SQL in console -->
22   - <property name="hibernate.format_sql" value="true" /> <!-- Show SQL formatted -->
23   - </properties>
24   -
25   - </persistence-unit>
26   -
27   -</persistence>
28 0 \ No newline at end of file