Hibernate HQL: select records that match two values at the same time
I have a table in a DB that I cannot modify, and I have a list of pairs of values that I have to use in order to select the records from that table that exactly match both the values in the list.
For example my list of pairs consists of ('A1', 'A2'), ('B1', 'B2'), ('C1', 'C2'), and my table consists of the following data
--------------------------
| ID | COLUMN1 | COLUMN2 |
--------------------------
| 1 | 'A1' | 'A2' |
| 2 | 'B1' | 'B2' |
| 3 | 'A1' | 'B2' |
| 4 | 'B1' | 'A2' |
| 5 | 'C1' | 'C2' |
--------------------------
The correct result set should be
1, 'A1', 'A2'
2, 'B1', 'B2'
5, 'C1', 'C2'
which means that records like
3, 'A1', 'B2'
4, 'B1', 'A2'
should not be in it.
How do I achieve such a result using HQL?
Performance is an obstacle in my situation, this is why a solution implemented via concatenation of values is not viable for me.
EDIT:
if this can help, the Java entity is very simple and it looks like this:
package org.example;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "MY_TABLE")
public class ExampleEntity implements Serializable
@Id
@Column(name = "ID", unique = true)
private int id;
@Column(name = "COLUMN1")
private String column1;
@Column(name = "COLUMN2")
private String column2;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getColumn1()
return column1;
public void setColumn1(String column1)
this.column1 = column1;
public String getColumn2()
return column2;
public void setColumn2(String column2)
this.column2 = column2;
hibernate hql
|
show 7 more comments
I have a table in a DB that I cannot modify, and I have a list of pairs of values that I have to use in order to select the records from that table that exactly match both the values in the list.
For example my list of pairs consists of ('A1', 'A2'), ('B1', 'B2'), ('C1', 'C2'), and my table consists of the following data
--------------------------
| ID | COLUMN1 | COLUMN2 |
--------------------------
| 1 | 'A1' | 'A2' |
| 2 | 'B1' | 'B2' |
| 3 | 'A1' | 'B2' |
| 4 | 'B1' | 'A2' |
| 5 | 'C1' | 'C2' |
--------------------------
The correct result set should be
1, 'A1', 'A2'
2, 'B1', 'B2'
5, 'C1', 'C2'
which means that records like
3, 'A1', 'B2'
4, 'B1', 'A2'
should not be in it.
How do I achieve such a result using HQL?
Performance is an obstacle in my situation, this is why a solution implemented via concatenation of values is not viable for me.
EDIT:
if this can help, the Java entity is very simple and it looks like this:
package org.example;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "MY_TABLE")
public class ExampleEntity implements Serializable
@Id
@Column(name = "ID", unique = true)
private int id;
@Column(name = "COLUMN1")
private String column1;
@Column(name = "COLUMN2")
private String column2;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getColumn1()
return column1;
public void setColumn1(String column1)
this.column1 = column1;
public String getColumn2()
return column2;
public void setColumn2(String column2)
this.column2 = column2;
hibernate hql
What is the logic by which you chose'A1', 'A2'to appear in the result and not'A1', 'B2'?
– Tim Biegeleisen
Nov 13 '18 at 8:42
it's what the software I'm working on needs. A result like'A1', 'B2'would not be correct
– Fabrizio Roman
Nov 13 '18 at 8:51
Great. Now just give codeable logic for that and maybe somone can answer your question.
– Tim Biegeleisen
Nov 13 '18 at 8:52
What do you mean? What info should I add in order to help people with their answers?
– Fabrizio Roman
Nov 13 '18 at 9:11
The record1, 'A1', 'A2'appears in the result because both the values'A1'and'A2'are contained in the same pair within the list
– Fabrizio Roman
Nov 13 '18 at 9:30
|
show 7 more comments
I have a table in a DB that I cannot modify, and I have a list of pairs of values that I have to use in order to select the records from that table that exactly match both the values in the list.
For example my list of pairs consists of ('A1', 'A2'), ('B1', 'B2'), ('C1', 'C2'), and my table consists of the following data
--------------------------
| ID | COLUMN1 | COLUMN2 |
--------------------------
| 1 | 'A1' | 'A2' |
| 2 | 'B1' | 'B2' |
| 3 | 'A1' | 'B2' |
| 4 | 'B1' | 'A2' |
| 5 | 'C1' | 'C2' |
--------------------------
The correct result set should be
1, 'A1', 'A2'
2, 'B1', 'B2'
5, 'C1', 'C2'
which means that records like
3, 'A1', 'B2'
4, 'B1', 'A2'
should not be in it.
How do I achieve such a result using HQL?
Performance is an obstacle in my situation, this is why a solution implemented via concatenation of values is not viable for me.
EDIT:
if this can help, the Java entity is very simple and it looks like this:
package org.example;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "MY_TABLE")
public class ExampleEntity implements Serializable
@Id
@Column(name = "ID", unique = true)
private int id;
@Column(name = "COLUMN1")
private String column1;
@Column(name = "COLUMN2")
private String column2;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getColumn1()
return column1;
public void setColumn1(String column1)
this.column1 = column1;
public String getColumn2()
return column2;
public void setColumn2(String column2)
this.column2 = column2;
hibernate hql
I have a table in a DB that I cannot modify, and I have a list of pairs of values that I have to use in order to select the records from that table that exactly match both the values in the list.
For example my list of pairs consists of ('A1', 'A2'), ('B1', 'B2'), ('C1', 'C2'), and my table consists of the following data
--------------------------
| ID | COLUMN1 | COLUMN2 |
--------------------------
| 1 | 'A1' | 'A2' |
| 2 | 'B1' | 'B2' |
| 3 | 'A1' | 'B2' |
| 4 | 'B1' | 'A2' |
| 5 | 'C1' | 'C2' |
--------------------------
The correct result set should be
1, 'A1', 'A2'
2, 'B1', 'B2'
5, 'C1', 'C2'
which means that records like
3, 'A1', 'B2'
4, 'B1', 'A2'
should not be in it.
How do I achieve such a result using HQL?
Performance is an obstacle in my situation, this is why a solution implemented via concatenation of values is not viable for me.
EDIT:
if this can help, the Java entity is very simple and it looks like this:
package org.example;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "MY_TABLE")
public class ExampleEntity implements Serializable
@Id
@Column(name = "ID", unique = true)
private int id;
@Column(name = "COLUMN1")
private String column1;
@Column(name = "COLUMN2")
private String column2;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getColumn1()
return column1;
public void setColumn1(String column1)
this.column1 = column1;
public String getColumn2()
return column2;
public void setColumn2(String column2)
this.column2 = column2;
hibernate hql
hibernate hql
edited Nov 13 '18 at 13:36
Fabrizio Roman
asked Nov 13 '18 at 8:36
Fabrizio RomanFabrizio Roman
84
84
What is the logic by which you chose'A1', 'A2'to appear in the result and not'A1', 'B2'?
– Tim Biegeleisen
Nov 13 '18 at 8:42
it's what the software I'm working on needs. A result like'A1', 'B2'would not be correct
– Fabrizio Roman
Nov 13 '18 at 8:51
Great. Now just give codeable logic for that and maybe somone can answer your question.
– Tim Biegeleisen
Nov 13 '18 at 8:52
What do you mean? What info should I add in order to help people with their answers?
– Fabrizio Roman
Nov 13 '18 at 9:11
The record1, 'A1', 'A2'appears in the result because both the values'A1'and'A2'are contained in the same pair within the list
– Fabrizio Roman
Nov 13 '18 at 9:30
|
show 7 more comments
What is the logic by which you chose'A1', 'A2'to appear in the result and not'A1', 'B2'?
– Tim Biegeleisen
Nov 13 '18 at 8:42
it's what the software I'm working on needs. A result like'A1', 'B2'would not be correct
– Fabrizio Roman
Nov 13 '18 at 8:51
Great. Now just give codeable logic for that and maybe somone can answer your question.
– Tim Biegeleisen
Nov 13 '18 at 8:52
What do you mean? What info should I add in order to help people with their answers?
– Fabrizio Roman
Nov 13 '18 at 9:11
The record1, 'A1', 'A2'appears in the result because both the values'A1'and'A2'are contained in the same pair within the list
– Fabrizio Roman
Nov 13 '18 at 9:30
What is the logic by which you chose
'A1', 'A2' to appear in the result and not 'A1', 'B2'?– Tim Biegeleisen
Nov 13 '18 at 8:42
What is the logic by which you chose
'A1', 'A2' to appear in the result and not 'A1', 'B2'?– Tim Biegeleisen
Nov 13 '18 at 8:42
it's what the software I'm working on needs. A result like
'A1', 'B2' would not be correct– Fabrizio Roman
Nov 13 '18 at 8:51
it's what the software I'm working on needs. A result like
'A1', 'B2' would not be correct– Fabrizio Roman
Nov 13 '18 at 8:51
Great. Now just give codeable logic for that and maybe somone can answer your question.
– Tim Biegeleisen
Nov 13 '18 at 8:52
Great. Now just give codeable logic for that and maybe somone can answer your question.
– Tim Biegeleisen
Nov 13 '18 at 8:52
What do you mean? What info should I add in order to help people with their answers?
– Fabrizio Roman
Nov 13 '18 at 9:11
What do you mean? What info should I add in order to help people with their answers?
– Fabrizio Roman
Nov 13 '18 at 9:11
The record
1, 'A1', 'A2' appears in the result because both the values 'A1' and 'A2' are contained in the same pair within the list– Fabrizio Roman
Nov 13 '18 at 9:30
The record
1, 'A1', 'A2' appears in the result because both the values 'A1' and 'A2' are contained in the same pair within the list– Fabrizio Roman
Nov 13 '18 at 9:30
|
show 7 more comments
1 Answer
1
active
oldest
votes
I try to resume the possible solutions, considering comments in the question.
Pay attention
In these solutions, I'll write in SQL because I don't know your objects, but these query can be re-write in HQL if you write your object structures
Solution 1
Consider ONLY input data set
SELECT *
FROM yourtable
WHERE column1 = 'A1' AND column2 = 'A2'
OR column1 = 'B1' AND column2 = 'B2'
OR column1 = 'C1' AND column2 = 'C2'
Solution 2
If you can create a separated table with good couple, as follow:
CREATE TABLE okcouples (col1 varchar(10), col2 varchar(10))
SELECT yt.*
FROM yourtable yt
JOIN okcouples oc
ON yt.column1 = oc.col1
AND yt.column2 = oc.col2
Solution 3
This is the same scenario of Solution 2 (create a separated table) but instead of JOIN you can use EXISTS clause, as follow:
SELECT *
FROM yourtable yt
WHERE EXISTS
(SELECT 'OK'
FROM okcouples oc
WHERE oc.col1 = yt.column1
AND oc.col2 = yt.column2)
I added the entity that refers to the table I gave you as an example, if this can be of any help
– Fabrizio Roman
Nov 14 '18 at 7:58
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%2f53276880%2fhibernate-hql-select-records-that-match-two-values-at-the-same-time%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
I try to resume the possible solutions, considering comments in the question.
Pay attention
In these solutions, I'll write in SQL because I don't know your objects, but these query can be re-write in HQL if you write your object structures
Solution 1
Consider ONLY input data set
SELECT *
FROM yourtable
WHERE column1 = 'A1' AND column2 = 'A2'
OR column1 = 'B1' AND column2 = 'B2'
OR column1 = 'C1' AND column2 = 'C2'
Solution 2
If you can create a separated table with good couple, as follow:
CREATE TABLE okcouples (col1 varchar(10), col2 varchar(10))
SELECT yt.*
FROM yourtable yt
JOIN okcouples oc
ON yt.column1 = oc.col1
AND yt.column2 = oc.col2
Solution 3
This is the same scenario of Solution 2 (create a separated table) but instead of JOIN you can use EXISTS clause, as follow:
SELECT *
FROM yourtable yt
WHERE EXISTS
(SELECT 'OK'
FROM okcouples oc
WHERE oc.col1 = yt.column1
AND oc.col2 = yt.column2)
I added the entity that refers to the table I gave you as an example, if this can be of any help
– Fabrizio Roman
Nov 14 '18 at 7:58
add a comment |
I try to resume the possible solutions, considering comments in the question.
Pay attention
In these solutions, I'll write in SQL because I don't know your objects, but these query can be re-write in HQL if you write your object structures
Solution 1
Consider ONLY input data set
SELECT *
FROM yourtable
WHERE column1 = 'A1' AND column2 = 'A2'
OR column1 = 'B1' AND column2 = 'B2'
OR column1 = 'C1' AND column2 = 'C2'
Solution 2
If you can create a separated table with good couple, as follow:
CREATE TABLE okcouples (col1 varchar(10), col2 varchar(10))
SELECT yt.*
FROM yourtable yt
JOIN okcouples oc
ON yt.column1 = oc.col1
AND yt.column2 = oc.col2
Solution 3
This is the same scenario of Solution 2 (create a separated table) but instead of JOIN you can use EXISTS clause, as follow:
SELECT *
FROM yourtable yt
WHERE EXISTS
(SELECT 'OK'
FROM okcouples oc
WHERE oc.col1 = yt.column1
AND oc.col2 = yt.column2)
I added the entity that refers to the table I gave you as an example, if this can be of any help
– Fabrizio Roman
Nov 14 '18 at 7:58
add a comment |
I try to resume the possible solutions, considering comments in the question.
Pay attention
In these solutions, I'll write in SQL because I don't know your objects, but these query can be re-write in HQL if you write your object structures
Solution 1
Consider ONLY input data set
SELECT *
FROM yourtable
WHERE column1 = 'A1' AND column2 = 'A2'
OR column1 = 'B1' AND column2 = 'B2'
OR column1 = 'C1' AND column2 = 'C2'
Solution 2
If you can create a separated table with good couple, as follow:
CREATE TABLE okcouples (col1 varchar(10), col2 varchar(10))
SELECT yt.*
FROM yourtable yt
JOIN okcouples oc
ON yt.column1 = oc.col1
AND yt.column2 = oc.col2
Solution 3
This is the same scenario of Solution 2 (create a separated table) but instead of JOIN you can use EXISTS clause, as follow:
SELECT *
FROM yourtable yt
WHERE EXISTS
(SELECT 'OK'
FROM okcouples oc
WHERE oc.col1 = yt.column1
AND oc.col2 = yt.column2)
I try to resume the possible solutions, considering comments in the question.
Pay attention
In these solutions, I'll write in SQL because I don't know your objects, but these query can be re-write in HQL if you write your object structures
Solution 1
Consider ONLY input data set
SELECT *
FROM yourtable
WHERE column1 = 'A1' AND column2 = 'A2'
OR column1 = 'B1' AND column2 = 'B2'
OR column1 = 'C1' AND column2 = 'C2'
Solution 2
If you can create a separated table with good couple, as follow:
CREATE TABLE okcouples (col1 varchar(10), col2 varchar(10))
SELECT yt.*
FROM yourtable yt
JOIN okcouples oc
ON yt.column1 = oc.col1
AND yt.column2 = oc.col2
Solution 3
This is the same scenario of Solution 2 (create a separated table) but instead of JOIN you can use EXISTS clause, as follow:
SELECT *
FROM yourtable yt
WHERE EXISTS
(SELECT 'OK'
FROM okcouples oc
WHERE oc.col1 = yt.column1
AND oc.col2 = yt.column2)
answered Nov 13 '18 at 11:47
Joe TarasJoe Taras
11.8k73146
11.8k73146
I added the entity that refers to the table I gave you as an example, if this can be of any help
– Fabrizio Roman
Nov 14 '18 at 7:58
add a comment |
I added the entity that refers to the table I gave you as an example, if this can be of any help
– Fabrizio Roman
Nov 14 '18 at 7:58
I added the entity that refers to the table I gave you as an example, if this can be of any help
– Fabrizio Roman
Nov 14 '18 at 7:58
I added the entity that refers to the table I gave you as an example, if this can be of any help
– Fabrizio Roman
Nov 14 '18 at 7:58
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%2f53276880%2fhibernate-hql-select-records-that-match-two-values-at-the-same-time%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
What is the logic by which you chose
'A1', 'A2'to appear in the result and not'A1', 'B2'?– Tim Biegeleisen
Nov 13 '18 at 8:42
it's what the software I'm working on needs. A result like
'A1', 'B2'would not be correct– Fabrizio Roman
Nov 13 '18 at 8:51
Great. Now just give codeable logic for that and maybe somone can answer your question.
– Tim Biegeleisen
Nov 13 '18 at 8:52
What do you mean? What info should I add in order to help people with their answers?
– Fabrizio Roman
Nov 13 '18 at 9:11
The record
1, 'A1', 'A2'appears in the result because both the values'A1'and'A2'are contained in the same pair within the list– Fabrizio Roman
Nov 13 '18 at 9:30