当前位置: 首页 > 知识库问答 >
问题:

组织。springframework。刀。HibernateTemplate中的InvalidDataAccessApiUsageException

邹缪文
2023-03-14

我使用hibernate-4.1.2和spring-framework-4.1.6中的所有jar文件来尝试hibernateTemplate问题中指定的项目。我正面临一个组织。springframework。刀。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注释来实现这些功能??

共有2个答案

熊俊人
2023-03-14

解决方法:使用com。springsource。组织。aopalliance-1.0.0。jar和@Transactional注释问题可以解决。

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

test.xml

<?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>

已启用的批注可用于导入组织。springframework。交易注释。对指定的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;
    }

                    .
                    .
                    .
                    .

}
黎承颜
2023-03-14

你能试试getHibernateTemplate()吗。getSessionFactory()。getCurrentSession‌​().设置FlushMode(Flus‌​hMode。自动)或将其添加到应用程序上下文中。xml

 类似资料:
  • 通用方法 (以下)管制员工作正常 另一个跟上面一样的控制器,但给出了一个例外 例外是 例外组织。springframework。刀。InvalidDataAccessApiUsageException:对象引用未保存的临时实例-在刷新之前保存临时实例:com。软的企业资源计划赞成的意见。模型ProPurchaseOrderModel;嵌套的异常是org。冬眠TransientObjectExcep

  • 我添加了以下代码以将记录插入/更新到表中 如果数据存在,它将更新表,否则将插入一个新记录,在我的情况下,数据已经存在,所以我首先更新记录,如果循环工作,但我得到 org.springframework.dao.DataIntegrityViolationException: key'umul_facebook_id_UNIQUE'的重复条目'482186425258498';SQL[n/a];约束

  • 我正在实现SimpleAsyncTaskExec导师。当我插入或更新到数据库时,它会抛出以下异常org.springframework.dao.InvalidDataAccessApiUsageExc0019:执行更新/删除查询;嵌套异常是javax.persistence.Transaction要求异常:执行更新/删除查询错误。 Spring的背景。xml 错误详细信息:

  • 我试图用Spring DI创建一个简单的HelloWorld应用程序。我创建了一个Java项目并导入了一些教程类,其中一个很简单: 然而,我得到了以下错误:。我正在使用Eclipse Spring Tool Suite 3.7.2,当我单击“修复项目设置”时,我认为通过使用这个Eclipse版本,它会为我添加这些依赖项。我在这里做错了什么?即使在STS中,我也需要手动添加这些依赖项吗?如果是,正确

  • 我正在将WebLogic Server 11g(10.3.6.0)升级到12c(12.2.1.3.0)。 我在12c服务器上没有问题地安装了war文件。然后,我尝试启动模块。由于缺少特定的类文件,我得到了错误。似乎该类文件属于“spring-aop-2.5.2.jar.” 当我在WebLogic Server 11g环境中部署相同的war文件时,我没有发现任何问题。我确保war文件中存在“spri

  • 我在Spring的申请中做了联系表格。我有一个问题: 字段名称上的对象接触中的字段错误:拒绝的值[null];代码[NotBlank.contact.name, NotBlank.name, NotBlank.java.lang.String, NotBlank];参数[org.springframework.context.support.DefaultMessageSourceResolabl