May 18 2014
Java EE DAO Pattern
Java EE really makes our life easy when dealing with Database Management. Here is a basic DAO design pattern that I use in most of my projects:
public abstract class AbstractDao, ID extends Serializable> {
protected abstract EntityManager getEntityManager();
private Class entity;
public AbstractDao(Class entity) {
this.entity = entity;
}
public T find(ID id) {
return getEntityManager().find(entity, id);
}
public List findAll() {
CriteriaQuery cq = getEntityManager().getCriteriaBuilder()
.createQuery(entity);
cq.select(cq.from(entity));
return getEntityManager().createQuery(cq).getResultList();
}
public List findByPage(int pageNum, int pageSize) {
CriteriaQuery cq = getEntityManager().getCriteriaBuilder()
.createQuery(entity);
cq.select(cq.from(entity));
int firstResult = pageNum * pageSize;
return getEntityManager().createQuery(cq).setFirstResult(firstResult)
.setMaxResults(pageSize).getResultList();
}
public long count() {
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Long.class);
Root personRoot = cq.from(entity);
cq.select(cb.count(personRoot));
List list = getEntityManager().createQuery(cq).setMaxResults(1)
.getResultList();
long personsNumber = 0l;
if (!list.isEmpty()) {
personsNumber = list.get(0);
}
return personsNumber;
}
public T save(T entity) {
try {
if (entity.getId() == null) {
getEntityManager().persist(entity);
} else {
entity = getEntityManager().merge(entity);
}
} catch (Exception e) {
getLogger().error(e.getMessage());
}
return entity;
}
public T saveAndFlush(T entity) {
try {
entity = save(entity);
flush();
} catch (Exception e) {
getLogger().error(e.getMessage());
}
return entity;
}
public void flush() {
getEntityManager().flush();
}
public List saveAllAndFlush(Collection collection) {
List list = null;
try {
list = collection.parallelStream()
.map(this::save)
.collect(Collectors.toList());
flush();
} catch (Exception e) {
getLogger().error(e.getMessage());
}
return list;
}
public boolean exists(ID id) {
return find(id) != null;
}
protected abstract Logger getLogger();
}
Aug 12 2016
Dealing with “Could not find artifact com.oracle:ojdbc6:jar”
Introduction
Chris Georgoulis and I describe solutions to working around the following maven error:
Below I describe 3 ways how to handle this but there are also other solutions available. Check reference at the bottom.
If you plan on using your Project as a dependency:
The simplest solution is to manually install the JAR into the local maven repository. Using the example from stackoverflow:
With the maven entry:
Run the following command in the command line:
If you plan on using your Project as a dependency & want the process automated:
Instead of installing the JAR to the repository manually, there’s actually a plugin for that – thanks Chris for the info.
Add the following entry:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-install-plugin</artifactId> <inherited>false</inherited> <executions> <execution> <id>install-external</id> <phase>clean</phase> <configuration> <file>${basedir}/lib/ojdbc14.jar</file> <repositoryLayout>default</repositoryLayout> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>10.2.0.3.0</version> <packaging>jar</packaging> <generatePom>true</generatePom> </configuration> <goals> <goal>install-file</goal> </goals> </execution> </executions> </plugin> </plugins> </build>If your don’t plan on using your Project as a dependency in another project:
By Menelaos Bakopoulos • Java EE 0 • Tags: maven