how to get result size for every select query ran using criteria and specification in springs?












-1















In JAVA after execution of the query I am counting the result set size in common database utils file before returning the data to monitor queries which are selecting more than 1000 rows. I would like to do the same in springs for both criteria and specification queries. How to achieve this?



UPDATE:



Logic I am following in java while executing query to send mail when there is a query extracting more than 2500 results.



Vector result = new Vector();
Vector row;
int colIndex;
int rowCount = 0;
int colCount;
ResultSet rst = null;
try {
rst = executeQuery(xQuery);
colCount = rst.getMetaData().getColumnCount();
while (rst.next()) {
row = new Vector();
for (colIndex = 1; colIndex <= colCount; colIndex++) {
if (rst.getString(colIndex) != null && rst.getString(colIndex) != "")
row.add(colIndex - 1, rst.getString(colIndex));
else
row.add(colIndex - 1, " ");
}
result.add(rowCount++, row);
}
try {
if (result.size() > 2500) {

Mailer(dbConn.getConnection().getMetaData().getURL(), xQuery, result.size());
}
} catch (Exception e) {

}

} catch (Exception e) {
throw e;
} finally {
if (rst != null) rst.close();
rst = null;
}
return result;









share|improve this question

























  • Can you show at least something you have done?

    – ErrorNotFoundException
    Nov 16 '18 at 9:37
















-1















In JAVA after execution of the query I am counting the result set size in common database utils file before returning the data to monitor queries which are selecting more than 1000 rows. I would like to do the same in springs for both criteria and specification queries. How to achieve this?



UPDATE:



Logic I am following in java while executing query to send mail when there is a query extracting more than 2500 results.



Vector result = new Vector();
Vector row;
int colIndex;
int rowCount = 0;
int colCount;
ResultSet rst = null;
try {
rst = executeQuery(xQuery);
colCount = rst.getMetaData().getColumnCount();
while (rst.next()) {
row = new Vector();
for (colIndex = 1; colIndex <= colCount; colIndex++) {
if (rst.getString(colIndex) != null && rst.getString(colIndex) != "")
row.add(colIndex - 1, rst.getString(colIndex));
else
row.add(colIndex - 1, " ");
}
result.add(rowCount++, row);
}
try {
if (result.size() > 2500) {

Mailer(dbConn.getConnection().getMetaData().getURL(), xQuery, result.size());
}
} catch (Exception e) {

}

} catch (Exception e) {
throw e;
} finally {
if (rst != null) rst.close();
rst = null;
}
return result;









share|improve this question

























  • Can you show at least something you have done?

    – ErrorNotFoundException
    Nov 16 '18 at 9:37














-1












-1








-1








In JAVA after execution of the query I am counting the result set size in common database utils file before returning the data to monitor queries which are selecting more than 1000 rows. I would like to do the same in springs for both criteria and specification queries. How to achieve this?



UPDATE:



Logic I am following in java while executing query to send mail when there is a query extracting more than 2500 results.



Vector result = new Vector();
Vector row;
int colIndex;
int rowCount = 0;
int colCount;
ResultSet rst = null;
try {
rst = executeQuery(xQuery);
colCount = rst.getMetaData().getColumnCount();
while (rst.next()) {
row = new Vector();
for (colIndex = 1; colIndex <= colCount; colIndex++) {
if (rst.getString(colIndex) != null && rst.getString(colIndex) != "")
row.add(colIndex - 1, rst.getString(colIndex));
else
row.add(colIndex - 1, " ");
}
result.add(rowCount++, row);
}
try {
if (result.size() > 2500) {

Mailer(dbConn.getConnection().getMetaData().getURL(), xQuery, result.size());
}
} catch (Exception e) {

}

} catch (Exception e) {
throw e;
} finally {
if (rst != null) rst.close();
rst = null;
}
return result;









share|improve this question
















In JAVA after execution of the query I am counting the result set size in common database utils file before returning the data to monitor queries which are selecting more than 1000 rows. I would like to do the same in springs for both criteria and specification queries. How to achieve this?



UPDATE:



Logic I am following in java while executing query to send mail when there is a query extracting more than 2500 results.



Vector result = new Vector();
Vector row;
int colIndex;
int rowCount = 0;
int colCount;
ResultSet rst = null;
try {
rst = executeQuery(xQuery);
colCount = rst.getMetaData().getColumnCount();
while (rst.next()) {
row = new Vector();
for (colIndex = 1; colIndex <= colCount; colIndex++) {
if (rst.getString(colIndex) != null && rst.getString(colIndex) != "")
row.add(colIndex - 1, rst.getString(colIndex));
else
row.add(colIndex - 1, " ");
}
result.add(rowCount++, row);
}
try {
if (result.size() > 2500) {

Mailer(dbConn.getConnection().getMetaData().getURL(), xQuery, result.size());
}
} catch (Exception e) {

}

} catch (Exception e) {
throw e;
} finally {
if (rst != null) rst.close();
rst = null;
}
return result;






spring postgresql jpa hibernate-criteria






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 5:20







Jagadeeswar

















asked Nov 15 '18 at 7:38









JagadeeswarJagadeeswar

48110




48110













  • Can you show at least something you have done?

    – ErrorNotFoundException
    Nov 16 '18 at 9:37



















  • Can you show at least something you have done?

    – ErrorNotFoundException
    Nov 16 '18 at 9:37

















Can you show at least something you have done?

– ErrorNotFoundException
Nov 16 '18 at 9:37





Can you show at least something you have done?

– ErrorNotFoundException
Nov 16 '18 at 9:37












1 Answer
1






active

oldest

votes


















1














If I get your question right, you need to be able to use hibernate Criteria to determine if the Resultset returned has more than 2500 rows. This can be achieved by obtaining a list from the Criteria and then determining the size of the List. For Example, say you are querying a purchase Orders table:



public static List<PurchaseOrders> getPurchaseOrderDetails() {
List<PurchaseOrders> orders = null;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Criteria cr = session.createCriteria(PurchaseOrders.class);
cr.add(Restrictions.eq("delFlg", "N"));
////add whatever other filters here
orders = cr.list();
tx.commit();
} catch (HibernateException asd) {
log.debug(asd.getMessage());
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return orders;
}


This is an equivalent of :



select * from purchase_orders where del_flg = 'N';


Note that PurchaseOrders is your jpa class having the database object mapping. Since you already have a List from here, you can always check the size.



public static void sendAlert() {
List<PurchaseOrders> orders = getPurchaseOrderDetails();
int size = orders.size();
if(size > 2500){
///Initiate alerts
}
}


I hope this helps.



UPDATE
If you need to cover all your queries in one method, you will need to write a generic method:



public static <T> List<T> getDetails(Class<T> c, Map<String, ?> params) {
List<T> details = null;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Criteria cr = session.createCriteria(c);
params.entrySet().forEach((entry) -> {
cr.add(Restrictions.eq(entry.getKey(), entry.getValue()));
});
details = cr.list();
tx.commit();
} catch (HibernateException asd) {
log.debug(asd.getMessage());
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return details;
}


and then fill in the parameters for your different queries and tables.






share|improve this answer


























  • This will work but I have to do code changes at many places. Instead I can have the logic some place, which executes for all queries. Can we achieve this in hibernate layer or somewhere ?

    – Jagadeeswar
    Nov 19 '18 at 14:52











  • In this case you will need a generic Method to cover all your query cases. See the update In the answer.

    – ErrorNotFoundException
    Nov 20 '18 at 2:43











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314496%2fhow-to-get-result-size-for-every-select-query-ran-using-criteria-and-specificati%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









1














If I get your question right, you need to be able to use hibernate Criteria to determine if the Resultset returned has more than 2500 rows. This can be achieved by obtaining a list from the Criteria and then determining the size of the List. For Example, say you are querying a purchase Orders table:



public static List<PurchaseOrders> getPurchaseOrderDetails() {
List<PurchaseOrders> orders = null;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Criteria cr = session.createCriteria(PurchaseOrders.class);
cr.add(Restrictions.eq("delFlg", "N"));
////add whatever other filters here
orders = cr.list();
tx.commit();
} catch (HibernateException asd) {
log.debug(asd.getMessage());
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return orders;
}


This is an equivalent of :



select * from purchase_orders where del_flg = 'N';


Note that PurchaseOrders is your jpa class having the database object mapping. Since you already have a List from here, you can always check the size.



public static void sendAlert() {
List<PurchaseOrders> orders = getPurchaseOrderDetails();
int size = orders.size();
if(size > 2500){
///Initiate alerts
}
}


I hope this helps.



UPDATE
If you need to cover all your queries in one method, you will need to write a generic method:



public static <T> List<T> getDetails(Class<T> c, Map<String, ?> params) {
List<T> details = null;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Criteria cr = session.createCriteria(c);
params.entrySet().forEach((entry) -> {
cr.add(Restrictions.eq(entry.getKey(), entry.getValue()));
});
details = cr.list();
tx.commit();
} catch (HibernateException asd) {
log.debug(asd.getMessage());
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return details;
}


and then fill in the parameters for your different queries and tables.






share|improve this answer


























  • This will work but I have to do code changes at many places. Instead I can have the logic some place, which executes for all queries. Can we achieve this in hibernate layer or somewhere ?

    – Jagadeeswar
    Nov 19 '18 at 14:52











  • In this case you will need a generic Method to cover all your query cases. See the update In the answer.

    – ErrorNotFoundException
    Nov 20 '18 at 2:43
















1














If I get your question right, you need to be able to use hibernate Criteria to determine if the Resultset returned has more than 2500 rows. This can be achieved by obtaining a list from the Criteria and then determining the size of the List. For Example, say you are querying a purchase Orders table:



public static List<PurchaseOrders> getPurchaseOrderDetails() {
List<PurchaseOrders> orders = null;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Criteria cr = session.createCriteria(PurchaseOrders.class);
cr.add(Restrictions.eq("delFlg", "N"));
////add whatever other filters here
orders = cr.list();
tx.commit();
} catch (HibernateException asd) {
log.debug(asd.getMessage());
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return orders;
}


This is an equivalent of :



select * from purchase_orders where del_flg = 'N';


Note that PurchaseOrders is your jpa class having the database object mapping. Since you already have a List from here, you can always check the size.



public static void sendAlert() {
List<PurchaseOrders> orders = getPurchaseOrderDetails();
int size = orders.size();
if(size > 2500){
///Initiate alerts
}
}


I hope this helps.



UPDATE
If you need to cover all your queries in one method, you will need to write a generic method:



public static <T> List<T> getDetails(Class<T> c, Map<String, ?> params) {
List<T> details = null;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Criteria cr = session.createCriteria(c);
params.entrySet().forEach((entry) -> {
cr.add(Restrictions.eq(entry.getKey(), entry.getValue()));
});
details = cr.list();
tx.commit();
} catch (HibernateException asd) {
log.debug(asd.getMessage());
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return details;
}


and then fill in the parameters for your different queries and tables.






share|improve this answer


























  • This will work but I have to do code changes at many places. Instead I can have the logic some place, which executes for all queries. Can we achieve this in hibernate layer or somewhere ?

    – Jagadeeswar
    Nov 19 '18 at 14:52











  • In this case you will need a generic Method to cover all your query cases. See the update In the answer.

    – ErrorNotFoundException
    Nov 20 '18 at 2:43














1












1








1







If I get your question right, you need to be able to use hibernate Criteria to determine if the Resultset returned has more than 2500 rows. This can be achieved by obtaining a list from the Criteria and then determining the size of the List. For Example, say you are querying a purchase Orders table:



public static List<PurchaseOrders> getPurchaseOrderDetails() {
List<PurchaseOrders> orders = null;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Criteria cr = session.createCriteria(PurchaseOrders.class);
cr.add(Restrictions.eq("delFlg", "N"));
////add whatever other filters here
orders = cr.list();
tx.commit();
} catch (HibernateException asd) {
log.debug(asd.getMessage());
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return orders;
}


This is an equivalent of :



select * from purchase_orders where del_flg = 'N';


Note that PurchaseOrders is your jpa class having the database object mapping. Since you already have a List from here, you can always check the size.



public static void sendAlert() {
List<PurchaseOrders> orders = getPurchaseOrderDetails();
int size = orders.size();
if(size > 2500){
///Initiate alerts
}
}


I hope this helps.



UPDATE
If you need to cover all your queries in one method, you will need to write a generic method:



public static <T> List<T> getDetails(Class<T> c, Map<String, ?> params) {
List<T> details = null;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Criteria cr = session.createCriteria(c);
params.entrySet().forEach((entry) -> {
cr.add(Restrictions.eq(entry.getKey(), entry.getValue()));
});
details = cr.list();
tx.commit();
} catch (HibernateException asd) {
log.debug(asd.getMessage());
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return details;
}


and then fill in the parameters for your different queries and tables.






share|improve this answer















If I get your question right, you need to be able to use hibernate Criteria to determine if the Resultset returned has more than 2500 rows. This can be achieved by obtaining a list from the Criteria and then determining the size of the List. For Example, say you are querying a purchase Orders table:



public static List<PurchaseOrders> getPurchaseOrderDetails() {
List<PurchaseOrders> orders = null;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Criteria cr = session.createCriteria(PurchaseOrders.class);
cr.add(Restrictions.eq("delFlg", "N"));
////add whatever other filters here
orders = cr.list();
tx.commit();
} catch (HibernateException asd) {
log.debug(asd.getMessage());
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return orders;
}


This is an equivalent of :



select * from purchase_orders where del_flg = 'N';


Note that PurchaseOrders is your jpa class having the database object mapping. Since you already have a List from here, you can always check the size.



public static void sendAlert() {
List<PurchaseOrders> orders = getPurchaseOrderDetails();
int size = orders.size();
if(size > 2500){
///Initiate alerts
}
}


I hope this helps.



UPDATE
If you need to cover all your queries in one method, you will need to write a generic method:



public static <T> List<T> getDetails(Class<T> c, Map<String, ?> params) {
List<T> details = null;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Criteria cr = session.createCriteria(c);
params.entrySet().forEach((entry) -> {
cr.add(Restrictions.eq(entry.getKey(), entry.getValue()));
});
details = cr.list();
tx.commit();
} catch (HibernateException asd) {
log.debug(asd.getMessage());
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return details;
}


and then fill in the parameters for your different queries and tables.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 2:46

























answered Nov 17 '18 at 7:18









ErrorNotFoundExceptionErrorNotFoundException

2,1792173132




2,1792173132













  • This will work but I have to do code changes at many places. Instead I can have the logic some place, which executes for all queries. Can we achieve this in hibernate layer or somewhere ?

    – Jagadeeswar
    Nov 19 '18 at 14:52











  • In this case you will need a generic Method to cover all your query cases. See the update In the answer.

    – ErrorNotFoundException
    Nov 20 '18 at 2:43



















  • This will work but I have to do code changes at many places. Instead I can have the logic some place, which executes for all queries. Can we achieve this in hibernate layer or somewhere ?

    – Jagadeeswar
    Nov 19 '18 at 14:52











  • In this case you will need a generic Method to cover all your query cases. See the update In the answer.

    – ErrorNotFoundException
    Nov 20 '18 at 2:43

















This will work but I have to do code changes at many places. Instead I can have the logic some place, which executes for all queries. Can we achieve this in hibernate layer or somewhere ?

– Jagadeeswar
Nov 19 '18 at 14:52





This will work but I have to do code changes at many places. Instead I can have the logic some place, which executes for all queries. Can we achieve this in hibernate layer or somewhere ?

– Jagadeeswar
Nov 19 '18 at 14:52













In this case you will need a generic Method to cover all your query cases. See the update In the answer.

– ErrorNotFoundException
Nov 20 '18 at 2:43





In this case you will need a generic Method to cover all your query cases. See the update In the answer.

– ErrorNotFoundException
Nov 20 '18 at 2:43




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314496%2fhow-to-get-result-size-for-every-select-query-ran-using-criteria-and-specificati%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python