Alan Lanzoni_ Generic Dao + Generic Service Com IoC (JPA)

3
14/09/13 Alan Lanzoni: Generic Dao + Generic Service com IoC (JPA) alanlanzoni.blogspot.com.br/2013/05/generic-dao-generic-service-com-ioc.html 1/3 Alan Lanzoni terça-feira, 14 de maio de 2013 Generic Dao + Generic Service com IoC (JPA) Seguindo nossas configurações do último post, implementaremos agora o GenericDao com o GenericService, com injeções de dependência através de annotations: No pacote seupacote.dao, teremos a interface Dao: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com. dao ; import java.io.Serializable ; import java.util.List ; public interface Dao <T, ID extends Serializable> { T getById (ID id); void persist (T entity); void update (T entity); void delete (T entity); void refresh (T entity); List<T> getList (); } E no pacote seupacote.dao.Impl teremos o sua implementação 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 package com. dao . Impl ; import java.io.Serializable ; import java.lang.reflect.ParameterizedType ; import java.util.List ; import javax.persistence.EntityManager ; import javax.persistence.PersistenceContext ; import javax.persistence.criteria.Root ; import org.springframework.stereotype.Repository ; import org.springframework.transaction.annotation.Transactional ; import com.dao.Dao ; @Repository public abstract class DaoImpl <T, ID extends Serializable> implements Dao<T, ID> { private Class<T> persistentClass; @PersistenceContext (name = "darkconnection" ) protected EntityManager entityManager; Root<T> root; @SuppressWarnings ( "rawtypes" ) TypedQuery q; @SuppressWarnings ( "unchecked" ) public DaoImpl () { this . persistentClass = (Class<T>) ((ParameterizedType) getClass() . getGenericSuperclass ()). getActualTypeArguments ()[ 0 ]; ; } public Class<T> getPersistentClass () { return persistentClass; } @Transactional (readOnly = true ) public T getById (ID id) { return entityManager. find (persistentClass, id); } @Transactional public void persist (T entity) { entityManager. persist (entity); } @Transactional public void update (T entity) { entityManager. merge (entity); } @Transactional public void delete (T entity) { Object o = entityManager. merge (entity); 2013 (11) Agosto (1) Maio (10) Pegar usuário logado Spring Security java.lang.OutOfMem ory Autenticação CAS com Spring Security via banco de ... Spring Security + SSO + Tomcat = CAS Configuração do Spring Security Camadas Dao e Service Generic Dao + Generic Service com IoC (JPA) Spring 3.2.1 + JSF 2.1.2 Maven, Spring + Spring Security FIRSTTTTTT!!!! Arquivo do blog Alan Lanzoni Visualizar meu perfil completo Quem sou eu 0 Compartilhar mais Próximo blog» Criar um blog Lo

Transcript of Alan Lanzoni_ Generic Dao + Generic Service Com IoC (JPA)

Page 1: Alan Lanzoni_ Generic Dao + Generic Service Com IoC (JPA)

14/09/13 Alan Lanzoni: Generic Dao + Generic Service com IoC (JPA)

alanlanzoni.blogspot.com.br/2013/05/generic-dao-generic-service-com-ioc.html 1/3

Alan Lanzoniterça-feira, 14 de maio de 2013

Generic Dao + Generic Service com IoC (JPA)

Seguindo nossas configurações do último post, implementaremos agora o GenericDao com o GenericServ ice, com injeções de

dependência através de annotations:

No pacote seupacote.dao, teremos a interface Dao:

1 2 3 4 5 6 7 8 91011121314151617181920

package com.dao;

import java.io.Serializable;import java.util.List;

public interface Dao<T, ID extends Serializable> { T getById(ID id);

void persist(T entity);

void update(T entity);

void delete(T entity);

void refresh(T entity);

List<T> getList();}

E no pacote seupacote.dao.Impl teremos o sua implementação

1 2 3 4 5 6 7 8 91011121314

15161718192021222324252627282930313233343536373839404142434445464748495051

package com.dao.Impl;

import java.io.Serializable;import java.lang.reflect.ParameterizedType;import java.util.List;import javax.persistence.EntityManager;import javax.persistence.PersistenceContext;import javax.persistence.criteria.Root;import org.springframework.stereotype.Repository;import org.springframework.transaction.annotation.Transactional;import com.dao.Dao;

@Repositorypublic abstract class DaoImpl<T, ID extends Serializable> implements Dao<T, ID> {

private Class<T> persistentClass; @PersistenceContext(name = "darkconnection") protected EntityManager entityManager; Root<T> root; @SuppressWarnings("rawtypes") TypedQuery q;

@SuppressWarnings("unchecked") public DaoImpl() { this.persistentClass = (Class<T>) ((ParameterizedType) getClass() .getGenericSuperclass()).getActualTypeArguments()[0]; ; }

public Class<T> getPersistentClass() { return persistentClass; }

@Transactional(readOnly = true) public T getById(ID id) { return entityManager.find(persistentClass, id); }

@Transactional public void persist(T entity) { entityManager.persist(entity); }

@Transactional public void update(T entity) { entityManager.merge(entity); }

@Transactional public void delete(T entity) { Object o = entityManager.merge(entity);

▼ 2013 (11)

► Agosto (1)

▼ Maio (10)

Pegar usuáriologado SpringSecurity

java.lang.OutOfMemory

Autenticação CAScom SpringSecurity viabanco de ...

Spring Security +SSO + Tomcat =CAS

Configuração doSpring Security

Camadas Dao eService

Generic Dao +Generic Servicecom IoC (JPA)

Spring 3.2.1 + JSF2.1.2

Maven, Spring +Spring Security

FIRSTTTTTT!!!!

Arquivo do blog

Alan Lanzoni

Visualizar meu perfil completo

Quem sou eu

0Compartilhar mais Próximo blog» Criar um blog Login

Page 2: Alan Lanzoni_ Generic Dao + Generic Service Com IoC (JPA)

14/09/13 Alan Lanzoni: Generic Dao + Generic Service com IoC (JPA)

alanlanzoni.blogspot.com.br/2013/05/generic-dao-generic-service-com-ioc.html 2/3

5152535455565758596061626364656667

Object o = entityManager.merge(entity); entityManager.remove(o); }

@Transactional public void refresh(T entity) { entityManager.refresh(entity); }

@Transactional(readOnly = true) @SuppressWarnings("unchecked") public List<T> getList() { return entityManager.createQuery( "Select t from " + persistentClass.getSimpleName() + " t") .getResultList(); }}

Agora no pacote seupacote.serv ice teremos a interface Serv ice

1 2 3

4 5 6 7 8 9101112131415161718

package com.service;

import java.io.Serializable;

import java.util.List;

public interface Service<T, ID extends Serializable> { void delete(T entity);

void persist(T entity);

void update(T entity);

void refresh(T entity);

List<T> getList();

T getById(ID id);}

E por fim no pacote seupacote.serv ice.Impl teremos sua implementação

1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031323334353637383940

41424344454647484950

package com.service.Impl;

import java.io.Serializable;import java.util.List;import com.dao.Dao;import com.service.Service;

@org.springframework.stereotype.Servicepublic class ServiceImpl<T extends Object> implements Service<T, Serializable> {

private Dao<T, Serializable> dao;

@Override public List<T> getList() { return dao.getList(); }

@Override public void delete(T bean) { dao.delete(bean); }

@Override public void persist(T bean) { dao.persist(bean); }

@Override public void update(T bean) { dao.update(bean); }

@Override public T getById(Serializable id) { return dao.getById(id); }

@Override public void refresh(T entity) { dao.refresh(entity);

}

public Dao<T, Serializable> getDaoImpl() { return dao; }

public void setDaoImpl(Dao<T, Serializable> dao) { this.dao = dao; }}

No próximo post, criarei um bean de exemplo, com suas camadas e relações.