java Spring 3.1.1 with hibernate 4.1 annotations 配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10315696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:32:14  来源:igfitidea点击:

Spring 3.1.1 with hibernate 4.1 annotations configuration

javaspringhibernateannotations

提问by Parthi

I am setting up the new project with spring3.1.1 and hibernate 4.1. When I run my project I am getting the following error

我正在使用 spring3.1.1 和 hibernate 4.1 设置新项目。当我运行我的项目时,我收到以下错误

java.lang.NoSuchMethodError: org.hibernate.SessionFactory.getCurrentSession()Lorg/hibernate/classic/Session;
at com.humancapital.dao.TestModelDAOImpl.getTestModelList(TestModelDAOImpl.java:22)

My applicationContext.xml

我的 applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?>


        <context:component-scan base-package="com.example"/>

        <!-- Use @Transaction annotations for managing transactions  -->    
        <tx:annotation-driven transaction-manager="transactionManager"/>

        <!-- PropertyConfiguer -->
        <bean id="propertyCongigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="/WEB-INF/config/jdbc/jdbc.properties"></property>
        </bean>

        <!--  DataSource connection -->
        <bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${jdbc.driverClassName}"></property>
            <property name="url" value="${jdbc.url}"></property>
            <property name="username" value="${jdbc.username}"></property>
            <property name="password" value="${jdbc.password}"></property>  
        </bean>

        <!-- Hibernate SessionFactory -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="myDataSource"></property>
            <property name="annotatedClasses">
                <list>
                    <value>com.humancapital.dao.TestModel</value>
                </list>
            </property>
            <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop> 
            </props>
            </property>
        </bean>



        <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>

        <context:annotation-config/>

        <bean id="testModelDAO" class="com.example.dao.TestModelDAOImpl"></bean>

MyDAOImpl

我的DAOImpl

@Repository
public class TestModelDAOImpl implements TestModelDAO {

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

@SuppressWarnings("rawtypes")
@Override
    @Transactional(readOnly=true,propagation=Propagation.REQUIRES_NEW)
public List getTestModelList() {
    System.out.println(sessionFactory.toString());
    return sessionFactory.getCurrentSession().createCriteria(TestModel.class).list();
}
}

I have added my dependency jars please suggest me

我已经添加了我的依赖 jars 请给我建议

antlr-2.7.7.jar
antlr-runtime-3.0.jar
commons-collections-3.2.1.jar
commons-dbcp-1.4.jar
commons-fileupload-1.2.jar
commons-lang-2.4.jar
commons-logging.jar
commons-pool-1.5.4.jar
dom4j-1.6.1.jar
ejb3-persistence-3.3.1.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.1.1.Final.jar
hibernate-entitymanager-4.1.1.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-validator-4.0.2.GA.jar
Hymanson-core-asl-1.9.2.jar
Hymanson-jaxrs-1.8.5.jar
Hymanson-mapper-asl-1.9.2.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.CR2.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
json-lib-2.3-jdk13.jar
jstl-1.2.jar
jta-1.1.jar
log4j-1.2.14.jar
mail.jar
mysql-connector-java-5.0.8-bin.jar
org.springframework.aop-3.1.1.RELEASE.jar
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.aspects-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context.support-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.jdbc-3.1.1.RELEASE.jar
org.springframework.jms-3.1.1.RELEASE.jar
org.springframework.js-2.0.8.RELEASE.jar
org.springframework.orm-3.1.1.RELEASE.jar
org.springframework.oxm-3.1.1.RELEASE.jar
org.springframework.test-3.1.1.RELEASE.jar
org.springframework.transaction-3.1.1.RELEASE.jar
org.springframework.web.servlet-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
servlet-api.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar
slf4j-simple-1.6.1.jar
spring-modules-validation-0.7.jar
standard.jar

Please Help me to complete the project setup. Please give some suggestion where we need to concentrate while setting up the new project structure where fewer changes are necessary for future up gradation.

请帮助我完成项目设置。请提出一些建议,我们在建立新的项目结构时需要集中精力的地方,以便将来升级所需的更改较少。

Sorry I am new to stackoverflow I don't know how to give replay to the commands

抱歉,我是 stackoverflow 的新手,我不知道如何重播命令

回答by danny.lesnik

It happens becuase you have hibernate.current_session_context_classproperty enabled

发生这种情况是因为您处于休眠状态。current_session_context_class启用属性

you should NEVER use that property when combining Hibernate with Spring that destroys proper session and transaction management. The only time you want to set this property is if you use Spring and Hibernate in a JTA environment, else don't use it.

在将 Hibernate 与破坏正确会话和事务管理的 Spring 结合使用时,您永远不应该使用该属性。唯一要设置此属性的时间是在 JTA 环境中使用 Spring 和 Hibernate 时,否则不要使用它。