Get all the records of column in Spring Data JPA, even we specify it as parameter
There is a table called zipcode with columns as id, zip, city, state.
In our case, we want to get the records based on either id or zip or city or state.
The General SQL query looks like
SELECT * FROM zipcode WHERE id=id AND zip=zip AND city=city AND state=state;
If we want to get a specific state then, we will replace the state value with the given name.
SELECT * FROM zipcode WHERE id=id AND zip=zip AND city=city AND state='California';
If we want a specific city, then we can write
SELECT * FROM zipcode WHERE id=id AND zip=zip AND city='Los Angles' AND state=state;
The problem with Spring Data JPA is, if we write a method
@Query(value = "SELECT * FROM zipcode WHERE id=?1 AND zip=?2 AND city=?3 AND state=?4", nativeQuery = true)
List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)
I want to get all the cities but a specific state only, then I can't use this method, like how I can use the above SQL query, where we just need to replace the column value.
How can we achieve this in Spring Data JPA, using native query ?
java mysql database spring spring-data-jpa
add a comment |
There is a table called zipcode with columns as id, zip, city, state.
In our case, we want to get the records based on either id or zip or city or state.
The General SQL query looks like
SELECT * FROM zipcode WHERE id=id AND zip=zip AND city=city AND state=state;
If we want to get a specific state then, we will replace the state value with the given name.
SELECT * FROM zipcode WHERE id=id AND zip=zip AND city=city AND state='California';
If we want a specific city, then we can write
SELECT * FROM zipcode WHERE id=id AND zip=zip AND city='Los Angles' AND state=state;
The problem with Spring Data JPA is, if we write a method
@Query(value = "SELECT * FROM zipcode WHERE id=?1 AND zip=?2 AND city=?3 AND state=?4", nativeQuery = true)
List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)
I want to get all the cities but a specific state only, then I can't use this method, like how I can use the above SQL query, where we just need to replace the column value.
How can we achieve this in Spring Data JPA, using native query ?
java mysql database spring spring-data-jpa
add a comment |
There is a table called zipcode with columns as id, zip, city, state.
In our case, we want to get the records based on either id or zip or city or state.
The General SQL query looks like
SELECT * FROM zipcode WHERE id=id AND zip=zip AND city=city AND state=state;
If we want to get a specific state then, we will replace the state value with the given name.
SELECT * FROM zipcode WHERE id=id AND zip=zip AND city=city AND state='California';
If we want a specific city, then we can write
SELECT * FROM zipcode WHERE id=id AND zip=zip AND city='Los Angles' AND state=state;
The problem with Spring Data JPA is, if we write a method
@Query(value = "SELECT * FROM zipcode WHERE id=?1 AND zip=?2 AND city=?3 AND state=?4", nativeQuery = true)
List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)
I want to get all the cities but a specific state only, then I can't use this method, like how I can use the above SQL query, where we just need to replace the column value.
How can we achieve this in Spring Data JPA, using native query ?
java mysql database spring spring-data-jpa
There is a table called zipcode with columns as id, zip, city, state.
In our case, we want to get the records based on either id or zip or city or state.
The General SQL query looks like
SELECT * FROM zipcode WHERE id=id AND zip=zip AND city=city AND state=state;
If we want to get a specific state then, we will replace the state value with the given name.
SELECT * FROM zipcode WHERE id=id AND zip=zip AND city=city AND state='California';
If we want a specific city, then we can write
SELECT * FROM zipcode WHERE id=id AND zip=zip AND city='Los Angles' AND state=state;
The problem with Spring Data JPA is, if we write a method
@Query(value = "SELECT * FROM zipcode WHERE id=?1 AND zip=?2 AND city=?3 AND state=?4", nativeQuery = true)
List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)
I want to get all the cities but a specific state only, then I can't use this method, like how I can use the above SQL query, where we just need to replace the column value.
How can we achieve this in Spring Data JPA, using native query ?
java mysql database spring spring-data-jpa
java mysql database spring spring-data-jpa
asked yesterday
KC7
8611
8611
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I don't know if Spring Data has a way of doing this in a single method, but you could try using an Example:
public List<Zipcode> getZipcodes(Integer id, Integer zip, String city, String state){
Zipcode zipcode = new Zipcode();
if (id != null)
zipcode.setId(id);
if (zip != null)
zipcode.setZip(zip);
...
return zipcodeRepository.findAll(Example.of(zipcode));
}
New contributor
gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
Try this,
@Query(value = "SELECT * FROM zipcode WHERE (id=?1 or ?1 is null) AND (zip=?2 or ?2 is null) AND (city=?3 or ?3 is null) AND (state=?4 or ?4 is null) ", nativeQuery = true)
List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)
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%2f53944169%2fget-all-the-records-of-column-in-spring-data-jpa-even-we-specify-it-as-paramete%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't know if Spring Data has a way of doing this in a single method, but you could try using an Example:
public List<Zipcode> getZipcodes(Integer id, Integer zip, String city, String state){
Zipcode zipcode = new Zipcode();
if (id != null)
zipcode.setId(id);
if (zip != null)
zipcode.setZip(zip);
...
return zipcodeRepository.findAll(Example.of(zipcode));
}
New contributor
gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I don't know if Spring Data has a way of doing this in a single method, but you could try using an Example:
public List<Zipcode> getZipcodes(Integer id, Integer zip, String city, String state){
Zipcode zipcode = new Zipcode();
if (id != null)
zipcode.setId(id);
if (zip != null)
zipcode.setZip(zip);
...
return zipcodeRepository.findAll(Example.of(zipcode));
}
New contributor
gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I don't know if Spring Data has a way of doing this in a single method, but you could try using an Example:
public List<Zipcode> getZipcodes(Integer id, Integer zip, String city, String state){
Zipcode zipcode = new Zipcode();
if (id != null)
zipcode.setId(id);
if (zip != null)
zipcode.setZip(zip);
...
return zipcodeRepository.findAll(Example.of(zipcode));
}
New contributor
gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I don't know if Spring Data has a way of doing this in a single method, but you could try using an Example:
public List<Zipcode> getZipcodes(Integer id, Integer zip, String city, String state){
Zipcode zipcode = new Zipcode();
if (id != null)
zipcode.setId(id);
if (zip != null)
zipcode.setZip(zip);
...
return zipcodeRepository.findAll(Example.of(zipcode));
}
New contributor
gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered yesterday
gbandres
3939
3939
New contributor
gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
Try this,
@Query(value = "SELECT * FROM zipcode WHERE (id=?1 or ?1 is null) AND (zip=?2 or ?2 is null) AND (city=?3 or ?3 is null) AND (state=?4 or ?4 is null) ", nativeQuery = true)
List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)
add a comment |
Try this,
@Query(value = "SELECT * FROM zipcode WHERE (id=?1 or ?1 is null) AND (zip=?2 or ?2 is null) AND (city=?3 or ?3 is null) AND (state=?4 or ?4 is null) ", nativeQuery = true)
List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)
add a comment |
Try this,
@Query(value = "SELECT * FROM zipcode WHERE (id=?1 or ?1 is null) AND (zip=?2 or ?2 is null) AND (city=?3 or ?3 is null) AND (state=?4 or ?4 is null) ", nativeQuery = true)
List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)
Try this,
@Query(value = "SELECT * FROM zipcode WHERE (id=?1 or ?1 is null) AND (zip=?2 or ?2 is null) AND (city=?3 or ?3 is null) AND (state=?4 or ?4 is null) ", nativeQuery = true)
List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)
answered yesterday
Himesh goswami
653315
653315
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53944169%2fget-all-the-records-of-column-in-spring-data-jpa-even-we-specify-it-as-paramete%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