0% found this document useful (0 votes)
26 views

Estrutura DAO Genérica Simples para Java

The document discusses using generics to create flexible data access objects (DAOs) for database entities. It defines a base Bean class with generic type parameters for the primary key and entity class. This allows any type of object to be used as a primary key. A generic DAO interface is also defined that works with any key and bean type. Finally, a GenericDAO implementation is shown that implements the generic DAO interface and uses the EntityManager to perform database operations like save, find, and remove on the generic entity types.

Uploaded by

AmarildodaSilva
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Estrutura DAO Genérica Simples para Java

The document discusses using generics to create flexible data access objects (DAOs) for database entities. It defines a base Bean class with generic type parameters for the primary key and entity class. This allows any type of object to be used as a primary key. A generic DAO interface is also defined that works with any key and bean type. Finally, a GenericDAO implementation is shown that implements the generic DAO interface and uses the EntityManager to perform database operations like save, find, and remove on the generic entity types.

Uploaded by

AmarildodaSilva
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Observe que a interface usa dois parâmetros genéricos, um para o tipo de chave primária e o segundo para o

tipo de bean, que usa a mesma chave primária. Isso permite que você use qualquer tipo de objeto como

chave primária, tornando seu código muito flexível. Este é um bom comportamento, especialmente se você

usar IDs incorporados JPA (…

Bean.java

@MappedSuperclass
public abstract class Bean<PK extends Serializable> implements Serializable {
private static final long serialVersionUID = 1L;
@Version
@Column(name = "VERSION", nullable = false)
private Long version;
public Bean(PK id, Long version) {
this.version = version;
}
public Bean() {
}
public abstract PK getId();
public abstract void setId(PK id);
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
@Override
public String toString() {
return "Bean [id=" + getId() + ", version=" + version + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((version == null) ? 0 :
version.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Bean<PK> other = (Bean<PK>) obj;
if (getId() == null) {
if (other.getId() != null)
return false;
} else if (!getId().equals(other.getId()))
return false;
if (version == null) {
if (other.version != null)
return false;
} else if (!version.equals(other.version))
return false;
return true;
}
}
Raw

DAO.java

public interface DAO<PK extends Serializable, T extends Bean<PK>>


{
/**
* Persist the given entity into through EntityManager.
*
* @param t
* entity to be saved.
*/
public T save(T t);
/**
* Find all items of this type in the database.
*
* @return a List of T elements from database.
*/
public List<T> findAll();
/**
* Find an item from database based on its ID.
*
* @param id
* to look for.
* @return found entity or null if no entity is found.
*/
public T find(PK id);
/**
* Delete the item from database.
*
* @param t
* item to delete.
*/
public void remove(T t);
}
Raw

GenericDAO.java

public class GenericDAO<PK extends Serializable, T extends Bean<PK>> implements DAO<PK, T>
{
protected EntityManager em;
private final Class<T> clazz;
public GenericDAO(Class<T> clazz, EntityManager em) {
super();
this.clazz = clazz;
this.em = em;
}
@Override
public T save(T t) {
if (t.getId() != null) {
t = update(t);
} else {
persist(t);
}
return t;
}
@Override
public List<T> findAll() {
TypedQuery<T> q = em.createQuery(
"select e from " + clazz.getSimpleName() + " e", clazz);
return q.getResultList();
}
@Override
public T find(PK id) {
return em.getReference(clazz, id);
}
@Override
public void remove(T t) {
em.remove(t);
}
/**
* Persist new itens in database.
*
* @param t
* @return
*/
protected T persist(T t) {
em.persist(t);
return t;
}
/**
* Update itens on database.
*
* @param t
* @return
*/
protected T update(T t) {
return em.merge(t);
}
}

You might also like