com.ymonnier.sql.help.service.CrudServiceBean Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ym-sql-helper Show documentation
Show all versions of ym-sql-helper Show documentation
A little abstraction of CRUD operations and
generator find function for model's attributs.
The newest version!
package com.ymonnier.sql.help.service;
import com.ymonnier.sql.help.service.fatory.EntityManagerFactory;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Project SqlHelper.
* Package com.ymonnier.sql.help.service.
* File CrudServiceBean.java.
* Created by Ysee on 15/03/2017 - 19:39.
* www.yseemonnier.com
* https://github.com/YMonnier
*/
public class CrudServiceBean implements CrudService {
public EntityManager entityManager;
public CrudServiceBean() {
this.entityManager = EntityManagerFactory.get();
}
public CrudServiceBean(EntityManager entityManager) {
this.entityManager = entityManager;
}
/**
* Save an entity into the database.
*
* @param object Entity object.
* @return The entity updated(@ID).
*/
@Override
public T save(T object) {
entityManager.persist(object);
entityManager.flush();
entityManager.refresh(object);
return object;
}
/**
* Update an entity into the database.
*
* @param object Entity object.
* @return The entity updated.
*/
@Override
public T update(T object) {
return entityManager.merge(object);
}
/**
* Delete an entity into the database.
*
* @param object Entity object.
*/
@Override
public void delete(T object) {
entityManager.remove(object);
}
/**
* Create a QueryBuilder for a named query.
*
* @param namedQueryName The string query name.
* @return QueryBuilder object. @see QueryBuilder
*/
public QueryBuilder findWithNamedQuery(String namedQueryName) {
Query query = entityManager.createNamedQuery(namedQueryName);
return new QueryBuilder<>(query);
}
/**
* Create a QueryBuilder for a specific query.
*
* @param query The string query (native SQL or JPQL).
* @return QueryBuilder object. @see QueryBuilder
*/
public QueryBuilder findWithQuery(String query) {
return new QueryBuilder<>(entityManager.createQuery(query));
}
/**
* Close the EntityManager connection.
*/
@Override
public void close() {
EntityManagerFactory.release(entityManager);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy