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

无法删除行:TransactionRequiredException:执行更新/删除查询

雍光远
2023-03-14

关于这个问题有好几篇帖子,但仍然没有找到答案。这是父类Userr。在@OneToMany关系中,我想删除一个特定的子帐户。

现在,当我通过“删除”查询执行此操作时,我得到以下异常。

组织。springframework。刀。InvalidDataAccessApiUsageException:执行更新/删除查询;嵌套的异常是javax。坚持不懈TransactionRequiredException:执行更新/删除查询

@RooJavaBean
@RooToString
@RooJpaEntity
@RooJpaActiveRecord(finders = { "findUserrsByUserName"})
public class Userr {


@NotNull
@Column(unique = true)
private String userName;


@NotNull
private int userType;

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Account> accounts = new ArrayList<Account>();
}

儿童班

@RooJavaBean
@RooToString
@RooJpaActiveRecord
@RooJpaEntity
public class Account {

@OneToMany(mappedBy="account", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
List<Message> messages = new ArrayList<Message>();


/*@OneToMany(mappedBy="account", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
List<PremiumPlayPositionCombination> premiumPlayPosition = new ArrayList<PremiumPlayPositionCombination>();*/


@OneToMany(mappedBy="account", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
List<PositionCombinationArc> allPositionsArc = new ArrayList<PositionCombinationArc>();

@ManyToOne
@JoinColumn(name="user_id")
private Userr user;
}

这是我的删除查询

@Transactional
public static void deleteClientByClientId(Long clientId) {
    System.out.println("Delete query findUsersClientsByUser" + clientId);
    int deleteCount= entityManager().createQuery("DELETE FROM Account where id =:clientId").setParameter("clientId", clientId).executeUpdate();
    System.out.println("Delete query findUsersClientsByUser" + deleteCount);



}

我在ApplicationContext中添加了安全性。像这样的xml

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"      xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!--
    This will automatically locate any and all property files you have
    within your classpath, provided they fall under the META-INF/spring
    directory. The located property files are parsed and their values can
    then be used within application context files in the form of
    ${propertyKey}.
-->
<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
<!--
    Turn on AspectJ @Configurable support. As a result, any time you
    instantiate an object, Spring will attempt to perform dependency
    injection on that object. This occurs for instantiation via the "new"
    keyword, as well as via reflection. This is possible because AspectJ
    is used to "weave" Roo-based applications at compile time. In effect
    this feature allows dependency injection of any object at all in your
    system, which is a very useful feature (without @Configurable you'd
    only be able to dependency inject objects acquired from Spring or
    subsequently presented to a specific Spring dependency injection
    method). Roo applications use this useful feature in a number of
    areas, such as @PersistenceContext injection into entities.
-->
<context:spring-configured/>
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
    <property name="driverClassName" value="${database.driverClassName}"/>
    <property name="url" value="${database.url}"/>
    <property name="username" value="${database.username}"/>
    <property name="password" value="${database.password}"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="dataSource" ref="dataSource"/>
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com"/>
    <property name="port" value="587"/>
    <property name="username" value="noreply@uforic.in"/>
    <property name="password" value="noreply@123"/>
    <property name="javaMailProperties">
        <props>
            <prop key="mail.transport.protocol">smtp</prop>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
            <prop key="mail.debug">true</prop>
        </props>
    </property>
</bean>
<!--
    This declaration will cause Spring to locate every @Component,
    @Repository and @Service in your application. In practical terms this
    allows you to write a POJO and then simply annotate the new POJO as an
    @Service and Spring will automatically detect, instantiate and
    dependency inject your service at startup time. Importantly, you can
    then also have your new service injected into any other class that
    requires it simply by declaring a field for your service inside the
    relying class and Spring will inject it. Note that two exclude filters
    are declared. The first ensures that Spring doesn't spend time
    introspecting Roo-specific ITD aspects. The second ensures Roo doesn't
    instantiate your @Controller classes, as these should be instantiated
    by a web tier application context. Refer to web.xml for more details
    about the web tier application context setup services.

    Furthermore, this turns on @Autowired, @PostConstruct etc support. These 
    annotations allow you to use common Spring and Java Enterprise Edition 
    annotations in your classes without needing to do any special configuration. 
    The most commonly used annotation is @Autowired, which instructs Spring to
    dependency inject an object into your class.
-->
<context:component-scan base-package="com.uforic.optionstrader">
    <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
    <!--context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/-->
</context:component-scan>

共有3个答案

龚寂弦
2023-03-14

当尝试使用HiberNate更新/删除时,最好使用一个事务初始化()和提交()示例来包围查询:

EntityTransaction tr=em.getTransaction();
tr.begin();

Query query = (Query) em.createQuery( "update etudiant set  email= :em, adresse= :adr,telephone= :tele,password= :pwd"
        + " where email= :mail");
        query.setParameter("em", email)
        .setParameter("adr", adresse)
        .setParameter("tele", tele)
        .setParameter("pwd", pass)
        .setParameter("mail", ancienEmail);
        
int a= query.executeUpdate();
tr.commit();
柳浩大
2023-03-14

在Spring上下文文件中,您需要添加以下代码:

<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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd">

<tx:annotation-driven />

另外,添加您的Spring Securitybean配置

锺离浩慨
2023-03-14

您可以在代码中检查以下内容:

>

我在代码中看到deleteClientByClientId方法是静态的@spring中的Transactional不支持静态方法。使您的方法成为非静态的,被注释为事务性的。您可以使用静态方法引用@Transactional

让我知道,哪个选项适合你。

 类似资料:
  • 问题内容: 我在Spring和mongodb中使用了hibernate JPA,并且我在Glassfish-4.0上运行我的应用程序。 我的服务类别是: 我的spring-context.xml是: 我在代码中应用了一些更改,但是它们没有效果。谁能帮我解决这个问题。提前致谢。 问题答案: 我不确定这是否会帮助你解决问题(即是否仍然存在),但是,在网上搜索类似问题之后。 我从持久性EntityMan

  • 我有父实体和一个方向作为具有关系的子实体。我尝试使用Hibernate查询删除超过一周的广告,但得到的结果是: com.mysql.jdbc.exceptions.jdbc4.mysqlintegrityConstraintViolationException:无法删除或更新父行:外键约束失败(.,constraint外键()引用() 用户: 筛选器: 广告: DAO删除方法:

  • 我有两个对象实体(用户和电话),它们应该有多对多关系。 使用者JAVA 电话JAVA 现在,我在用户表中添加了两个ID为1和2的用户。然后,我添加一个id为1的手机,并将它们映射到两个用户id(1) 我的用户电话表如下所示: 现在,我想删除一个ID为2的用户。当我尝试这样做时,我得到了一个错误 我的删除脚本: 知道我哪里出错了吗?非常感谢:)

  • 我正在尝试使用query-更新行(删除列str_column中的连字符和连字符后的所有字符)- 但上面的查询是删除匹配的行而不是更新。 我不明白,如何更新查询可以删除行??

  • 我正在尝试更新JTable(或data?)的行在我对一行执行删除操作后,当选择另一行时,可以编辑所选行。当前删除该行将使已删除行下方的行上移,但如果选择了该行号并尝试进行编辑,则会返回(JOptionPane),就像未选择任何内容一样。它似乎没有“刷新”数据(?)。 这是一个SCCE,希望有人可以运行它并指出问题。我试着尽可能地缩小它,以便于复制/粘贴: 我在这里看到这个问题的各种形式都有一些不同

  • 当我试图删除宠物时,我得到了这个错误。这个宠物,有访问(子),但我在宠物实体中定义了CASCADE. ALL。任何想法?错误:无法删除或更新父行:外键约束失败(。 访问类: