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

首先,您应该创建一个如下所示的上下文,您可以更改 db 和 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 回答
0

当前没有事务处于活动状态,所以只需添加一个注解告诉spring,这是事务

@Modifying
@Transactional
@Query("update UserInfo u set u.address = ?1 where u.username = ?2")
void setAddress(String address, String username);
于 2013-07-29T10:01:10.763 回答