java - Hibernate+Spring SessionFactory configuration -


what right way configure sessionfactory?

if way:

<tx:annotation-driven transaction-manager="transactionmanager" />  <bean id="sessionfactory" class="org.springframework.orm.hibernate5.localsessionfactorybean"              p:datasource-ref="datasource"              p:packagestoscan="ua.com.javer.flowerexpert"/>  <bean id="transactionmanager" class="org.springframework.orm.hibernate5.hibernatetransactionmanager"             p:sessionfactory-ref="sessionfactory" /> 

i error:

nested exception org.hibernate.hibernateexception: not obtain transaction-synchronized session current thread 

and if change annotationsessionfactorybean:

    <bean id="sessionfactory" class="org.springframework.orm.hibernate3.annotation.annotationsessionfactorybean"              p:datasource-ref="datasource"              p:packagestoscan="ua.com.javer.flowerexpert"/> 

i get:

nested exception java.lang.noclassdeffounderror: [lorg/hibernate/engine/filterdefinition; 

even though in older project hibernate3.annotation.annotationsessionfactorybean works fine.

my pom.xml contains:

        <dependency>         <groupid>org.hibernate</groupid>         <artifactid>hibernate-core</artifactid>         <version>5.0.1.final</version>         <exclusions>             <exclusion>                 <groupid>org.hibernate.javax.persistence</groupid>                 <artifactid>hibernate-jpa-2.1-api</artifactid>             </exclusion>         </exclusions>     </dependency>      <dependency>         <groupid>org.hibernate</groupid>         <artifactid>hibernate-entitymanager</artifactid>         <version>5.0.1.final</version>     </dependency>      <dependency>         <groupid>c3p0</groupid>         <artifactid>c3p0</artifactid>         <version>0.9.1.2</version>     </dependency>      <dependency>         <groupid>org.springframework</groupid>         <artifactid>spring-orm</artifactid>         <version>${spring.version}</version>     </dependency> 

here's service class:

@service("colorsservice") @transactional public class colorsservice { @autowired private colordao colordao;  public list<color> getallcolors() {     return colordao.getallcolors(); } } 

and here's dao:

@component @repository("colordao") public class colordaohibernate implements colordao {  @autowired private sessionfactory sessionfactory;  public colordaohibernate() { }  @override public list<color> getallcolors() {     session session = sessionfactory.getcurrentsession(); // statelesssession session = sessionfactory.openstatelesssession();     query query = session.createquery("from color");     return  query.list(); } } 

notice:

if use sessionfactory.openstatelesssession(); in dao class hibernate5.localsessionfactorybean in session configuration not cause problem.

but point - want use sessionfactory.getcurrentsession(); how can achieve this?

hope enabled transaction support in spring configuration file. if not, enable using <tx:annotation-driven>

and also, declare transactionmanager follows:

<bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">     <property name="datasource" ref="datasource"/> </bean> 

try removing @transactional colorsservice below:

@service("colorsservice") public class colorsservice { @autowired private colordao colordao;  public list<color> getallcolors() {     return colordao.getallcolors(); } } 

and adding colordaohibernate:

@repository("colordao") public class colordaohibernate implements colordao {  @autowired private sessionfactory sessionfactory;  public colordaohibernate() { } @transactional @override public list<color> getallcolors() {     session session = sessionfactory.getcurrentsession(); // statelesssession session = sessionfactory.openstatelesssession();     query query = session.createquery("from color");     return  query.list(); } } 

edit sessionfactory bean definition below:

<bean id="hibernateprops"                 class="org.springframework.beans.factory.config.propertiesfactorybean">                 <property name="properties">                     <props>                         <prop key="hibernate.current_session_context_class">thread</prop>                     </props>                 </property>             </bean>      <bean id="sessionfactory"                 class="org.springframework.orm.hibernate5.localsessionfactorybean"                 p:datasource-ref="datasource" p:packagestoscan="ua.com.javer.flowerexpert"                 p:hibernateproperties-ref="hibernateprops" /> 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -