Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IDao<K, E> {
- /**
- * Performs SQL insert for the specified entity
- * @param entity domain object to store in database
- */
- void persist(E entity);
- /**
- * Performs SQL DELETE based on entity PK
- * @param id domain object to remove from database
- */
- void remove(K id);
- /**
- * Performs SQL SELECT with WHERE clause based in PK
- * @param id primary key attribute of entity
- * @return entity of type E with id of value of K
- */
- E findById(K id);
- /**
- * Performs SQL UPDATE for the specified entity
- * @param entity domain object to store in database
- */
- void update(E entity);
- }
- public class LicenseDao implements IDao<Integer, License> {
- private EntityManagerFactory entityManagerFactory;
- private EntityManager entityManager;
- private Logger logger = LoggerFactory.getLogger(LicenseDao.class);
- /**
- * JPA EntityManagerFactory instance injected, All other new parameters can should be injected similarly
- * to ensure compatibility with route controllers if extending this class.
- *
- * @param entityManagerFactory Injected by Jooby Framework IoC DI module
- */
- @Inject
- public LicenseDao(@Named("testPU") EntityManagerFactory entityManagerFactory) {
- this.entityManagerFactory = entityManagerFactory;
- this.entityManager = entityManagerFactory.createEntityManager();
- }
- /**
- * Performs SQL insert for the specified entity
- * @param entity domain object to store in database
- */
- @Override
- public void persist(License entity) {
- entityManager.getTransaction().begin();
- entityManager.persist(entity);
- entityManager.getTransaction().commit();
- entityManager.close();
- }
- /**
- * Performs SQL DELETE based on entity PK
- * @param id domain object to remove from database
- */
- @Override
- public void remove(Integer id) {
- entityManager.getTransaction().begin();
- License license = entityManager.find(License.class, id);
- if (license == null) {
- //Add notfication of failure
- logger.warn("Trying to remove non-exiting licence: " + id);
- } else {
- logger.info("Removing " + license);
- entityManager.remove(license);
- }
- entityManager.getTransaction().commit();
- entityManager.close();
- }
- /**
- * Performs SQL SELECT with WHERE clause based in PK
- * @param id primary key attribute of entity
- * @return entity of type E with id of value of K
- */
- @Override
- public License findById(Integer id) {
- return entityManager.find(License.class, id);
- }
- /**
- * Find all Licenses related to an organisation. Note that this will return all Licenses
- * and may be slow if large amount of Licenses exist.
- * @param organisationId organisation primary key used to relate conductor to organisation
- * @return List of Licenses
- */
- public List<License> findAllByOrganisationId(Integer organisationId) {
- EntityManager entityManager = entityManagerFactory.createEntityManager();
- return entityManager.
- createQuery(
- "SELECT l from License l " +
- "WHERE l.organisation.organisationId = :organisationId", License.class)
- .setParameter("organisationId", organisationId)
- .getResultList();
- }
- /**
- * Updates the license
- * @param license domain object to store in database
- */
- @Override
- public void update(License license) {
- entityManager.getTransaction().begin();
- entityManager.merge(license);
- entityManager.getTransaction().commit();
- entityManager.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement