0

住所を更新したい。

@Modifying
@Query("update UserInfo u set u.address = ?1 where u.username = ?2")
void setAddress(String address, String username);

しかし、うまくいきません。

org.springframework.web.util.NestedServletException: Request processing failed; 
nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: 
|Exception Description: No transaction is currently active; nested exception is 
javax.persistence.TransactionRequiredException: |Exception Description: No transaction 
is currently active

そして、クエリ文字列が不明なソースであると表示されます

この例外を占める理由はどれですか?

更新クエリで複数のフィールドを更新する方法は?

@Query("update UserInfo u set u.address = ?1, u.phoneNumber = ?2 where u.username = ?3")
4

2 に答える 2

0

まず、以下のようなコンテキストを作成する必要があります。データベースと ORM ベンダーを変更できます。これで、トランザクション マネージャーができました。次に、メソッドで @Transactional アノテーションを使用します。

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="true" />
            <property name="database" value="HSQL" />
        </bean>
    </property>
    <property name="persistenceUnitName" value="jpa.sample" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<jdbc:embedded-database id="dataSource" type="HSQL" />
于 2013-07-29T11:03:43.477 に答える