HQL not working. Gives strange Exception in console.

How could that happen? I have this HQL: SELECT mfr.id FROM EMailToEMailFolderOrdered mfr WHERE NOT(SIZE(mfr.EMail) = MAX(mfr.orderNumber) AND MIN(mfr.orderNumber)=1) GROUP BY mfr.id
34 Replies
JavaBot
JavaBot8mo ago
This post has been reserved for your question.
Hey @Peter Rader! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st8mo ago
Can you show the stack trace?
Peter Rader
Peter RaderOP8mo ago
dan1st
dan1st8mo ago
Can you please also show the code of the entity and the code you used for querying?
Peter Rader
Peter RaderOP8mo ago




Peter Rader
Peter RaderOP8mo ago
/* ___ _
* | _ \___| |___ __ _ ___ ___ Copyright 2016
* | / -_) / -_) _` (_-</ -_) https://www.e-nexus.de./
* |_|_\___|_\___\__,_/__/\___| by Peter Rader _
* | \/ |__ _ _ _ __ _ __ _ ___ _ __ ___ _ _| |_
* | |\/| / _` | ' \/ _` / _` / -_) ' \/ -_) ' \ _|
* |_| |_\__,_|_||_\__,_\__, \___|_|_|_\___|_||_\__|
* All rights reserved |___/
*/
package de.e_nexus.web.rm.war;

import java.util.logging.Logger;

import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.scheduling.annotation.Scheduled;

@Named
public class MailReindexer {

/**
* The logger for this class.
*/
private static final Logger LOG = Logger.getLogger(MailReindexer.class.getCanonicalName());

@PersistenceContext
private final EntityManager entityManager = null;

@Scheduled(fixedDelay = 1_000 * 60 * 60, initialDelay = 5_000)
public void reindexEMails() {
for (Long orderedMailfolderId : entityManager.createQuery(
"SELECT mfr.id FROM EMailToEMailFolderOrdered mfr WHERE NOT(SIZE(mfr.EMail) = MAX(mfr.orderNumber) AND MIN(mfr.orderNumber)=1) GROUP BY mfr.id",
Long.class).getResultList()) {
System.out.println("Mailfolder " + orderedMailfolderId + " have unordered eMails!");
}
}
}
/* ___ _
* | _ \___| |___ __ _ ___ ___ Copyright 2016
* | / -_) / -_) _` (_-</ -_) https://www.e-nexus.de./
* |_|_\___|_\___\__,_/__/\___| by Peter Rader _
* | \/ |__ _ _ _ __ _ __ _ ___ _ __ ___ _ _| |_
* | |\/| / _` | ' \/ _` / _` / -_) ' \/ -_) ' \ _|
* |_| |_\__,_|_||_\__,_\__, \___|_|_|_\___|_||_\__|
* All rights reserved |___/
*/
package de.e_nexus.web.rm.war;

import java.util.logging.Logger;

import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.scheduling.annotation.Scheduled;

@Named
public class MailReindexer {

/**
* The logger for this class.
*/
private static final Logger LOG = Logger.getLogger(MailReindexer.class.getCanonicalName());

@PersistenceContext
private final EntityManager entityManager = null;

@Scheduled(fixedDelay = 1_000 * 60 * 60, initialDelay = 5_000)
public void reindexEMails() {
for (Long orderedMailfolderId : entityManager.createQuery(
"SELECT mfr.id FROM EMailToEMailFolderOrdered mfr WHERE NOT(SIZE(mfr.EMail) = MAX(mfr.orderNumber) AND MIN(mfr.orderNumber)=1) GROUP BY mfr.id",
Long.class).getResultList()) {
System.out.println("Mailfolder " + orderedMailfolderId + " have unordered eMails!");
}
}
}
In an eMailFolder we have the eMails ordered. If 10 eMails are in the folder, the first eMail have the OrderNumber 1 and the last eMail have the order Number 10
dan1st
dan1st8mo ago
Does it work without lazy fetching?
Peter Rader
Peter RaderOP8mo ago
However, sometimes the order numbers are broken. Then the order-numbers must be reindexed. In order to find a broken order, I assume the lowest order number is 1 and the highest is equal the count of emails.
dan1st
dan1st8mo ago
which Spring version are you using?
Peter Rader
Peter RaderOP8mo ago
5.1.7.RELEASE What do you mean? I should load the EMailToEMailFolderOrdered? Or do you mean does entity fetching work in general?
dan1st
dan1st8mo ago
I meant without the FetchType.LAZY Can you show your pom.xml?
Peter Rader
Peter RaderOP8mo ago
07-Apr-2024 08:35:05.050 INFORMATION [pool-26-thread-1] de.e_nexus.server.maven.mvn.site.TransformArticleTextToHTMLBlob.transformTextToHtmlBlob Finished transform articles
ERROR TaskUtils$LoggingErrorHandler Unexpected error occurred in scheduled task.
java.lang.ClassCastException: class org.hibernate.type.ManyToOneType cannot be cast to class org.hibernate.type.CollectionType (org.hibernate.type.ManyToOneType and org.hibernate.type.CollectionType are in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @128b7f83)
at org.hibernate.hql.internal.ast.tree.CollectionPathNode.from(CollectionPathNode.java:222)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createCollectionPath(HqlSqlWalker.java:1234)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.collectionPath(HqlSqlBaseWalker.java:5619)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.functionCall(HqlSqlBaseWalker.java:2743)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1384)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4778)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4268)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2166)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2116)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:832)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:626)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:330)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:278)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:276)
07-Apr-2024 08:35:05.050 INFORMATION [pool-26-thread-1] de.e_nexus.server.maven.mvn.site.TransformArticleTextToHTMLBlob.transformTextToHtmlBlob Finished transform articles
ERROR TaskUtils$LoggingErrorHandler Unexpected error occurred in scheduled task.
java.lang.ClassCastException: class org.hibernate.type.ManyToOneType cannot be cast to class org.hibernate.type.CollectionType (org.hibernate.type.ManyToOneType and org.hibernate.type.CollectionType are in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @128b7f83)
at org.hibernate.hql.internal.ast.tree.CollectionPathNode.from(CollectionPathNode.java:222)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createCollectionPath(HqlSqlWalker.java:1234)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.collectionPath(HqlSqlBaseWalker.java:5619)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.functionCall(HqlSqlBaseWalker.java:2743)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1384)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4778)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4268)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2166)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2116)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:832)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:626)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:330)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:278)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:276)
Same exception with eager loading. I have about 60 pom.xml in the project.
dan1st
dan1st8mo ago
Are you explicitly setting the version of anything related to Spring, Java EE or Hibernate?
Peter Rader
Peter RaderOP8mo ago
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-envers</artifactId> <version>5.4.28.Final</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.7.RELEASE</version> </dependency>
dan1st
dan1st8mo ago
mft.eMail doesn't have a size..?
Peter Rader
Peter RaderOP8mo ago
It may be that some folders have no emails. Yes
dan1st
dan1st8mo ago
like you are trying to get the size of something that has exactly one element if you want to use SIZE, I think it needs to be a collection
Peter Rader
Peter RaderOP8mo ago
Since I use Group it is a collection.
dan1st
dan1st8mo ago
I don't think that is supposed to work Also you are grouping by id
Peter Rader
Peter RaderOP8mo ago
I try again using: SELECT mfr.id FROM EMailToEMailFolderOrdered mfr WHERE SIZE(mfr) > 0 AND NOT(SIZE(mfr) = MAX(mfr.orderNumber) AND MIN(mfr.orderNumber)=1) GROUP BY mfr.id Please be patient.
dan1st
dan1st8mo ago
which eliminates any grouping
Peter Rader
Peter RaderOP8mo ago
07-Apr-2024 08:44:53.806 WARNUNG [pool-44-thread-1] de.e_nexus.web.rm.pki.CertificateInvalidationChecker.checkValid Unfortunately the file does not exists: '/home/grim/apache-tomcat-9.0.73/temp/newks-8936372-since-2024-04-07.keystore'.
07-Apr-2024 08:44:54.017 INFORMATION [pool-44-thread-1] de.e_nexus.server.maven.mvn.site.TransformArticleTextToHTMLBlob.transformTextToHtmlBlob Start transform articles
07-Apr-2024 08:44:54.020 INFORMATION [pool-44-thread-1] de.e_nexus.server.maven.mvn.site.TransformArticleTextToHTMLBlob.transformTextToHtmlBlob Finished transform articles
ERROR TaskUtils$LoggingErrorHandler Unexpected error occurred in scheduled task.
java.lang.NullPointerException: Cannot invoke "org.hibernate.persister.collection.CollectionPersister.getCollectionSpaces()" because "collectionDescriptor" is null
at org.hibernate.hql.internal.ast.tree.CollectionPathNode.<init>(CollectionPathNode.java:66)
at org.hibernate.hql.internal.ast.tree.CollectionPathNode.from(CollectionPathNode.java:100)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createCollectionPath(HqlSqlWalker.java:1234)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.collectionPath(HqlSqlBaseWalker.java:5619)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.functionCall(HqlSqlBaseWalker.java:2743)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1384)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4778)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4316)
07-Apr-2024 08:44:53.806 WARNUNG [pool-44-thread-1] de.e_nexus.web.rm.pki.CertificateInvalidationChecker.checkValid Unfortunately the file does not exists: '/home/grim/apache-tomcat-9.0.73/temp/newks-8936372-since-2024-04-07.keystore'.
07-Apr-2024 08:44:54.017 INFORMATION [pool-44-thread-1] de.e_nexus.server.maven.mvn.site.TransformArticleTextToHTMLBlob.transformTextToHtmlBlob Start transform articles
07-Apr-2024 08:44:54.020 INFORMATION [pool-44-thread-1] de.e_nexus.server.maven.mvn.site.TransformArticleTextToHTMLBlob.transformTextToHtmlBlob Finished transform articles
ERROR TaskUtils$LoggingErrorHandler Unexpected error occurred in scheduled task.
java.lang.NullPointerException: Cannot invoke "org.hibernate.persister.collection.CollectionPersister.getCollectionSpaces()" because "collectionDescriptor" is null
at org.hibernate.hql.internal.ast.tree.CollectionPathNode.<init>(CollectionPathNode.java:66)
at org.hibernate.hql.internal.ast.tree.CollectionPathNode.from(CollectionPathNode.java:100)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createCollectionPath(HqlSqlWalker.java:1234)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.collectionPath(HqlSqlBaseWalker.java:5619)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.functionCall(HqlSqlBaseWalker.java:2743)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1384)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4778)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4316)
dan1st
dan1st8mo ago
1. Grouping by the primary key doesn't actually group anything 2. What do you expect SIZE() to do here?
Peter Rader
Peter RaderOP8mo ago
You are right. I changed to GROUP BY mfr.folder.id
dan1st
dan1st8mo ago
if you do that, you cannot SELECT mfr.id
Peter Rader
Peter RaderOP8mo ago
2. Size should give the count of eMails in the folder. I changed it to mfr.folder.id accordingly. Query is now: SELECT mfr.folder.id FROM EMailToEMailFolderOrdered mfr WHERE SIZE(mfr) > 0 AND NOT(SIZE(mfr) = MAX(mfr.orderNumber) AND MIN(mfr.orderNumber)=1) GROUP BY mfr.folder.id
dan1st
dan1st8mo ago
Do you want
SELECT mfr.folder.id
FROM EMailToEMailFolderOrdered mfr
GROUP BY mfr.folder
HAVING COUNT(mfr) <> MAX(mfr.orderNumber) OR
MIN(mfr.orderNumber) <> 1
SELECT mfr.folder.id
FROM EMailToEMailFolderOrdered mfr
GROUP BY mfr.folder
HAVING COUNT(mfr) <> MAX(mfr.orderNumber) OR
MIN(mfr.orderNumber) <> 1
or something like that
Peter Rader
Peter RaderOP8mo ago
Max/Min works without group?
dan1st
dan1st8mo ago
you know that WHERE is executed before grouping by, right? ? oh I gorgot the group by edited WHERE is always without grouping edited again
Peter Rader
Peter RaderOP8mo ago
So you can never use min or max in where? Are you sure? (except subquery)
dan1st
dan1st8mo ago
not for grouping WHERE filters the elements before grouping HAVING filters the elements after grouping
Peter Rader
Peter RaderOP8mo ago
I tried this and hibernate yelled: "ERROR: aggregate functions are not allowed in WHERE" Ok, your query works fine Thanks
JavaBot
JavaBot8mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot8mo ago
Post Closed
This post has been closed by <@574922348642172929>.
Want results from more Discord servers?
Add your server