JPA fetching one to many

I have the following entities: Event and Attribute. An event can have many attributes.

The problem I am having is, I can't seem to find a query that returns the events with their attributes in a single SQL query. I could have millions of events that need to be iterated through and I don't want to lazy load them all for obvious performance reasons.

I've tried using fetch in the query, but it returns an event for every attribute. i.e. if an event had 2 attributes it would return 2 events each with one attribute.

What I want is one event with 2 attributes.

SELECT e FROM Event e LEFT JOIN FETCH e.attributes

If I add DISTINCT it works, but then it creates a distinct SQL query which is extremely slow over large data sets.

public class Event {
  @OneToMany(mappedBy = "event", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  public Set<Attribute> getAttributes(){}

public class Attribute {
    @JoinColumn(name = "event_id", nullable = false)
    public Event getEvent() {
        return event;


I couldn't find a JPA solution. Instead, I unwrapped the EntityManager to the Hibernate session and used the criteria's result transformer as @milkplusvellocet suggested. This retrieves distinct root entities without creating a distinct SQL query.

Session session = em.unwrap(Session.class);
Criteria criteria = session.createCriteria(Event.class);
criteria.setFetchMode("attributes", FetchMode.JOIN);


One thing to note is, I tried using HQL from the unwrapped session. I added DISTINCT to the query and set the result transformer to DISTINCT_ROOT_ENTITY similar to the criteria solution above. Doing it this way still created a distinct SQL query.


You need a ResultTransformer.

Try the following HQL:


This is equivalent to using CriteriaSpecification.DISTINCT_ROOT_ENTITY in a criteria query.

Need Your Help

Adding an input field to the dom and focusing it in IE

javascript internet-explorer focus prototypejs

I am trying to make a div, that when you click it turns into an input box, and focuses it. I am using prototype to achieve this. This works in both Chrome and Firefox, but not in IE. IE refuses to ...

Python: Cannot copy file TypeError: coercing to Unicode: need string or buffer, file found

python file unicode

I have a list of text files named like "default_xxx.txt", for example: default_abc.txt, default_def.txt