package lt.baraksoft.summersystem.dao;

import lt.baraksoft.summersystem.model.IEntity;

import javax.persistence.EntityManager;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;

/**
 * Created by Zygimantas on 2016.03.18.
 */
public abstract class GenericDao<T extends IEntity<K>, K extends Serializable> implements IGenericDao<T, K> {

        //reikia kažkaip suwirinti, gal @Inject?
        private EntityManager entityManager;

        protected Class<T> entityClass;

        public GenericDao() {
                ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
                this.entityClass = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
        }

        @Override public T save(T entity) {
                if (entity.getVersion() == null){
                        entityManager.persist(entity);
                        return entity;
                } else {
                        return entityManager.merge(entity);
                }
        }

        @Override public void remove(T entity) {
                entityManager.remove(entityManager.merge(entity));
        }

        //reikia užžymėti kaip transactional read only true, bet ne kaip springe :D
        @Override public T findById(K id) {
                return entityManager.find(entityClass, id);
        }
}

@@ -44,47 +44,72 @@
 ort 
-lt.baraksoft.summersystem.model.IEntity
+java.io.Serializable;%0Aimport java.lang.reflect.ParameterizedType
 ;%0A%0Ai
@@ -151,79 +151,55 @@
 er;%0A
+%0A
 import 
-java.io.Serializable;%0Aimport java.lang.reflect.ParameterizedType
+lt.baraksoft.summersystem.model.IEntity
 ;%0A%0A/
@@ -356,26 +356,20 @@
 , K%3E %7B%0A%0A
-        
+%09
 //
+ 
 reikia k
@@ -369,17 +369,16 @@
 eikia ka
-%C5%BE
 kaip suw
@@ -398,24 +398,17 @@
 Inject?%0A
-        
+%09
 private 
@@ -437,24 +437,17 @@
 nager;%0A%0A
-        
+%09
 protecte
@@ -471,24 +471,17 @@
 Class;%0A%0A
-        
+%09
 public G
@@ -494,32 +494,18 @@
 Dao() %7B%0A
-                
+%09%09
 Paramete
@@ -589,32 +589,18 @@
 lass();%0A
-                
+%09%09
 this.ent
@@ -668,35 +668,21 @@
 s()%5B0%5D;%0A
-        %7D%0A%0A        
+%09%7D%0A%0A%09
 @Overrid
@@ -674,33 +674,34 @@
 ;%0A%09%7D%0A%0A%09@Override
- 
+%0A%09
 public T save(T 
@@ -710,32 +710,18 @@
 tity) %7B%0A
-                
+%09%09
 if (enti
@@ -748,34 +748,14 @@
 ull)
+ 
 %7B%0A
-                        
+%09%09%09
 enti
@@ -785,169 +785,85 @@
 y);%0A
-                        return entity;%0A                %7D else %7B%0A                        return entityManager.merge(entity);%0A                %7D%0A        %7D%0A%0A        
+%09%09%09return entity;%0A%09%09%7D else %7B%0A%09%09%09return entityManager.merge(entity);%0A%09%09%7D%0A%09%7D%0A%0A%09
 @Ove
@@ -863,25 +863,26 @@
 %0A%0A%09@Override
- 
+%0A%09
 public void 
@@ -900,32 +900,18 @@
 tity) %7B%0A
-                
+%09%09
 entityMa
@@ -957,29 +957,16 @@
 ));%0A
-        %7D%0A%0A        
+%09%7D%0A%0A%09
 //
+ 
 reik
@@ -1035,20 +1035,88 @@
 inge
- :D%0A        
+%0A%09// :D%0A%09// man atrodo taip pat settinasi kaip ir springe su anotacija ir tiek%0A%09
 @Ove
@@ -1120,17 +1120,18 @@
 Override
- 
+%0A%09
 public T
@@ -1152,24 +1152,10 @@
 ) %7B%0A
-                
+%09%09
 retu
@@ -1198,16 +1198,9 @@
 d);%0A
-        
+%09
 %7D%0A%7D%0A

test git connection