2016-10-14 18 views
0

この質問にはいくつかの記事がありますが、解決策がまだありません。 これは親クラスUserrです。 @OneToMany関係では、特定の子アカウントを削除したいと考えています。行を削除できません:TransactionRequiredException:更新/削除クエリの実行

今私は "DELETE"クエリでこれを行うと、次の例外を取得しています。

org.springframework.dao.InvalidDataAccessApiUsageException:更新/削除クエリを実行しています。ネストされた例外はjavax.persistence.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 like this

<?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="[email protected]"/> 
    <property name="password" value="[email protected]"/> 
    <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> 
あなたは、コードの下に追加する必要がありSpringコンテキストファイルで0

+0

のように、メッセージのようにTRANSACTIONが必要です。そして?あなたの取引はどこですか? –

+0

問題の関連するポイントは、アプリケーションでトランザクションをどのように設定しているかです。質問に追加する必要があります。 'deleteClientByClientId'はトランザクションなしで実行され、失敗しています。 –

+0

@NeilStocktonが質問を編集しました。そこにはまだ同じエラーが表示されます –

答えて

1

  1. あなたは春ベースのトランザクションを使用している場合は、必ずあなたのケースでは@Transactional注釈 ためorg.springframework.transaction.annotation.Transactionalクラスをインポートしますそれは可能性がありますjavax.transaction.Transactional

  2. 私は静的としてあなたのコードでdeleteClientByClientIdメソッドを参照してください。 @Transactionalは静的メソッドをサポートしていません。あなたのメソッドを非静的にして、トランザクションとして注釈を付けます。 あなたはあなたのために働くのオプション、私に教えてください@Transactional with static method

を参照することができます。

+0

ありがとうございますReena。オプション2は私のために働いた –

0

:また

<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 security Bean構成を追加します。あなたのコードに次のことを確認することができます

+0

に追加した後でも同じエラーを受けています。すでに追加されています –

関連する問題