All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.onetwo.common.db.spi.BaseEntityManager Maven / Gradle / Ivy

The newest version!
package org.onetwo.common.db.spi;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.onetwo.common.db.DbmQueryValue;
import org.onetwo.common.db.EntityManagerProvider;
import org.onetwo.common.db.builder.QueryBuilder;
import org.onetwo.common.db.sqlext.SQLSymbolManager;
import org.onetwo.common.utils.Page;
import org.onetwo.dbm.core.spi.DbmSessionFactory;
import org.onetwo.dbm.dialet.DBDialect.LockInfo;
import org.onetwo.dbm.utils.DbmLock;

/****
 * 通用的实体查询接口
 * ByProperties后缀的方法名一般以Map为参数,其作用和没有ByProperties后缀的一样
 * @author way
 *
 */
public interface BaseEntityManager {

	public  T load(Class entityClass, Serializable id);
	
	public  T findById(Class entityClass, Serializable id);
	
	/***
	 * @see org.onetwo.dbm.dialet.DBDialect$LockInfo
	 * @author wayshall
	 * @param entityClass
	 * @param id
	 * @param lock
	 * @param timeoutInMillis lock forevaer if null, support: oracle, not support: mysql
	 * @return return the target or null if not found
	 */
	public  T lock(Class entityClass, Serializable id, DbmLock lock, Integer timeoutInMillis);
	
	default  T lockWrite(Class entityClass, Serializable id) {
		return lock(entityClass, id, DbmLock.PESSIMISTIC_WRITE, LockInfo.WAIT_FOREVER);
	}
	
	default  T lockRead(Class entityClass, Serializable id) {
		return lock(entityClass, id, DbmLock.PESSIMISTIC_READ, LockInfo.WAIT_FOREVER);
	}

	public  T save(T entity);
	public  Collection saves(Collection entities);
	
	public  void persist(T entity);
	
	/****
	 * 根据id把实体的所有字段更新到数据库
	 * @param entity
	 */
	public void update(Object entity);
	/***
	 * 根据id把实体的非null字段更新到数据库
	 * @author wayshall
	 * @param entity
	 */
	public void dymanicUpdate(Object entity);

	/****
	 * 执行此方法时,若实体实现了逻辑删除接口ILogicDeleteEntity,则只是更新状态
	 * @param entity
	 */
	public int remove(Object entity);
	
	/***
	 * 执行此方法时,若实体实现了逻辑删除接口ILogicDeleteEntity,则只是更新状态
	 * 
	 * 返回updateCount
	 * 注意:某些数据库或版本的jdbc driver,批量删除时,updateCount并不正确
	 * @author wayshall
	 * @param entities
	 * @return
	 */
	public  int removes(Collection entities);
	/***
	 * 实际上是removeById的批量操作,如果removeById返回了null,则不会被放到返回的list里
	 * 可以根据实际返回的list数量和传入的集合数量是否相等来判断是否全部删除
	 * @author wayshall
	 * @param entityClass
	 * @param id
	 * @return
	 */
	public  Collection removeByIds(Class entityClass, Serializable[] id);
	/***
	 * 执行此方法时,若实体实现了逻辑删除接口ILogicDeleteEntity,则只是更新状态
	 * 若找不到数据,则抛错
	 * @author wayshall
	 * @param entityClass
	 * @param id
	 * @return
	 */
	public  T removeById(Class entityClass, Serializable id);
	/***
	 * 物理删除,不判断实体是否实现 ILogicDeleteEntity 接口
	 * 返回updateCount
	 * @author wayshall
	 * @param entityClass
	 * @return
	 */
	public int removeAll(Class entityClass);
	
	/***
	 * 逻辑删除
	 * @author wayshall
	 * @param entity
	 */
//	public void delete(ILogicDeleteEntity entity);

	/***
	 * 逻辑删除
	 * @author wayshall
	 * @param entityClass
	 * @param id
	 * @return
	 */
//	public  T deleteById(Class entityClass, Serializable id);

	public  List findAll(Class entityClass);

	public Number countRecordByProperties(Class entityClass, Map properties);

	public Number countRecord(Class entityClass, Object... params);

	/***
	 *  查找唯一记录,如果找不到返回null,如果多于一条记录,抛出异常。
	 * @param entityClass
	 * @param properties
	 * @return
	 */
	public  T findUniqueByProperties(Class entityClass, Map properties);
	public  T findUnique(Class entityClass, Object... properties);
	
	public  T findOne(Class entityClass, Object... properties);
	public  T findOneByProperties(Class entityClass, Map properties);


	/*****
	 * 根据属性查询列表
	 * @param entityClass
	 * @param properties
	 * @return
	 */
	public  List findList(Class entityClass, Object... properties);
	public  List findListByProperties(Class entityClass, Map properties);
	
	/***
	 * @deprecated 不建议使用此方法,直接用Querys dsl api
	 * @author weishao zeng
	 * @param squery
	 * @return
	 */
	public  List findList(QueryBuilder squery);

	public  List selectFields(Class entityClass, Object[] selectFields, Object... properties);
	public  List selectFieldsToEntity(Class entityClass, Object[] selectFields, Object... properties);
	

	public  Page findPage(final Class entityClass, final Page page, Object... properties);

	public  Page findPageByProperties(final Class entityClass, final Page page, Map properties);
	
	/***
	 * 此方法是提供给一些把QueryBuilder作为固化为实例的查询参数所用
	 * 
	 * @deprecated 不建议使用此方法,直接用Querys dsl api
	 * @author weishao zeng
	 * @param page
	 * @param query
	 * @return
	 */
	@Deprecated
	public  Page findPage(final Page page, QueryBuilder query);
	
	public  Page findPage(Page page, DbmQueryValue squery);
	
	public void flush();
	
	public void clear();
	
	/***
	 * 依赖于实现
	 * @param entity
	 * @return
	 */
	public  T merge(T entity);
	
//	public EntityManager getEntityManager();
	
//	public DataQuery createMappingSQLQuery(String sqlString, String resultSetMapping);
	
	
	public QueryWrapper createNamedQuery(String name);
	public QueryWrapper createQuery(String sql, Map values);
	
	public Long getSequences(String sequenceName, boolean createIfNotExist);
	public Long getSequences(Class entityClass, boolean createIfNotExist);
//	public SequenceNameManager getSequenceNameManager();
	
	public EntityManagerProvider getEntityManagerProvider();
	
	public SQLSymbolManager getSQLSymbolManager();
	
	public  T narrowAs(Class entityManagerClass);
	
	public DbmSessionFactory getSessionFactory();
	
	 QueryBuilder from(Class entityClass);

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy