2013-07-29 78 views
0

我想更新地址。彈簧數據jpa查詢字符串錯誤

@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") 

回答

0

當前沒有事務處於活動狀態,因此只需要加一個註釋告訴春天,這是交易

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

首先,你應該爲那種下面創建一個背景下,您可以更改數據庫和ORM vendor.Now你有交易經理。然後在你的方法上使用@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" />