springboot之使⽤springdatajpa的⾃定义sql
spring data jpa介绍
PA(Java Persistence API)是Sun官⽅提出的Java持久化规范。它为Java开发⼈员提供了⼀种对象/关联映射⼯具来管理Java应⽤中的关
系数据。他的出现主要是为了简化现有的持久化开发⼯作和整合ORM技术,结束现在Hibernate,TopLink,JDO等ORM框架各⾃为营的
局⾯。值得注意的是,JPA是在充分吸收了现有Hibernate,TopLink,JDO等ORM框架的基础上发展⽽来的,具有易于使⽤,伸缩性强等
优点。
⾃定义SQL查询
Spring data 觉⼤部分的SQL都可以根据⽅法名定义的⽅式来实现,但是由于某些原因我们想使⽤⾃定义的SQL来查询,spring data也是
完美⽀持的;在SQL的查询⽅法上⾯使⽤@Query注解,如涉及到删除和修改在需要加上@Modifying.
public interface CustomerRepository extends JpaRepository<Customer, Integer>{
@Modifying
@Query("update Customer c set c.customerName=?1 where c.id=?2")
Integer modifyByIdAndUserId(String customerName,Integer id);
@Modifying
@Query("delete from Customer where id = ?1")
void deleteByCustomerId(Integer id);
}
注意当调⽤⾃定义更新和删除sql操作时,会出现下⾯的异常
org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; nested exception is javax.persistence.TransactionRequ    at vertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:396)
at jpa.anslateExceptionIfPossible(HibernateJpaDialect.java:227)
at anslateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
at org.springframework.dao.anslateExceptionIfPossible(ChainedPersistenceExceptionTranslator    at org.springframework.dao.anslateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.pository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke 
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethod    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$difyByIdAndUserId(Unknown Source)
at com.tLoads(HelloPeopleApplicationTests.java:42)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at flect.Method.invoke(Method.java:498)
at org.del.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.del.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.del.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at st.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at st.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
at st.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at st.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at st.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at st.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at st.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at st.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at st.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at st.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
lipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
lipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
lipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
lipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
lipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
lipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query
at org.hibernate.query.uteUpdate(AbstractProducedQuery.java:1496)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at flect.Method.invoke(Method.java:498)
at jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:380)
at com.sun.proxy.$uteUpdate(Unknown Source)
at org.springframework.pository.query.JpaQueryExecution$ModifyingExecution.doExecute(JpaQueryExecution.java:256)
at org.springframework.pository.ute(JpaQueryExecution.java:91)
at org.springframework.pository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:136)
at org.springframework.pository.ute(AbstractJpaQuery.java:125)
at org.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport    at org.support.RepositoryFactorySupport$QueryExecutorMet
hodInterceptor.invoke(RepositoryFactorySupport    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
springboot aopat ansaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at ansaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
... 41 more
对于该异常的原因是更新和删除操作没有进⾏事务处理,解决办法是在service层调⽤CustomerRepository接⼝的⽅法的⽅法上添加注解
@Transactional进⾏事务的处理,然后在调⽤service层的⽅法
@Service
public class CustomerService {
@Autowired
private CustomerRepository customerRepository;
@Transactional
public void deleteByCustomerId(Integer id) {
customerRepository.deleteByCustomerId(id);
}
@Transactional
public Integer modifyByIdAndUserId(String customerName,Integer id) {
difyByIdAndUserId(customerName, id);
}
Integer line = difyByIdAndUserId("张三", 4);
System.out.println(line);
customerService.deleteByCustomerId(3);
执⾏后如图所⽰:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。