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--
This article introduces the inheritance of jpa Repository to write custom method query example analysis, the content is very detailed, interested partners can refer to, I hope to help you.
Inheriting jpa Repository and writing custom method queries
Today, when writing jpa query, I encountered adding custom methods. The reason for the project startup error is summarized as follows:
First define entity class @Entity@Table(name = "user")Class User{ @Id @GeneratedValue int id; @Column String age; @Column String school; @Column String userName; set, get method (omitted)}public interface UserRepository extends JpaRepository { List findByUsernameLike(String username); List aaa();}
When starting a project,
The item error prompt message is
org.springframework.data.mapping.PropertyReferenceException: No property aaa found for type com.fpi.safety.common.entity.po.User
After removing the List aaa(); method, the project can start running normally again.
What's the reason?
After searching, it turns out to be inherited jpa, and some rules must be satisfied. The rules are as follows
Spring Data JPA framework in the method name resolution, will first be the method name of the extra prefix truncated, such as find, findBy, read, readBy, get, getBy, and then the rest of the part of the resolution.
If you create a query like findByUserName (), the framework first strips out findBy when parsing the method, and then parses the remaining attributes, assuming that the query entity is User
1: First judge whether userName (the initial letter is changed to lowercase according to POJO specification) is an attribute of the query entity. If yes, it means that the query is performed according to this attribute; if there is no such attribute, continue to the second step;
2: Intercept the character string starting with the first capital letter from right to left, and then check whether the remaining character string is an attribute of the query entity. If yes, it means that the query is based on this attribute; if there is no such attribute, repeat the second step and continue to intercept from right to left; finally assume that the user is an attribute of the query entity;
3: Next, process the rest (UserName), first determine whether the type corresponding to the user has userName attribute, if yes, it means that the method finally queries according to the value of "User.userName." Otherwise, continue to intercept from right to left according to the rules in step 2, and finally means that the query is performed according to the value of "User.userName."
4: There may be a special case, for example, User contains an attribute of, there is also a userNameChange attribute, in this case there will be mixed. You can explicitly add "_" between attributes to express meaning, such as "findByUser_NameChange)" or "findByUserName_Change ()"
From the above, we can know that jap in the parsing is, aaa in the user class is no attribute, so the error No property aaa found.
If we want to use the jap framework without adding another custom class, we must follow its naming convention
If you can't remember the rules of jpa, it doesn't matter. You can write another class to implement custom query methods.
As follows:
1. Customize an interface that declares your own additional queries.
public interface UseerRepositoryTwo { public List searchUser(String name, int id);}
2. Create an interface that extends JpaRepository or CurdRepository, and the interface UserRepository defined above
public interface UserRepositoryTwoService extends CrudRepository, CustomizedLogRepository {}
3. Implementing UserRepositoryTwoService
Note that the class name here must be declared with the name UserRepositoryTwoService of the interface created in 2, followed by Impl, rather than UserRepositoryTwoImpl
public class UserRepositoryTwoServiceImpl implements UserRepositoryTwoService { @Autowired @PersistenceContext private EntityManager entityManager; @Override public List searchLogs(int Id, String name) { ...... }}
Write your own custom implementation.
JpaRepository naming conventions KeywordSampleJPQLEndfindByLastnameAndFirstnamewhere x.lastname=? 1 and x.firstname=? 2OrfindByLastnameOrFirstnamewhere x.lastname=? 1 or x.firstname=? 2BetweenfindByStartDateBetweenwhere x.startDate between ? 1 and ? 2LessThanfindByAgeLessThanwhere x.startDate
< ?1GreaterThanfindByAgeGreaterThanwhere x.startDate >? 1AfterfindByStartDateAfterwhere x.startDate >n ? 1BeforefindByStartDateBeforewhere x.startDate < ? 1IsNullfindByAgeIsNullwhere x.age is nullIsNotNull,NotNullfindByAge(Is)NotNullwhere x.age not nullLikefindByFirstnameLikewhere x.firstname like ? 1notLikefindByFirstnameNotLikewhere x.firstname not like ? 1StartingWithfindByFirstnameStartingWithXXXwhere x.firstname like ? 1(parameter bound with appended %)EndingWithfindByFirstnameEndingWithXXXwhere x.firstname like ? 1(parameter bound with appended %)ContainingfindByFirstnameContainingwhere x.firstname like ? 1(parameter bound wrapped in %)OrderByfindByAgeOrderByLastnamewhere x.age = ? 1 order by x.lastname descNotfindByLastnameNotwhere x.lastname ? 1NotInfindByAgeNotIn(Collection age )where x.age not in ? 1TruefindByActiveTrue()where x.active = trueFalsefindByActiveFalse()
where x.active = false
Example:
@RepositoryDefinition(domainClass = Employee.class, idClass = Integer.class)public interface EmployeeRepository { //extends Repository{ public Employee findByName(String name); // where name like ?% and age
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.