-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
171 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package models.repository; | ||
|
||
import java.util.List; | ||
|
||
import javax.persistence.Query; | ||
|
||
import play.db.jpa.JPA; | ||
|
||
/** | ||
* Camada genérica para acesso ao Banco de Dados | ||
*/ | ||
public class GenericDAO { | ||
|
||
/** | ||
* Salva um objeto no BD. | ||
*/ | ||
public boolean persist(Object e) { | ||
JPA.em().persist(e); | ||
return true; | ||
} | ||
|
||
/** | ||
* Força que as operações executadas até agora no código sejam | ||
* executadas no BD. Normalmente, o final de cada bloco @Transactional | ||
* fará o flush. Porém, se você quer controlar erros, melhor fazer antes. | ||
*/ | ||
public void flush() { | ||
JPA.em().flush(); | ||
} | ||
|
||
/** | ||
* Atualiza a informação da entidade do código com a do banco de dados. | ||
* | ||
* Mais sobre merge: http://www.arquiteturacomputacional.eti.br/2013/02/entenda-o-ciclo-de-vida-das-entidades.html | ||
*/ | ||
public void merge(Object e) { | ||
JPA.em().merge(e); | ||
} | ||
|
||
/** | ||
* Procura uma certa {@code clazz} pelo seu {@code id}. | ||
*/ | ||
public <T> T findByEntityId(Class<T> clazz, Long id) { | ||
return JPA.em().find(clazz, id); | ||
} | ||
|
||
/** | ||
* Procura todos os objetos de uma certa classe pelo seu {@code className} | ||
* descrito na Entidade. | ||
*/ | ||
public <T> List<T> findAllByClass(Class clazz) { | ||
String hql = "FROM " + clazz.getName(); | ||
Query hqlQuery = JPA.em().createQuery(hql); | ||
return hqlQuery.getResultList(); | ||
} | ||
|
||
/** | ||
* Deleta do banco de dados uma {@code classe} referenciada pelo seu | ||
* {@code id}. | ||
*/ | ||
public <T> void removeById(Class<T> classe, Long id) { | ||
JPA.em().remove(findByEntityId(classe, id)); | ||
} | ||
|
||
/** | ||
* Remove o respectivo {@code objeto} do banco de dados. | ||
*/ | ||
public void remove(Object objeto) { | ||
JPA.em().remove(objeto); | ||
} | ||
|
||
/** | ||
* Procura uma certa {@code className} pelo seu {@code attributeName}. | ||
*/ | ||
public <T> List<T> findByAttributeName(String className, | ||
String attributeName, String attributeValue) { | ||
String hql = "FROM " + className + " c" + " WHERE c." + attributeName | ||
+ " = '" + attributeValue + "'"; | ||
Query hqlQuery = JPA.em().createQuery(hql); | ||
return hqlQuery.getResultList(); | ||
} | ||
|
||
private Query createQuery(String query) { | ||
return JPA.em().createQuery(query); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<persistence xmlns="http://java.sun.com/xml/ns/persistence" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" | ||
version="2.0"> | ||
|
||
<persistence-unit name="h2PersistenceUnit" | ||
transaction-type="RESOURCE_LOCAL"> | ||
<provider>org.hibernate.ejb.HibernatePersistence</provider> | ||
<non-jta-data-source>DefaultDS</non-jta-data-source> | ||
<properties> | ||
<property name="hibernate.connection.url" value="jdbc:h2:mem:play" /> | ||
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" /> | ||
<property name="hibernate.connection.driver_class" value="org.h2.Driver" /> | ||
<property name="hibernate.hbm2ddl.auto" value="update" /> | ||
<property name="hibernate.format_sql" value="true" /> | ||
</properties> | ||
</persistence-unit> | ||
<persistence-unit name="postgrePersistenceUnit" | ||
transaction-type="RESOURCE_LOCAL"> | ||
<provider>org.hibernate.ejb.HibernatePersistence</provider> | ||
<non-jta-data-source>DefaultDS</non-jta-data-source> | ||
<properties> | ||
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" /> | ||
<property name="hibernate.hbm2ddl.auto" value="update" /> | ||
<property name="hibernate.format_sql" value="true" /> | ||
</properties> | ||
</persistence-unit> | ||
</persistence> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters