In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to solve the Spring data jpa @ Query update pit, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!
The pit of Spring data jpa @ Query update
Jpa has only the save (Entity) method by default, which is added if there are no records in the database, and updates the records if there are records in the database.
If you want to add the update (Entity) method manually
You can refer to this example @ Modifying @ Query (value = "UPDATE SmallFileEntity s SET s.length ='# {entity.getLength ()}', s.modificationTime ='# {entity.getModificationTime ()}', s.accessTime ='# {entity.getAccessTime ()} 'WHERE s.autoId =' # {entity.getAutoId ()}'", nativeQuery = false) void updateLengthAndModificationTimeAndAccessTime (SmallFileEntity entity)
The focus of the above example is to enclose the EL expression in single parentheses.
Java statements can be written in EL expressions, and in the @ Query annotation, EL expressions can fetch objects in the parameter list.
If it really doesn't work, nativeQuery = true.
Update operation of Spring data jpa
There are two main ways to use jpa for update operations:
1. Call the method to save the entity
1) Save an entity: repository.save (T entity)
2) Save multiple entities: repository.save (Iterable entities)
3) Save and immediately refresh an entity: repository.saveAndFlush (T entity)
Note: if you change, the primary key field must be set in entity, otherwise it will not correspond to the records in the database and become new (database automatically generates primary key) or error (database does not automatically generate primary key).
2. @ Query annotation, write the JPQL statement yourself
Example:
@ Modifying@Query ("update ShopCoupon sc set sc.deleted = true where sc.id in: ids") public void deleteByIds (@ Param (value = "ids") List ids)
Note:
1) when update or delete, you must annotate the method with @ Modifying in order to let ORM know that it is a write operation.
2) sometimes without the @ Param annotation parameter, the following exception may be reported:
Org.springframework.dao.InvalidDataAccessApiUsageException: Name must not be null or emptiness; nested exception i is Java.lang.IllegalArgumentException: Name must not be null or empty!
The above is all the contents of the article "how to solve the pit of Spring data jpa @ Query update". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.