How to return a select query from jpa repository with foreign key type
I am trying to do this:
@Query(value = "SELECT DISTINCT c.* FROM comarca c INNER JOIN debito_negativacao d ON d.comarca_id = c.id WHERE d.status = :status", nativeQuery = true)
List<Comarca> findDistinctComarcaByStatus(@Param("status") String status);
But I get this error:
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object] to type [com.hc.projects.model.Comarca] for value '{9, 0, 7323}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.math.BigInteger] to type [com.hc.projects.model.Comarca]
java spring hibernate spring-data-jpa
add a comment |
I am trying to do this:
@Query(value = "SELECT DISTINCT c.* FROM comarca c INNER JOIN debito_negativacao d ON d.comarca_id = c.id WHERE d.status = :status", nativeQuery = true)
List<Comarca> findDistinctComarcaByStatus(@Param("status") String status);
But I get this error:
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object] to type [com.hc.projects.model.Comarca] for value '{9, 0, 7323}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.math.BigInteger] to type [com.hc.projects.model.Comarca]
java spring hibernate spring-data-jpa
add a comment |
I am trying to do this:
@Query(value = "SELECT DISTINCT c.* FROM comarca c INNER JOIN debito_negativacao d ON d.comarca_id = c.id WHERE d.status = :status", nativeQuery = true)
List<Comarca> findDistinctComarcaByStatus(@Param("status") String status);
But I get this error:
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object] to type [com.hc.projects.model.Comarca] for value '{9, 0, 7323}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.math.BigInteger] to type [com.hc.projects.model.Comarca]
java spring hibernate spring-data-jpa
I am trying to do this:
@Query(value = "SELECT DISTINCT c.* FROM comarca c INNER JOIN debito_negativacao d ON d.comarca_id = c.id WHERE d.status = :status", nativeQuery = true)
List<Comarca> findDistinctComarcaByStatus(@Param("status") String status);
But I get this error:
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object] to type [com.hc.projects.model.Comarca] for value '{9, 0, 7323}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.math.BigInteger] to type [com.hc.projects.model.Comarca]
java spring hibernate spring-data-jpa
java spring hibernate spring-data-jpa
edited Jan 18 at 18:54
Billy Frost
1,74598
1,74598
asked Jan 18 at 14:18
Kaique DiasKaique Dias
126
126
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
If, in a second time, you want to isolate a list of comarca_id, try to stream your request result.
List<Comarca> comarca = debitoNegativacao.stream().map(dn -> dn.getComarca()).distinct().collect(Collectors.toList());
++
add a comment |
You have to return all columns that are necessary to construct a Comarca. So you will have to join the table.
As didn't provide the tables I can only guess:
@Query(value = "SELECT DISTINCT * FROM comarca c " +
"JOIN debito_negativacao d ON d.comarca_id = c.id "+
"WHERE d.debito_negativacao.status= :status", nativeQuery = true)
List<Comarca> findDistinctComarcaIdByStatus(@Param("status") String status);
ok, i'll try to do this!
– Kaique Dias
Jan 18 at 14:25
i did as you say, but the error persists
– Kaique Dias
Jan 18 at 15:16
add a comment |
Your request tells that you want a list of BigInteger : SELECT DISTINCT comarca_id...
because comarca_id is a biginteger I guess.
If you want a Comarca list, you have to request on all your table.
i did as you say, but the error persists. Question att
– Kaique Dias
Jan 18 at 15:17
add a comment |
If you want to use a distinct query that will return other columns than the distinct one you need some kind of strategy how to "merge" the objects. Imagine rows like this:
------------------------------
| comarca_id| key | status |
| 1 | A | your_state|
| 1 | B | your_state|
| 2 | C | your_state|
------------------------------
What would you get in this case?
SELECT DISTINCT comarca_id FROM comarca;
will return 1,2
However, how can you merge two (or more) entries which have the same comarca_id
and status
?
This leaves you with three cases:
- you assume
comarca_id
+status
is unique -> you don't need theDISTINCT
query - There might be more than one row with same
comarca_id
andstatus
-> you can't make the query distinct - you only want the distinct
comarca_id
values -> make your method returnList<BigInteger>
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%2f54255858%2fhow-to-return-a-select-query-from-jpa-repository-with-foreign-key-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
If, in a second time, you want to isolate a list of comarca_id, try to stream your request result.
List<Comarca> comarca = debitoNegativacao.stream().map(dn -> dn.getComarca()).distinct().collect(Collectors.toList());
++
add a comment |
If, in a second time, you want to isolate a list of comarca_id, try to stream your request result.
List<Comarca> comarca = debitoNegativacao.stream().map(dn -> dn.getComarca()).distinct().collect(Collectors.toList());
++
add a comment |
If, in a second time, you want to isolate a list of comarca_id, try to stream your request result.
List<Comarca> comarca = debitoNegativacao.stream().map(dn -> dn.getComarca()).distinct().collect(Collectors.toList());
++
If, in a second time, you want to isolate a list of comarca_id, try to stream your request result.
List<Comarca> comarca = debitoNegativacao.stream().map(dn -> dn.getComarca()).distinct().collect(Collectors.toList());
++
edited Jan 18 at 22:43
Kaique Dias
126
126
answered Jan 18 at 14:34
LovegiverLovegiver
12711
12711
add a comment |
add a comment |
You have to return all columns that are necessary to construct a Comarca. So you will have to join the table.
As didn't provide the tables I can only guess:
@Query(value = "SELECT DISTINCT * FROM comarca c " +
"JOIN debito_negativacao d ON d.comarca_id = c.id "+
"WHERE d.debito_negativacao.status= :status", nativeQuery = true)
List<Comarca> findDistinctComarcaIdByStatus(@Param("status") String status);
ok, i'll try to do this!
– Kaique Dias
Jan 18 at 14:25
i did as you say, but the error persists
– Kaique Dias
Jan 18 at 15:16
add a comment |
You have to return all columns that are necessary to construct a Comarca. So you will have to join the table.
As didn't provide the tables I can only guess:
@Query(value = "SELECT DISTINCT * FROM comarca c " +
"JOIN debito_negativacao d ON d.comarca_id = c.id "+
"WHERE d.debito_negativacao.status= :status", nativeQuery = true)
List<Comarca> findDistinctComarcaIdByStatus(@Param("status") String status);
ok, i'll try to do this!
– Kaique Dias
Jan 18 at 14:25
i did as you say, but the error persists
– Kaique Dias
Jan 18 at 15:16
add a comment |
You have to return all columns that are necessary to construct a Comarca. So you will have to join the table.
As didn't provide the tables I can only guess:
@Query(value = "SELECT DISTINCT * FROM comarca c " +
"JOIN debito_negativacao d ON d.comarca_id = c.id "+
"WHERE d.debito_negativacao.status= :status", nativeQuery = true)
List<Comarca> findDistinctComarcaIdByStatus(@Param("status") String status);
You have to return all columns that are necessary to construct a Comarca. So you will have to join the table.
As didn't provide the tables I can only guess:
@Query(value = "SELECT DISTINCT * FROM comarca c " +
"JOIN debito_negativacao d ON d.comarca_id = c.id "+
"WHERE d.debito_negativacao.status= :status", nativeQuery = true)
List<Comarca> findDistinctComarcaIdByStatus(@Param("status") String status);
answered Jan 18 at 14:24
Simon MartinelliSimon Martinelli
5,87111230
5,87111230
ok, i'll try to do this!
– Kaique Dias
Jan 18 at 14:25
i did as you say, but the error persists
– Kaique Dias
Jan 18 at 15:16
add a comment |
ok, i'll try to do this!
– Kaique Dias
Jan 18 at 14:25
i did as you say, but the error persists
– Kaique Dias
Jan 18 at 15:16
ok, i'll try to do this!
– Kaique Dias
Jan 18 at 14:25
ok, i'll try to do this!
– Kaique Dias
Jan 18 at 14:25
i did as you say, but the error persists
– Kaique Dias
Jan 18 at 15:16
i did as you say, but the error persists
– Kaique Dias
Jan 18 at 15:16
add a comment |
Your request tells that you want a list of BigInteger : SELECT DISTINCT comarca_id...
because comarca_id is a biginteger I guess.
If you want a Comarca list, you have to request on all your table.
i did as you say, but the error persists. Question att
– Kaique Dias
Jan 18 at 15:17
add a comment |
Your request tells that you want a list of BigInteger : SELECT DISTINCT comarca_id...
because comarca_id is a biginteger I guess.
If you want a Comarca list, you have to request on all your table.
i did as you say, but the error persists. Question att
– Kaique Dias
Jan 18 at 15:17
add a comment |
Your request tells that you want a list of BigInteger : SELECT DISTINCT comarca_id...
because comarca_id is a biginteger I guess.
If you want a Comarca list, you have to request on all your table.
Your request tells that you want a list of BigInteger : SELECT DISTINCT comarca_id...
because comarca_id is a biginteger I guess.
If you want a Comarca list, you have to request on all your table.
answered Jan 18 at 14:31
LovegiverLovegiver
12711
12711
i did as you say, but the error persists. Question att
– Kaique Dias
Jan 18 at 15:17
add a comment |
i did as you say, but the error persists. Question att
– Kaique Dias
Jan 18 at 15:17
i did as you say, but the error persists. Question att
– Kaique Dias
Jan 18 at 15:17
i did as you say, but the error persists. Question att
– Kaique Dias
Jan 18 at 15:17
add a comment |
If you want to use a distinct query that will return other columns than the distinct one you need some kind of strategy how to "merge" the objects. Imagine rows like this:
------------------------------
| comarca_id| key | status |
| 1 | A | your_state|
| 1 | B | your_state|
| 2 | C | your_state|
------------------------------
What would you get in this case?
SELECT DISTINCT comarca_id FROM comarca;
will return 1,2
However, how can you merge two (or more) entries which have the same comarca_id
and status
?
This leaves you with three cases:
- you assume
comarca_id
+status
is unique -> you don't need theDISTINCT
query - There might be more than one row with same
comarca_id
andstatus
-> you can't make the query distinct - you only want the distinct
comarca_id
values -> make your method returnList<BigInteger>
add a comment |
If you want to use a distinct query that will return other columns than the distinct one you need some kind of strategy how to "merge" the objects. Imagine rows like this:
------------------------------
| comarca_id| key | status |
| 1 | A | your_state|
| 1 | B | your_state|
| 2 | C | your_state|
------------------------------
What would you get in this case?
SELECT DISTINCT comarca_id FROM comarca;
will return 1,2
However, how can you merge two (or more) entries which have the same comarca_id
and status
?
This leaves you with three cases:
- you assume
comarca_id
+status
is unique -> you don't need theDISTINCT
query - There might be more than one row with same
comarca_id
andstatus
-> you can't make the query distinct - you only want the distinct
comarca_id
values -> make your method returnList<BigInteger>
add a comment |
If you want to use a distinct query that will return other columns than the distinct one you need some kind of strategy how to "merge" the objects. Imagine rows like this:
------------------------------
| comarca_id| key | status |
| 1 | A | your_state|
| 1 | B | your_state|
| 2 | C | your_state|
------------------------------
What would you get in this case?
SELECT DISTINCT comarca_id FROM comarca;
will return 1,2
However, how can you merge two (or more) entries which have the same comarca_id
and status
?
This leaves you with three cases:
- you assume
comarca_id
+status
is unique -> you don't need theDISTINCT
query - There might be more than one row with same
comarca_id
andstatus
-> you can't make the query distinct - you only want the distinct
comarca_id
values -> make your method returnList<BigInteger>
If you want to use a distinct query that will return other columns than the distinct one you need some kind of strategy how to "merge" the objects. Imagine rows like this:
------------------------------
| comarca_id| key | status |
| 1 | A | your_state|
| 1 | B | your_state|
| 2 | C | your_state|
------------------------------
What would you get in this case?
SELECT DISTINCT comarca_id FROM comarca;
will return 1,2
However, how can you merge two (or more) entries which have the same comarca_id
and status
?
This leaves you with three cases:
- you assume
comarca_id
+status
is unique -> you don't need theDISTINCT
query - There might be more than one row with same
comarca_id
andstatus
-> you can't make the query distinct - you only want the distinct
comarca_id
values -> make your method returnList<BigInteger>
answered Jan 18 at 14:31
Abaddon666Abaddon666
782722
782722
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%2f54255858%2fhow-to-return-a-select-query-from-jpa-repository-with-foreign-key-type%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