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 6594131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:46:15+00:00 2026-05-25T17:46:15+00:00

I have a JPQL like this one: select distinct d from Department d left

  • 0

I have a JPQL like this one:

select distinct d 
from Department d
left join fetch d.employees

When I want to fetch one of the lazy property of my Department entity, the distinct is not working any more.

select distinct d, substring(d.htmlDescription, 1,400)
from Department d
left join fetch d.employees

The query returns as much Department as the number of employees in it.

The substring(d.htmlDescription) is important because the property is defined as a CLOB (type TEXT under postgresql):

@Column(columnDefinition = "TEXT")
@Basic(fetch = FetchType.LAZY)
String htmlBody;

The substring function is translated in sql thus limiting the amount of data transfered beetween the database and the web server.

As a workaround, I tried to break the query in two parts : 

select d, substring(d.htmlDescription, 1,400)
from Department d where d in (
    select distinct d1
    from Department d1  left join fetch d1.employees
)

This doestn’t work because the JOIN FETCH must not be used in the FROM clause of a subquery.

  • 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-05-25T17:46:16+00:00Added an answer on May 25, 2026 at 5:46 pm

    Finally I found a solution to my problem by :

    1. modifying my mapping
    2. cutting the request in 2 calls.

    The htmlBody field is now in another entity. Thus the departement entity is lighter.

    class Department{
    ...
    @OneToOne (fetch = FetchType.LAZY, 
        cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    Content content = new Content();
    ...
    }
    
    
    class Content{
    ...
    @Column(columnDefinition = "TEXT")
    @Basic(fetch = FetchType.LAZY)
    String htmlBody;
    ...
    }
    

    I can then use the following requests :

        List<Department> deps = em.get().createQuery(
                "select distinct d " +
                        "from Department d " +
                        "order by d.id desc ", Department.class)
                .setFirstResult(first)
                .setMaxResults(count)
                .getResultList();
    
        List<Object[]> tuple = em.get().createQuery(
                "select d, substring(d.content.htmlBody, 1,400)" +
                        "from Department d " +
                        "left join fetch d.employees" +
                        "where d in (:deps) order by d.id desc")
                .setParameter("deps", deps)
                .getResultList();
    
        ... //Filter the duplicates due to the fetching
    

    That way, I have 2 sql queries. The fetching of employees is done in the second query witch occurs on a small amount of datas. The substring is realized in SQL. Perfect!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a JPQL like this: select sum(c.field1) field1, sum(c.field2) field2, sum(c.field3) field3
Consider the following JPQL query: SELECT foo FROM Foo foo INNER JOIN FETCH foo.bar
I would like to select every column of a table, but want to have
I would like to convert this SQL statement to a JPQL equivalent. SELECT *
Please have this query in jpql i want migrate to criteria. How do I
I have a one-to-one relation where I use CascadeType.PERSIST. This has over time build
I have a JPQL statement to return a schedule of sports games: SELECT NEW
I have a JPA entity class (one of many) and I can run JPQL
Can JPQL execute LIKE expressions against enums? If I have an entity Foo with
Have you managed to get Aptana Studio debugging to work? I tried following this,

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.