Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8334807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:28:05+00:00 2026-06-09T03:28:05+00:00

How do I query with JPQL for the max of a join, as this

  • 0

How do I query with JPQL for the max of a join, as this SQL: SELECT MAX(MESSAGENUMBER) FROM ARTICLE left join NEWSGROUP on ARTICLE.NEWSGROUP_ID=NEWSGROUP.ID WHERE NEWSGROUP.newsgroup = "gwene.com.economist";

package net.bounceme.dur.usenet.driver;

import java.util.logging.Logger;
import javax.mail.Folder;
import javax.mail.Message;
import javax.persistence.*;
import net.bounceme.dur.usenet.model.Article;
import net.bounceme.dur.usenet.model.Newsgroup;

class DatabaseUtils {

    private static final Logger LOG = Logger.getLogger(DatabaseUtils.class.getName());
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("USENETPU");
    private EntityManager em = emf.createEntityManager();

    /*
     * SELECT MAX(MESSAGENUMBER) FROM ARTICLE left join NEWSGROUP on
     * ARTICLE.NEWSGROUP_ID=NEWSGROUP.ID WHERE NEWSGROUP.newsgroup =
     * "gwene.com.economist";
     */
    public int getMax(Folder folder) {
        em.getTransaction().begin();
        int max = 5;
        em.getTransaction().commit();
        return max;
    }

    public void persistArticle(Message message, Folder folder) {
        //do all the persistence here?
        em.getTransaction().begin();
        String fullNewsgroupName = folder.getFullName();
        Newsgroup newsgroup = null;
        TypedQuery<Newsgroup> query = em.createQuery("SELECT n FROM Newsgroup n WHERE n.newsgroup = :newsGroupParam", Newsgroup.class);
        query.setParameter("newsGroupParam", fullNewsgroupName);
        try {
            newsgroup = query.getSingleResult();
            LOG.fine("found " + query.getSingleResult());
        } catch (javax.persistence.NoResultException e) {
            LOG.fine(e + "\ncould not find " + fullNewsgroupName); 
            newsgroup = new Newsgroup(folder);
            em.persist(newsgroup);  
        } catch (NonUniqueResultException e) {
            LOG.warning("\nshould never happen\t" + fullNewsgroupName);
        }

        Article article = new Article(message, newsgroup);
        em.persist(article); 
        em.getTransaction().commit();
    }

    public void close() {
        em.close();
        emf.close();//necessary?
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:28:07+00:00Added an answer on June 9, 2026 at 3:28 am

    You can use such query to get max message number:

        String queryString = "select max(article.messagenumber) 
    from Article article left join Newsgroup newsgroup on article.newsgroup.id=newsgroup.id 
    where newsgroup.newsgroup = "gwene.com.economist"
    

    Or you can even simplified query in JPQL:

    String queryString = "select max(article.messagenumber) 
        from Article article left join article.newsgroup newsgroup
        where newsgroup.newsgroup = "gwene.com.economist"
    

    And to execute query use this:

    int maxNumber = (Integer) entityManager.createQuery(queryString).getSingleResult();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Query: SELECT u.user_id,r.totalVotes votes,r.totalPoints rating,@row:=@row+1 rank FROM mismatch_user u LEFT JOIN ratingItems r ON
Query: SELECT DISTINCT ([Equipment List].ID) AS Expr1, [Job Assignments].Job FROM [Equipment List] LEFT JOIN
I am struggling to convert this sql query SELECT feature.*, rolefeature.roleId, rolefeature.permission FROM feature
Consider the following JPQL query: SELECT foo FROM Foo foo INNER JOIN FETCH foo.bar
It is possible to do the equivalent of this sql query in JPQL? SELECT
In hibernate I want to run this JPQL / HQL query: select new org.test.userDTO(
Query : select i.Name,ri.Country,ri.State,ri.City from Information as i join ResidenceInformation as ri order by
I wrote the following query for MySQL: SELECT subquery.t1_column1, subquery.t2_id, MAX(subquery.val) FROM ( SELECT
Given the following Oracle sql query cooked for a PreparedStatement : SELECT * FROM
I'm trying to evaluate the following JPQL query: select err from Error err where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.