In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Springboot JPA how to use distinct to return objects", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Springboot JPA how to use distinct to return objects" bar!
How JPA uses distinct to return the object package com.frank.jpaBatchSave.repository;import com.frank.jpaBatchSave.entity.Person;import org.springframework.data.jpa.repository.Query;import org.springframework.data.repository.PagingAndSortingRepository;import org.springframework.stereotype.Repository;import java.util.List; @ Repositorypublic interface PersonRepository extends PagingAndSortingRepository {@ Query ("select distinct new Person (p.name, p.province, p.city) from Person p") List getAllPerson ();}
If you need to return an object here, you need new Person (p.name, p.province, p.city), and the Person class must have a three-parameter constructor.
Public Person (String name, String province, String city) {this.name = name; this.province = province; this.city = city;}
Because: select distinct name, province, city) from Person p returns the Object [] array if you write it this way. So you need to use the above method.
JPA Custom return object
Any ORM framework can not do without the problem of opening up custom sql. Jpa is no exception, and many scenes need to write complex sql.
First define a method signature, then annotate it with @ Query. Like this, you need to pay attention to nativeQuery, which means that the characters in query are executed in the original sql statement, that is, without any adjustments. Execute whatever sql statement you write. But want to return to the custom entity, Sorry, can not do so. In another way.
@ Query (value = "SELECT * from table where id=: id", nativeQuery = true) List getByaaId (@ Param ("id") BigInteger id); method 1
The following is the entity method that implements the custom. First, you need to define an entity that must contain no-parameter and all-parameter constructors. Then remove the nativeQuery, and it is important to note that the statement written is not the corresponding table name in the database, but the entity mapping you defined. For example, the following table1 and table2 are actually the names of the entity classes you define, not your database table names, but the entity class names that map the data tables in your code.
@ Data@NoArgsConstructor@AllArgsConstructorpublic class aaDto {private String aa; private String bb; private String cc;} @ Query (value = "SELECT new com.xxx.xxx.dto.aaDto (I.aaMagneI. BBMI.cc) FROM table1 I JOIN table2 e on i.id=e.id") List getList (); method 2
Method 1 this way of writing is still very inflexible, sometimes write some functions and so on, it is estimated that it will kill, or you have to write the original sql. The query field I'm talking about belongs to multiple tables. If you query only one table field, the associated query is only a condition, and it is no problem that you can return the corresponding entity directly.
List@Query (value = "SELECT a.aajie b.bb from a LEFT JOIN b a.id=b.id", nativeQuery = true) List getFundAccountList ()
The data returned in this way is a collection of key values. If we really want to use an object without Map, I will first serialize the return parameter to json, and then deserialize the json into the object we want.
Method three
When inheriting the jpa interface, the entity class in it writes your corresponding result class, which can be received with the corresponding result.
Public interface xxxDTORepository extends JpaRepository method 4
The above methods are all based on inheriting the JpaRepository interface, but there is another more flexible way to write sql and define return objects.
@ PersistenceContextprivate EntityManager entityManager
Log an error: Path expected for join!
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join!
Such a mistake is actually very simple, but I did not take a closer look at the error message, resulting in the correct statement to find the error, can not find. When this error occurs, it will follow the hql statement you wrote. Pay attention to the hql statement and check it carefully. There must be something wrong with it.
There is a blog that needs to add a mapping relationship, in fact, it is not needed at all, it is one-to-many, class A contains List is not necessary, independent classes without any association is OK, careful.
By the way, let's record another problem. The problem is simple. The following code appears, followed by some information. According to the information, you can know whether it is the constructor type of the returned object. In this case, you can adjust the attribute type of the returned entity object.
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class
Thank you for your reading, the above is the content of "how Springboot JPA uses distinct to return objects". After the study of this article, I believe you have a deeper understanding of how Springboot JPA uses distinct to return objects, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.