Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] batch loading of single object reporting that query is not a single object

James,

Thank you - that JPQL worked - and I've got that "Doh!" feeling.

Here is the mapping

Story is

@Entity
@Table(name="STORY")
public class Story {

    @Id
    @Column(name = "ID")
    private int id;

    @Column(name = "NAME", length = 255)
    private String name;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "STORY_ID", nullable = false)
    @OrderColumn(name = "REVISION")
    private List<Scenario> scenarios = new ArrayList<Scenario>();

}

Scenario is

@Entity
@Table(name="SCENARIO")
public class Scenario {

    @Id
    @Column(name = "ID")
    private int id;

    @ManyToOne
    @JoinColumn(name = "STORY_ID", updatable = false, nullable = false)
    private Story story;

    @Column(name = "NAME", length = 255)
    private String name;

    @ElementCollection(fetch = FetchType.LAZY)
    @CollectionTable(name = "SCENARIO_TAG", joinColumns = @JoinColumn(name = "SCENARIO_ID"))
    @OrderColumn(name="SEQUENCE")
    @Column(name = "TAG", length = 4000)
    private List<String> tags = new ArrayList<String>();

}

I'll look into the QueryKey on the REVISION column.

Thanks a ton!!!

Brian


----- Original message -----
From: James Sutherland <jamesssss@xxxxxxxxx>
To: eclipselink-users@xxxxxxxxxxx
Subject: Re: [eclipselink-users] batch loading of single object reporting that query is not a single object
Date: Thu, 17 May 2012 06:55:21 -0700 (PDT)


You could try,

select sc from Scenario sc, Story s JOIN s.scenarios sc2 WHERE s.id = :st
oryId and sc = sc2 order by index(sc2) asc

How is scenarios  mapped, does it use a JoinTable?  If it doesn't then you
could probably also just add a QueryKey for the index field, or even map it
in Scenario.


BrianRepko wrote:
> 
> I have 2 Entities
>   * Story, which has a 1-to-many ordered bidirectional
>     relationship with
>   * Scenario, which has an ElementCollection of tags (a List
>     element collection)
> 
> I want to write a query that returns the Scenarios for a given
> Story ID, however, I want the ability to limit the number of
> results AND to batch/pre-load the Scenario tags.
> 
> When I run the following:
> em.getTransaction().begin();
> String jpql = "select sc from Story s JOIN s.scenarios sc WHERE s.id = :st
> oryId order by index(sc) asc";
> TypedQuery<Scenario> query = em.createQuery(jpql, Scenario.class);
> query.setParameter("storyId", 2);
> query.setFirstResult(0);
> query.setMaxResults(15);
> query.setHint(QueryHints.BATCH, "sc.tags");
> query.setHint(QueryHints.BATCH_TYPE, "IN");
> query.setHint(QueryHints.LOAD_GROUP_ATTRIBUTE, "tags");
> List<Scenario> results = query.getResultList();
> em.getTransaction().commit();
> 
> I get the following error:
> Exception in thread "main" java.lang.IllegalArgumentException: Query selec
> t sc from Story s JOIN s.scenarios sc WHERE s.id = :storyId order by index
> (sc) asc, query hint eclipselink.batch is not valid for this type of query
> .
> 
> Which is because Eclipselink thinks that this is not a single
> object query. I've seen this before - it wants Scenario to be the
> first referenced table in the query, but I cannot for the life of
> me figure out how to write this with Scenario first in the query.
> When I do, then I get messages about the index() function not
> being available as that is on the ListJoin.
> 
> Any clues?
> 
> 
> 


-----
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.eclipse.org/forums/index.php?t=thread&frm_id=111&S=1b00bfd151289b297688823a00683aca
EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
Blog:  http://java-persistence-performance.blogspot.com/ Java Persistence
Performance 
-- 
View this message in context: http://old.nabble.com/batch-loading-of-single-object-reporting-that-query-is-not-a-single-object-tp33823753p33864331.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top