Comass的增量索引----数据同步

    Compass的数据同步(Data Mirror),实话说,我一直都没有调试成功过。有兴趣的朋友可以参考SpringSide的文档。http://wiki.springside.org.cn/display/springside/Compass。当时曾构思过,不如自已写一个SPRING的AOP来同步?后来做好后居然发现Compass居然也提供了AOP的关于同步的组件。郁闷。下文中,我主要介绍如何使用Compass的AOP和DAO层协作实现我的Data Mirror功能。本文仍延用上一篇文章中的环境,案例。请查阅博客地址:

http://wangwanbao.iteye.com/admin/blogs/368252

 

一、实现Hibernate的DAO

代码不多,直接给出好了。

public class GenericDaoImpl extends HibernateDaoSupport implements GenericDao {
	
	Session session;
	
	public Session getHibernateSession(){
		if(null == session)
			session = getSession();
		return session;
	}
	@SuppressWarnings("unchecked")
	public List findByHQL(final String hql) {
		return (List) getHibernateTemplate().executeWithNativeSession(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(hql);
						return query.list();
					}

				});
	}

	public <T> List<T> getAll(Class<T> entityClass) {
		return this.getHibernateTemplate().loadAll(entityClass);
	}

	public void deletebyID(Class entityClass, Serializable id) {
		this.delete(this.getbyID(entityClass, id));

	}

	public void deleteByList(Collection con) {
		this.getHibernateTemplate().deleteAll(con);
	}

	public void save(Object o) {
		getHibernateTemplate().saveOrUpdate(o);
	}

	public void delete(Object o) {
		this.getHibernateTemplate().delete(o);
	}

	public <T>T getbyID(Class<T> entityClass, Serializable id) {
		Object o = this.getHibernateTemplate().get(entityClass, id);
		if (o == null) {
			throw new ObjectRetrievalFailureException(entityClass, id);
		}
		return (T) o;
	}

	public <T> List<T> findBy(Class<T> entityClass, String name, Object value) {
		Assert.hasText(name);
		Criteria criteria = getEntityCriteria(entityClass);
		criteria.add(Restrictions.eq(name, value));
		return criteria.list();
	}
	
	public <T> List<T> findByParameters(Class<T> entityClass, String[] name, Object[] value) {
		Criteria criteria = getEntityCriteria(entityClass);
		for(int i=0;i<name.length;i++){
			criteria.add(Restrictions.eq(name[i], value[i]));
		}
		return criteria.list();
	}

	protected Criteria getEntityCriteria(Class entityClass) {
		return getHibernateSession().createCriteria(entityClass);
	}
}

 

  二、Spring的配置文件:

 

<bean id="compassSaveAdvisor"
		class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
		<property name="advice">
			<bean class="org.compass.spring.aop.CompassSaveAdvice">
				<property name="compass" ref="compass" />
			</bean>
		</property>
		<property name="pattern" value=".*save" />
	</bean>

	<bean id="compassDeleteAdvisor"
		class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
		<property name="advice">
			<bean class="org.compass.spring.aop.CompassDeleteAdvice">
				<property name="compass" ref="compass" />
			</bean>
		</property>
		<property name="pattern" value=".*delete" />
	</bean>

	<bean id="proxyCreator"
		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="beanNames">
			<list>
				<value>genericDao</value>
			</list>
		</property>
		<property name="interceptorNames">
			<list>
				<value>compassSaveAdvisor</value>
				<value>compassDeleteAdvisor</value>
			</list>
		</property>
	</bean>

 

其中上面的genericDao指我们的DAO文件的bean。它必须是实现接口的实现,否则的时候在获取genericDao的Bean时,会出现ClassCastException,这是J2se反射的机制。

 

三、测试代码

		GenericDao gd = (GenericDao) factory.getBean("genericDao");
		Course course = gd.getbyID(Course.class, 1);
		HomeTown ht = gd.getbyID(HomeTown.class, 1);
		List<Course> courses = new ArrayList<Course>();
		courses.add(course);
		
		Student s = new Student();
		s.setName("Jerry");
		s.setCourses(courses);
		s.setHomeTown(ht);
		gd.save(s);
		
		CompassDaoImpl cd = (CompassDaoImpl) factory.getBean("compassDAO");
		List<SearchRequest.SearchField> searchFields = new ArrayList<SearchRequest.SearchField>();
		SearchRequest.SearchField s1 = new SearchRequest().new SearchField();
		s1.setFieldName("Student.name");
		s1.setFieldType(Occur.MUST);
		s1.setFieldValue("Jerry");
		searchFields.add(s1);
		
		SearchRequest sr = new SearchRequest();
		sr.setAlias("Student");
		sr.setEntityClass(Student.class);
		sr.setFields(searchFields);
		List<Student> students = cd.searchByOSEM(sr);
		if(students.size() <1){
			System.out.println("长度为0");
		}else{
			for(Student sd : students){
				System.out.println(sd.getName() + "---" + sd.getHomeTown().getCity());
			}
		}
	}

 

我们首先通过Dao增中一条学生记录,紧接着,立刻搜索学生,控制台输入内容正确。测试成功!

你可能感兴趣的:(DAO,spring,AOP,Hibernate,bean)