HibernateTemplate中的org.springframework.dao.InvalidDataAccessApiUsageException

问题描述 投票:0回答:2

我正在尝试使用hibernate-4.1.2和spring-framework-4.1.6中的所有jar文件在hibernateTemplate problem中指定的项目。我正面临org.springframework.dao.InvalidDataAccessApiUsageException

Dec 17, 2017 1:22:19 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4dcbadb4: startup date [Sun Dec 17 13:22:19 IST 2017]; root of context hierarchy
Dec 17, 2017 1:22:19 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [resources/test.xml]
Dec 17, 2017 1:22:21 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Dec 17, 2017 1:22:21 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.2}
Dec 17, 2017 1:22:21 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Dec 17, 2017 1:22:21 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Dec 17, 2017 1:22:23 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.OracleDialect
Dec 17, 2017 1:22:23 PM org.hibernate.dialect.Oracle9Dialect <init>
WARN: HHH000063: The Oracle9Dialect dialect has been deprecated; use either Oracle9iDialect or Oracle10gDialect instead
Dec 17, 2017 1:22:23 PM org.hibernate.dialect.OracleDialect <init>
WARN: HHH000064: The OracleDialect dialect has been deprecated; use Oracle8iDialect instead
Dec 17, 2017 1:22:23 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
Dec 17, 2017 1:22:23 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Dec 17, 2017 1:22:23 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Dec 17, 2017 1:22:23 PM org.hibernate.tuple.PojoInstantiator <init>
INFO: HHH000182: No default (no-argument) constructor for class: model.Student (class must be instantiated by Interceptor)
Dec 17, 2017 1:22:27 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
Dec 17, 2017 1:22:27 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
Dec 17, 2017 1:22:27 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
Dec 17, 2017 1:22:30 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: SYSTEM.STUDENT
Dec 17, 2017 1:22:30 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [address, name, id, email]
Dec 17, 2017 1:22:30 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
Dec 17, 2017 1:22:30 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [sys_c007625]
Dec 17, 2017 1:22:30 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
    at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1135)
    at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:620)
    at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:617)
    at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:340)
    at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:308)
    at org.springframework.orm.hibernate4.HibernateTemplate.save(HibernateTemplate.java:617)
    at dao.StudentDaoImplHT.save(StudentDaoImplHT.java:20)
    at test.SaveClient.main(SaveClient.java:16)

上面提到的错误堆栈建议将会话的刷新模式设置为COMMIT / AUTO或从事务定义中删除'readOnly'标记。有没有办法通过spring依赖注入或hibernate注释来做这些事情?

java spring hibernate
2个回答
0
投票

你可以尝试getHibernateTemplate().getSessionFactory().getCurrentSession‌​().setFlushMode(Flus‌​hMode.AUTO);或将其添加到您的applicationcontext.xml <property name="checkWriteOperations" value="false"></property>


0
投票

解决:通过使用com.springsource.org.aopalliance-1.0.0.jar和@Transactional注释问题可以解决。

启用事务:可以通过配置文件启用事务注释:

test.hml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- Enable Annotation based Declarative Transaction Management -->
    <tx:annotation-driven proxy-target-class="true" transaction-manager="tm" /> 

    <!-- Creating TransactionManager Bean-->
    <bean id="tm"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="bds" />
    </bean>

    <!-- Creating DataSource Bean-->
    <bean id="bds" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
        <property name="username" value="system"/>
        <property name="password" value="manager"/>
        <property name="maxActive" value="15"/>
        <property name="maxIdle" value="5"/>
        <property name="maxWait" value="5000"/>
    </bean> 

    <!-- Creating SessionFactory Bean-->
    <bean id="sf" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="bds" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>model.Student</value>
            </list>
        </property>
    </bean>

    <bean id="ht" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory" ref="sf" />
    </bean>

    <bean id="dao" class="dao.StudentDaoImplHT">
        <property name="ht" ref="ht" />
    </bean>

</beans>

通过将org.springframework.transaction.annotation.Transactional导入到宽度为java的类,可以使用启用的注释。通过@Transactional(readOnly = false)注释,readOnly标记设置为false,并且可以轻松地将事务提交到表中。

package dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;
import model.Student;

public class StudentDaoImplHT implements StudentDaoInterface {

    private HibernateTemplate ht;

    public void setHt(HibernateTemplate ht) {
        this.ht = ht;
    }

    @Override
    @Transactional(readOnly = false)
    public int save(Student st) {

        int i=(Integer)ht.save(st);
        return i;
    }

                    .
                    .
                    .
                    .

}
© www.soinside.com 2019 - 2024. All rights reserved.