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

ars.database.hibernate.HibernateSimpleRepository Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package ars.database.hibernate;

import java.io.Serializable;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import ars.database.repository.Query;
import ars.database.repository.AbstractRepository;
import ars.database.hibernate.DetachedCriteriaQuery;

/**
 * 基于Hibernate数据持久化操作简单实现
 * 
 * @author yongqiangwu
 * 
 * @param 
 *            数据模型
 */
public class HibernateSimpleRepository extends AbstractRepository {
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/**
	 * 获取Session
	 * 
	 * @return Session对象
	 */
	protected Session getSession() {
		if (this.sessionFactory == null) {
			throw new RuntimeException("Session factory has not been initialize");
		}
		Session session = this.sessionFactory.getCurrentSession();
		session.clear();
		return session;
	}

	@Override
	protected void modify(T object) {
		if (object != null) {
			this.getSession().update(object);
		}
	}

	@Override
	protected Serializable insert(T object) {
		return object == null ? null : this.getSession().save(object);
	}

	@Override
	protected void remove(T object) {
		if (object != null) {
			this.getSession().delete(object);
		}
	}

	@Override
	public Query query() {
		return new DetachedCriteriaQuery(this.sessionFactory, this.getModel());
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy