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/02 Report--
Editor to share with you how to solve the problems encountered by SpringDataJpa to write native sql, 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 together.
Problems encountered by SpringDataJpa in writing native sql @ Repositorypublic interface EduCourseDao extends JpaRepository, JpaSpecificationExecutor {/ / query course confirmation information @ Query based on course id (value = "SELECT ec.id,ec.title,ec.price,ec.lesson_num,"+" ecd.description, "+" es1.title AS oneSubject) "+" es2.title AS twoSubject "+" FROM edu_course ec LEFT JOIN edu_course_description ecd ON ec.id=ecd.id "+" LEFT JOIN edu_teacher et ON ec.teacher_id=et.id "+" LEFT JOIN edu_subject es1 ON ec.subject_parent_id=es1.id "+" LEFT JOIN edu_subject es2 ON ec.subject_id=es2.id "+" WHERE ec.id=?1 " NativeQuery = true) List
< Map>FindPublishInfoById (String id);}
Because other tables are involved, the return type uses List
< Map>Instead of using the previously defined vo class
At the end of each line, there must be a space between "and", otherwise an error will be reported.
Spring data jpa custom SQL statement encountered error Not supported for DML operations
Today, I encountered an error while customizing a Update statement, which shows that Not supported for DML operations means that DML operation is not supported.
My UserRepository is an inherited PagingAndSortingRepository interface. After reading the JPA documentation, I found that this interface does not support update transactions, so I need to add @ Modifying to the annotation.
The original text is as follows:
3.3.7. Modifying queries
All the sections above describe how to declare queries to access a given entity or collection of entities.Of course you can add custom modifying behaviour by using facilities described in Customimplementations for Spring Data repositories. As this approach is feasible for comprehensive customfunctionality, you can achieve the execution of modifying queries that actually only need parameterbinding by annotating the query method with @ Modifying:
Example 45. Declaring manipulating queries
@ Modifying
@ Query ("update User u set u.firstname =? 1 where u.lastname =? 2")
Int setFixedFirstnameFor (String firstname, String lastname)
This will trigger the query annotated to the method as updating query instead of a selecting one. As theEntityManager might contain outdated entities after the execution of the modifying query, we do notautomatically clear it (see JavaDoc of EntityManager.clear () for details) since this will effectively dropall non-flushed changes still pending in the EntityManager. If you wish the EntityManager to be clearedautomatically you can set @ Modifying annotation's clearAutomatically attribute to true.
Here is my source code:
@ Modifying@Query ("updateUser u set u. Upright sexuality 2, String u.upright sexuality, 3re u.u.birthday date =? 4, bookmark u.u.uyogminzu =? 5, u.u.uyogareaobixxxu.u.uyogname.com 7 where u.u_id =? 1") public void updateUserById (string u_sex, String u_date, String upright minzuol string u_area) String u_country) The above is all the contents of this article entitled "how to solve the problems encountered by SpringDataJpa in writing native sql". 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.