Let’s handle the database via the sourcecode
Before we could connect to the database, we have edit persist.xml file to configure EclipseLink to connect to the database. When you use the default configuration for EclipseLink, then you should have to edit a lot in that file.
It looks a bit like this:
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" 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"> <persistence-unit name="com.rivaso_exp" transaction-type="JTA"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <jta-data-source>/jdbc/expdb</jta-data-source> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <property name="eclipselink.ddl-generation.output-mode" value="database"/> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/> </properties> </persistence-unit> </persistence>
And then we should make a connector to it: This is easy done with an entity manager.
To achieve that, we have to make an abstract class which covers many the basic actions you want to do on a database like counting, getting the highest used id (which is definitely not the same as the amount of entities in your table!), create, edit and remove.
Make a class like this:
package com.rivaso.exp.facade; import java.util.List; import javax.persistence.EntityManager; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; /** * @author RiVaSo * @version 1.0 */ public abstract class AbstractManager { private Class entityClass; public AbstractManager(Class entityClass) { this.entityClass = entityClass; } protected abstract EntityManager getEntityManager(); public void create(T entity) { getEntityManager().persist(entity); } public void edit(T entity) { getEntityManager().merge(entity); } public void remove(T entity) { getEntityManager().remove(getEntityManager().merge(entity)); } public T find(Object id) { return getEntityManager().find(entityClass, id); } @SuppressWarnings ({"rawtypes", "unchecked"}) public List findAll() { javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder(). createQuery(); cq.select(cq.from(entityClass)); return getEntityManager().createQuery(cq).getResultList(); } @SuppressWarnings ({"rawtypes", "unchecked"}) public List findRange(int[] range) { javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder(). createQuery(); cq.select(cq.from(entityClass)); javax.persistence.Query q = getEntityManager().createQuery(cq); q.setMaxResults(range[1] - range[0]); q.setFirstResult(range[0]); return q.getResultList(); } @SuppressWarnings ({"rawtypes", "unchecked"}) public int count() { javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder(). createQuery(); javax.persistence.criteria.Root rt = cq.from(entityClass); cq.select(getEntityManager().getCriteriaBuilder().count(rt)); javax.persistence.Query q = getEntityManager().createQuery(cq); return ((Long) q.getSingleResult()).intValue(); } @SuppressWarnings ({"rawtypes", "unchecked"}) public int getCurrentMaxOfId() { javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder(). createQuery(); javax.persistence.criteria.Root rt = cq.from(entityClass); cq.select(getEntityManager().getCriteriaBuilder().max(rt.get("id"))); javax.persistence.Query q = getEntityManager().createQuery(cq); Object result = q.getSingleResult(); if (result == null) { return 0; } return ((Integer) result).intValue(); } }
Then extend that abstract class with your own class which you want to use, like for example PersonManager:
package com.rivaso.exp.manager; import com.rivaso.exp.domain.Person; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.NoResultException; import javax.persistence.PersistenceContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author RiVaSo * @version 1.0 */ @Stateless(name = "PersonManager") public class PersonManager extends AbstractManager<Person> { private static Logger log = LoggerFactory.getLogger(PersonManager.class); @PersistenceContext(unitName = "com.rivaso_exp") private EntityManager em; @Override protected EntityManager getEntityManager() { return em; } public PersonManager() { super(Person.class); } public Person getPersonByEmail(String personEmail) { try { return (Person) em .createQuery("SELECT OBJECT(Person) FROM Person person WHERE person.emailAddress = :personEmail") .setParameter("personEmail", personEmail).getSingleResult(); } catch (NoResultException e) { log.warn("Person could not be found"); } catch (Exception e) { log.warn("Could not get PersonByMail", e); } return null; } }