Sunday 15 May 2011

java - How to obtain the current session in hibernate -



java - How to obtain the current session in hibernate -

i'm trying current session in hibernate create criteria query, i'm getting error.

here code:

datasource.xml:

<bean id="entitymanagerfactory" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean"> <property name="jpavendoradapter" ref="jpavendoradapter" /> <property name="datasource" ref="datasource" /> <property name="persistenceunitname" ref="persistenceunit" /> <property name="jpaproperties" ref="jpaproperties" /> </bean> <bean id="sessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean"> <property name="datasource" ref="datasource" /> </bean> <bean id="transactionmanager" class="org.springframework.orm.jpa.jpatransactionmanager"> <property name="entitymanagerfactory" ref="entitymanagerfactory" /> <property name="datasource" ref="datasource" /> <property name="globalrollbackonparticipationfailure" value="true" /> </bean> <bean id="jpavendoradapter" class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter"> <property name="databaseplatform" ref="sqldialect" /> </bean>

class java:

@autowired private sessionfactory sessionfactory; session session = sessionfactory.getcurrentsession(); fulltextentitymanager fulltextentitymanager = search.getfulltextentitymanager(em); list<product> bookresult = new arraylist<>(); seek { // ensure index inserted info created. fulltextentitymanager.createindexer().startandwait(); querybuilder qb = fulltextentitymanager.getsearchfactory().buildquerybuilder().forentity(product.class).get(); org.apache.lucene.search.query query = qb.keyword().onfields("name").matching("cocacola").createquery(); javax.persistence.query jpaquery = fulltextentitymanager.createfulltextquery(query, product.class); criteria criteria = session.createcriteria(product.class); criteria.add(restrictions.eq("category.supermarket", "mercadona")); list<product> productos = criteria.list();

but error:

org.hibernate.hibernateexception: no session found current thread @ org.springframework.orm.hibernate4.springsessioncontext.currentsession(springsessioncontext.java:106)

how solve this?

have add together servlet-context.xml

<tx:annotation-driven transaction-manager="transactionmanager" />

to utilize @transaction?

and check if @autowired working right way illustration system.out.println(sessionfactory) in class.java sure isn't null.

java spring hibernate criteria

No comments:

Post a Comment