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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:10:31+00:00 2026-06-03T15:10:31+00:00

I have an entity (Book) with an ElementCollection for a List property (tags). @Entity

  • 0

I have an entity (Book) with an ElementCollection for a List property (tags).

@Entity
@Table(name = "BOOK")
public class Book {

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "BOOK_TAGS", joinColumns = @JoinColumn(name = "BOOK_ID"))
@Column(name = "TAG", length = "4000")
private List<String> tags;

}

For getting a single book, I’m doing entityManager.find and for getting all the books, I’m doing criteria query. This all works. I now want to make the list of tags lazily loaded, which will prevent them from loading when I get all books (which I want) but I still want to load them when I get a particular Book.

My first attempt is to change the entityManager.find to a query. The following works:

select b from Book b left outer join fetch b.tags where b.id = :id

However, I want to do this as a CriteriaQuery as well as add the option for getting tags on the get all books query. Doing the following is not working:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Book.class);
Root<Book> b = cq.from(Book.class);
b.fetch("tags", JoinType.LEFT);
cq.select(b);
cq.where(cb.equal(b.get("id"),cb.parameter(String.class,"id"));
TypedQuery<Book> q = em.createQuery(cq);
q.setParameter(id);
return q.getSingleResult();

This is failing in that it is not a single result – its looking like N objects where N is the number of tags.

I also tried adding:

q.setHint("eclipselink.join-fetch","b.tags");

and taking out the fetch but that also doesn’t work.

I’m getting the columns added to the SQL but its like the processing of those extra columns is messing up the returned object – its either multiples or no object (for hints).

Any ideas how to turn the JOIN FETCH into a proper CriteriaQuery?

  • 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-03T15:10:33+00:00Added an answer on June 3, 2026 at 3:10 pm

    Answering my own question – join fetch is not the way to go here as that will mess up result counting – trick is to use batch fetching via QueryHints – and pre-loading the tags as well as a QueryHint.

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

Sidebar

Related Questions

I have the following model classes: @Entity @Table(name = title) public final class Title
I have 2 classes @Entity @Table(name = user) public class User @Id @Column(name =
I have an entity similar to the below: public class Entity { public List<DateItem>
I have Person entity which has composition with Location Entity @ManyToOne(fetch = FetchType.EAGER, cascade
I have two related entities, say @Entity public class Book { @ManyToOne Shelf shelf;
On my Core Data Entity Book i have a boolean property, 'wasViewed' (NSNumber numberWithBool)
I have an example class book : public class Book { [Key] public int
I have 2 models. User: @Entity public class User implements Serializable { Long id;
I have one-to-many parent-child entity Book <->> Borrow Book name borrows (relation) Borrow borrowDate
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A

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.