Spring Autowiring + Hibernate Search
I am trying to inject a hibernate session to use with hibernate search and am having trouble figuring out how to setup the spring beans for hibernate search. I am aware that spring does not support hibernate search. I had this working before i decided to use hibernateDAO. Any suggestions would be much appreciated.
public class RestaurantDAOImpl extends HibernateDaoSupport implements RestaurantDAO{
public String getDishResults(final String query, final String location){
return getHibernateTemplate().execute( new HibernateCallback<String>() {
@Override
public String doInHibernate(Session session) throws HibernateException, SQLException {
FullTextSession fullTextSession = Search.getFullTextSession(session);
fullTextSession.beginTransaction();
QueryBuilder qBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity( Dish.class ).get();
Query luceneQuery = qBuilder.keyword()
.onFields("dishName").matching("Burger")
.createQuery();
...
return // code that generates json result based on hibernate search
});
}
}
spring context
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="configLocation">
<value>hibernate.cfg.xml</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="hibernateProperties">
<props>
<!-- <prop key="hibernate.hbm2ddl.auto">create</prop> -->
<prop key="hibernate.connection.pool_size">1</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<!--MySQL -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/database</prop>
<prop key="hibernate.connection.username">root</prop>
<prop key="hibernate.connection.password"></prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.bytebite.entities.Restaurant</value>
<v开发者_JAVA百科alue>com.bytebite.entities.Dish</value>
</list>
</property>
</bean>
<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor"
autowire="byName" /><!--sessionFactory will get autowired -->
<bean id="restaurantDAOTarget" class="com.bytebite.dao.RestaurantDAOImpl"
autowire="byName" /><!--sessionFactory will get autowired -->
<bean id="RestaurantDAO" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.bytebite.dao.RestaurantDAO</value>
</property>
<property name="interceptorNames">
<list>
<value>hibernateInterceptor</value>
<value>restaurantDAOTarget</value>
</list>
</property>
</bean>
And i have to somehow reference these (which were in hibernate.cfg.xml). Could I connect the hibernate context to hibernate.cfg.xml? would that help?
hibernate.cfg.xml (what remains of it anyway. where should i put this?)
<!-- Hibernate Search -->
<property name="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider</property>
<property name="hibernate.search.default.indexBase">/Users/amandacanyon/Bytebite/Bytebite/.luceneIndex</property>
Is your problem what to do with the properties from hibernate.cfg.xml? That's what the <property name="hibernateProperties">
is for in the factory bean. You put all the other properties there. Are you saying that adding these last two breaks something? You can tell the factory bean to reference the hibernate config with setConfigLocation() if you really want to, but simply moving those properties into the Spring config should accomplish exactly the same thing.
See also http://community.jboss.org/wiki/SpringHibernateHibernateSearch (even though this example uses JPA)
精彩评论