问题
I have the following design:
For reference, I'm only posting the SimpleGame
entity:
@Entity
@Table(name = "\"SimpleGames\"")
@NamedQuery(name = SimpleGame.FIND_ALL, query = "SELECT ga FROM SimpleGame ga")
@NamedQuery(name = SimpleGame.FIND_ALL_JOIN_SCORES_GROUP_BY_GAME_ID, query = "SELECT ga FROM SimpleGame ga JOIN ga.simpleScores sc GROUP BY ga.id")
@NamedEntityGraph(name = SimpleGame.FETCH_SCORES, attributeNodes = {@NamedAttributeNode("simpleScores")})
public class SimpleGame implements Serializable
{
private static final long serialVersionUID = 1L;
public static final String FIND_ALL = "SimpleGame.findAll";
public static final String FIND_ALL_JOIN_SCORES_GROUP_BY_GAME_ID = "SimpleGame.findAllJoinScoresGroupByGameId";
public static final String FETCH_SCORES = "SimpleGame.fetchScores";
@Id
@Column
private Integer id;
@Basic(optional = false)
@Column(name = "scheduled_tipoff")
private LocalDateTime scheduledTipoff;
@OneToMany(mappedBy = "simpleGame")
@MapKeyColumn(name = "is_home", insertable = false, updatable = false)
private Map<Boolean, SimpleScore> simpleScores;
...
}
You can find the complete code at https://github.com/kawoolutions/primefaces-test/tree/master/src/main/java/org/eclipselink/test/fetchgroupreportquery. This is a fork of PrimeFaces test case project + JPA/EclipseLink added. You can basically get the code, use a run configuration to start the thing via Maven and then copy http://localhost:8080/primefaces-test/fetchgroupreportquery/gameScoreManager.xhtml into your browser to see what's happening.
When I execute the following code (see GameScoreManager.java
in the project URL):
TypedQuery<SimpleGame> query = em.createNamedQuery( SimpleGame.FIND_ALL, SimpleGame.class );
EntityGraph<?> graph = em.createEntityGraph( SimpleGame.FETCH_SCORES );
query.setHint( "javax.persistence.fetchgraph", graph );
List<SimpleGame> entities = query.getResultList();
...an exception gets thrown:
Exception Description: You must define a fetch group manager at descriptor (org.eclipselink.test.fetchgroupreportquery.entity.SimpleGame) in order to set a fetch group on the query (SimpleGame.findAll)
Query: ReadAllQuery(name="SimpleGame.findAll" referenceClass=SimpleGame sql="SELECT ID, scheduled_tipoff FROM "_Simple_Games"")
FetchGroup(SimpleGame.fetchScores){simpleScores => {} => {}}
at org.eclipse.persistence.exceptions.QueryException.fetchGroupValidOnlyIfFetchGroupManagerInDescriptor(QueryException.java:1307)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.prepareFetchGroup(ObjectLevelReadQuery.java:2140)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.prePrepare(ObjectLevelReadQuery.java:2160)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrePrepare(ObjectLevelReadQuery.java:985)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:829)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1191)
at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:485)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1279)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2983)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1898)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1880)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1845)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:262)
at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:482)
... 65 more
Changing the query to something that joins the simple scores + GROUP BY ga.id
results in:
TypedQuery<SimpleGame> query = em.createNamedQuery( SimpleGame.FIND_ALL_JOIN_SCORES_GROUP_BY_GAME_ID, SimpleGame.class );
EntityGraph<?> graph = em.createEntityGraph( SimpleGame.FETCH_SCORES );
query.setHint( "javax.persistence.fetchgraph", graph );
List<SimpleGame> entities = query.getResultList();
Exception:
Exception Description: Fetch group cannot be set on report query.
Query: ReportQuery(name="SimpleGame.findAllJoinScoresGroupByGameId" referenceClass=SimpleGame sql="SELECT t0.ID, t0.scheduled_tipoff FROM "_Simple_Games" t0, "_Simple_Scores" t1 WHERE (t1.game_id = t0.ID) GROUP BY t0.ID")
FetchGroup(SimpleGame.fetchScores){simpleScores => {} => {}}
at org.eclipse.persistence.exceptions.QueryException.fetchGroupNotSupportOnReportQuery(QueryException.java:1291)
at org.eclipse.persistence.queries.ReportQuery.prepareFetchGroup(ReportQuery.java:1128)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.prePrepare(ObjectLevelReadQuery.java:2160)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrePrepare(ObjectLevelReadQuery.java:985)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:829)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1191)
at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:485)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1279)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2983)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1898)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1880)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1845)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:262)
at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:482)
... 70 more
QUESTION:
What's wrong?
I mean, these are really simple statements and the design + mappings at hand are nothing special.
The first exception seems absolutely identical to EntityGraph - You must define a fetch group manager at descriptor in order to set a fetch group on the query and the second one is mentioned in the comments.
The question has not been answered, so I gave it another try.
来源:https://stackoverflow.com/questions/62397133/eclipselink-query-exceptions-using-entitygraph-you-must-define-a-fetch-group-m