Skip to content

Instantly share code, notes, and snippets.

@jnwelzel
Last active February 8, 2024 21:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jnwelzel/8435196 to your computer and use it in GitHub Desktop.
Save jnwelzel/8435196 to your computer and use it in GitHub Desktop.
Simple generic DAO structure for Java. Note that the interface takes two generic parameters, one for the primary key type, and the second for the bean type, which uses the same primary key. This enables you to use any kind of object as a primary key, making your code very flexible. This is a good behavior especially if you use JPA embedded ids (…
@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;
}
}
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);
}
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment