How to change method names from jpa-repository?
There is application on spring+jpa(repositories)
Entity:
@Entity
@Getter
@Setter
@NoArgsConstructor
public class User{
private int id;
private String name;
private String surname;
private int age;
}
So, my repository looks like this:
@Repository
public interface UserRepository extends CrudRepository<User, Integer>{
public List<User> findUsersByNameAndAge(String name, int age);
public List<User> findUsersByNameAndSurname(String name, String surname);
}
In my real prod code, there too much params, so this method-name is too long and not comfortable.
Is there a way to make from long method names comfortable options, like just find?
spring spring-data-jpa repository
add a comment |
There is application on spring+jpa(repositories)
Entity:
@Entity
@Getter
@Setter
@NoArgsConstructor
public class User{
private int id;
private String name;
private String surname;
private int age;
}
So, my repository looks like this:
@Repository
public interface UserRepository extends CrudRepository<User, Integer>{
public List<User> findUsersByNameAndAge(String name, int age);
public List<User> findUsersByNameAndSurname(String name, String surname);
}
In my real prod code, there too much params, so this method-name is too long and not comfortable.
Is there a way to make from long method names comfortable options, like just find?
spring spring-data-jpa repository
1
Use@Queryto define your own query and name the methods whatever you want: baeldung.com/spring-data-jpa-query
– Alan Hay
Nov 15 '18 at 8:48
Use a query of use theCriteriaAPI to generate your queries. The method names and translation to queries is only the simple use-cases.
– M. Deinum
Nov 15 '18 at 11:46
add a comment |
There is application on spring+jpa(repositories)
Entity:
@Entity
@Getter
@Setter
@NoArgsConstructor
public class User{
private int id;
private String name;
private String surname;
private int age;
}
So, my repository looks like this:
@Repository
public interface UserRepository extends CrudRepository<User, Integer>{
public List<User> findUsersByNameAndAge(String name, int age);
public List<User> findUsersByNameAndSurname(String name, String surname);
}
In my real prod code, there too much params, so this method-name is too long and not comfortable.
Is there a way to make from long method names comfortable options, like just find?
spring spring-data-jpa repository
There is application on spring+jpa(repositories)
Entity:
@Entity
@Getter
@Setter
@NoArgsConstructor
public class User{
private int id;
private String name;
private String surname;
private int age;
}
So, my repository looks like this:
@Repository
public interface UserRepository extends CrudRepository<User, Integer>{
public List<User> findUsersByNameAndAge(String name, int age);
public List<User> findUsersByNameAndSurname(String name, String surname);
}
In my real prod code, there too much params, so this method-name is too long and not comfortable.
Is there a way to make from long method names comfortable options, like just find?
spring spring-data-jpa repository
spring spring-data-jpa repository
edited Nov 15 '18 at 10:49
Billy Frost
1,800198
1,800198
asked Nov 15 '18 at 8:46
RobertoRoberto
179215
179215
1
Use@Queryto define your own query and name the methods whatever you want: baeldung.com/spring-data-jpa-query
– Alan Hay
Nov 15 '18 at 8:48
Use a query of use theCriteriaAPI to generate your queries. The method names and translation to queries is only the simple use-cases.
– M. Deinum
Nov 15 '18 at 11:46
add a comment |
1
Use@Queryto define your own query and name the methods whatever you want: baeldung.com/spring-data-jpa-query
– Alan Hay
Nov 15 '18 at 8:48
Use a query of use theCriteriaAPI to generate your queries. The method names and translation to queries is only the simple use-cases.
– M. Deinum
Nov 15 '18 at 11:46
1
1
Use
@Query to define your own query and name the methods whatever you want: baeldung.com/spring-data-jpa-query– Alan Hay
Nov 15 '18 at 8:48
Use
@Query to define your own query and name the methods whatever you want: baeldung.com/spring-data-jpa-query– Alan Hay
Nov 15 '18 at 8:48
Use a query of use the
Criteria API to generate your queries. The method names and translation to queries is only the simple use-cases.– M. Deinum
Nov 15 '18 at 11:46
Use a query of use the
Criteria API to generate your queries. The method names and translation to queries is only the simple use-cases.– M. Deinum
Nov 15 '18 at 11:46
add a comment |
1 Answer
1
active
oldest
votes
Use default Java 8 feature for wrapping :
for example :
interface UserRepository extends JpaRepository<User, Long> {
// don't use that crazy long method! use getByEmail instead
User findFirstByEmailContainsIgnoreCaseAndField1NotNullAndField2NotNullAndField3NotNullAndField4NotNullAndField5NotNullAndField6NotNull(final String email);
default User getByEmail(final String email) {
return findFirstByEmailContainsIgnoreCaseAndField1NotNullAndField2NotNullAndField3NotNullAndField4NotNullAndField5NotNullAndField6NotNull(email);
}
}
You can find complete reference here : https://github.com/daggerok/spring-data-examples/blob/master/shadov/src/main/java/daggerok/ShadovApplication.java
Or else You can specify any name for a method and add an annotation @Query with parameter value which holds desired query to database like this:
@Query(value="select u from User u where u.deleted=false and u.email=:email")
User findOneByEmail(@Param("email")String email);
or, with native sql query:
@Query(value="SELECT * FROM users WHERE deleted=false AND email=?1", nativeQuery=true)
User findOneByEmail(String email);
You can also use names that follow the naming convention for queries since @Query annotation will take precedence over query from method name.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315452%2fhow-to-change-method-names-from-jpa-repository%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use default Java 8 feature for wrapping :
for example :
interface UserRepository extends JpaRepository<User, Long> {
// don't use that crazy long method! use getByEmail instead
User findFirstByEmailContainsIgnoreCaseAndField1NotNullAndField2NotNullAndField3NotNullAndField4NotNullAndField5NotNullAndField6NotNull(final String email);
default User getByEmail(final String email) {
return findFirstByEmailContainsIgnoreCaseAndField1NotNullAndField2NotNullAndField3NotNullAndField4NotNullAndField5NotNullAndField6NotNull(email);
}
}
You can find complete reference here : https://github.com/daggerok/spring-data-examples/blob/master/shadov/src/main/java/daggerok/ShadovApplication.java
Or else You can specify any name for a method and add an annotation @Query with parameter value which holds desired query to database like this:
@Query(value="select u from User u where u.deleted=false and u.email=:email")
User findOneByEmail(@Param("email")String email);
or, with native sql query:
@Query(value="SELECT * FROM users WHERE deleted=false AND email=?1", nativeQuery=true)
User findOneByEmail(String email);
You can also use names that follow the naming convention for queries since @Query annotation will take precedence over query from method name.
add a comment |
Use default Java 8 feature for wrapping :
for example :
interface UserRepository extends JpaRepository<User, Long> {
// don't use that crazy long method! use getByEmail instead
User findFirstByEmailContainsIgnoreCaseAndField1NotNullAndField2NotNullAndField3NotNullAndField4NotNullAndField5NotNullAndField6NotNull(final String email);
default User getByEmail(final String email) {
return findFirstByEmailContainsIgnoreCaseAndField1NotNullAndField2NotNullAndField3NotNullAndField4NotNullAndField5NotNullAndField6NotNull(email);
}
}
You can find complete reference here : https://github.com/daggerok/spring-data-examples/blob/master/shadov/src/main/java/daggerok/ShadovApplication.java
Or else You can specify any name for a method and add an annotation @Query with parameter value which holds desired query to database like this:
@Query(value="select u from User u where u.deleted=false and u.email=:email")
User findOneByEmail(@Param("email")String email);
or, with native sql query:
@Query(value="SELECT * FROM users WHERE deleted=false AND email=?1", nativeQuery=true)
User findOneByEmail(String email);
You can also use names that follow the naming convention for queries since @Query annotation will take precedence over query from method name.
add a comment |
Use default Java 8 feature for wrapping :
for example :
interface UserRepository extends JpaRepository<User, Long> {
// don't use that crazy long method! use getByEmail instead
User findFirstByEmailContainsIgnoreCaseAndField1NotNullAndField2NotNullAndField3NotNullAndField4NotNullAndField5NotNullAndField6NotNull(final String email);
default User getByEmail(final String email) {
return findFirstByEmailContainsIgnoreCaseAndField1NotNullAndField2NotNullAndField3NotNullAndField4NotNullAndField5NotNullAndField6NotNull(email);
}
}
You can find complete reference here : https://github.com/daggerok/spring-data-examples/blob/master/shadov/src/main/java/daggerok/ShadovApplication.java
Or else You can specify any name for a method and add an annotation @Query with parameter value which holds desired query to database like this:
@Query(value="select u from User u where u.deleted=false and u.email=:email")
User findOneByEmail(@Param("email")String email);
or, with native sql query:
@Query(value="SELECT * FROM users WHERE deleted=false AND email=?1", nativeQuery=true)
User findOneByEmail(String email);
You can also use names that follow the naming convention for queries since @Query annotation will take precedence over query from method name.
Use default Java 8 feature for wrapping :
for example :
interface UserRepository extends JpaRepository<User, Long> {
// don't use that crazy long method! use getByEmail instead
User findFirstByEmailContainsIgnoreCaseAndField1NotNullAndField2NotNullAndField3NotNullAndField4NotNullAndField5NotNullAndField6NotNull(final String email);
default User getByEmail(final String email) {
return findFirstByEmailContainsIgnoreCaseAndField1NotNullAndField2NotNullAndField3NotNullAndField4NotNullAndField5NotNullAndField6NotNull(email);
}
}
You can find complete reference here : https://github.com/daggerok/spring-data-examples/blob/master/shadov/src/main/java/daggerok/ShadovApplication.java
Or else You can specify any name for a method and add an annotation @Query with parameter value which holds desired query to database like this:
@Query(value="select u from User u where u.deleted=false and u.email=:email")
User findOneByEmail(@Param("email")String email);
or, with native sql query:
@Query(value="SELECT * FROM users WHERE deleted=false AND email=?1", nativeQuery=true)
User findOneByEmail(String email);
You can also use names that follow the naming convention for queries since @Query annotation will take precedence over query from method name.
answered Nov 15 '18 at 8:58
Samyak JainSamyak Jain
5713
5713
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315452%2fhow-to-change-method-names-from-jpa-repository%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Use
@Queryto define your own query and name the methods whatever you want: baeldung.com/spring-data-jpa-query– Alan Hay
Nov 15 '18 at 8:48
Use a query of use the
CriteriaAPI to generate your queries. The method names and translation to queries is only the simple use-cases.– M. Deinum
Nov 15 '18 at 11:46