不能使用EclipseLink将org.eclipse.persistence.indirection.IndirectMap强制转换为org.eclipse.persistence.queries.FetchGroupTracker [英] org.eclipse.persistence.indirection.IndirectMap cannot be cast to org.eclipse.persistence.queries.FetchGroupTracker using EclipseLink

查看:80
本文介绍了不能使用EclipseLink将org.eclipse.persistence.indirection.IndirectMap强制转换为org.eclipse.persistence.queries.FetchGroupTracker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设计如下:

一个体育比赛有两个得分(每个球队一个),每个得分都有多个球员数据,每个球衣号码一个.两种关系都映射为Map.这只是多级聚合.

One sports game has two scores (one per team) and each score has multiple player stats, one per jersey number. Both relationships are mapped as Map. This is just multi-level aggregation.

Game实体:

@Entity
@Table(name = "\"Games\"")
@NamedQuery(name = Game.FIND_ALL, query = "SELECT ga FROM Game ga")
@NamedEntityGraph(name = Game.FETCH_SCORES, attributeNodes = {@NamedAttributeNode("scores")})
@NamedEntityGraph(name = Game.FETCH_SCORES_AND_PLAYER_STATS,
                  attributeNodes = {@NamedAttributeNode(value = "scores", subgraph = Score.FETCH_PLAYER_STATS)},
                  subgraphs = {@NamedSubgraph(name = Score.FETCH_PLAYER_STATS, attributeNodes = @NamedAttributeNode("playerStats"))}
)
public class Game implements Serializable
{
    private static final long serialVersionUID = 1L;

    public static final String FIND_ALL = "Game.findAll";
    public static final String FETCH_SCORES = "Game.fetchScores";
    public static final String FETCH_SCORES_AND_PLAYER_STATS = "Game.fetchScoresAndPlayerStats";

    @Id
    @Column
    private Integer id;

    @Basic(optional = false)
    @Column(name = "scheduled_tipoff")
    private LocalDateTime scheduledTipoff;

    @OneToMany(mappedBy = "game")
    // @MapKey(name = "home")
    @MapKeyColumn(name = "is_home", insertable = false, updatable = false)
    private Map<Boolean, Score> scores;

    ...
}

Score实体(PK是游戏的本垒打",因此PK中的Boolean home就足够了):

Score entity (PK is "home or away of a game", so a Boolean home in the PK is enough):

@Entity
@Table(name = "\"Scores\"")
@IdClass(ScoreId.class)
public class Score implements Serializable
{
    private static final long serialVersionUID = 1L;

    public static final String FETCH_PLAYER_STATS = "Score.fetchPlayerStats";

    @Id
    @Column(name = "game_id")
    private Integer gameId;

    @Id
    @Column(name = "is_home")
    private Boolean home;

    @Basic(optional = false)
    @Column(name = "roster_id")
    private Integer rosterId;

    @Basic
    @Column(name = "final_score")
    private Integer finalScore;

    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    @JoinColumn(name = "game_id", insertable = false, updatable = false)
    private Game game;

    @OneToMany(mappedBy = "score")
    @MapKeyColumn(name = "jersey_nbr")
    private Map<Integer, PlayerStat> playerStats;

    ...
}

ScoreId PK类:

public class ScoreId implements Serializable
{
    private static final long serialVersionUID = 1L;

    private Integer game;

    private Boolean home;

    ...
}

PlayerStat实体:

@Entity
@Table(name = "\"PlayerStats\"")
@IdClass(PlayerStatId.class)
public class PlayerStat implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "game_id")
    private Integer gameId;

    @Id
    @Column(name = "is_home")
    private Boolean home;

    @Id
    @Column(name = "player_id")
    private Integer playerId;

    @Basic(optional = false)
    @Column(name = "jersey_nbr", insertable = false, updatable = false)
    private Integer jerseyNbr;

    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    @JoinColumn(name = "game_id", referencedColumnName = "game_id", insertable = false, updatable = false)
    @JoinColumn(name = "is_home", referencedColumnName = "is_home", insertable = false, updatable = false)
    private Score score;
    
    ...
}

PlayerStatId:

public class PlayerStatId implements Serializable
{
    private static final long serialVersionUID = 1L;

    private Integer gameId;

    private Boolean home;

    private Integer playerId;

    ...
}

以下查询有效:

// fetch only scores: OK
games = gameService.findByNamedQuery( Game.FIND_BY_GROUP, Game.FETCH_SCORES, with( "roundId", group.getRoundId() ).and( "groupCode", group.getCode() ).map() );

更进一步,获取每个分数附带的球员统计信息:

Taking it one level further to fetch player stats attached to each score:

// fetch player stats: not OK
games = gameService.findByNamedQuery( Game.FIND_BY_GROUP, Game.FETCH_SCORES_AND_PLAYER_STATS, with( "roundId", group.getRoundId() ).and( "groupCode", group.getCode() ).map() );

... EclipseLink失败并显示:

...EclipseLink fails with:

Caused by: javax.persistence.PersistenceException: java.lang.ClassCastException: org.eclipse.persistence.indirection.IndirectMap cannot be cast to org.eclipse.persistence.queries.FetchGroupTracker
    at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:493)
    at net.bbstatstest.i303.GameService.findAllWithScores(GameService.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:509)
    at org.jboss.as.weld.ejb.DelegatingInterceptorInvocationContext.proceed(DelegatingInterceptorInvocationContext.java:92)
    at org.jboss.weld.interceptor.proxy.WeldInvocationContextImpl.interceptorChainCompleted(WeldInvocationContextImpl.java:107)
    at org.jboss.weld.interceptor.proxy.WeldInvocationContextImpl.proceed(WeldInvocationContextImpl.java:126)
    at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorBase.invokeInCallerTx(TransactionalInterceptorBase.java:203)
    at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorSupports.doIntercept(TransactionalInterceptorSupports.java:55)
    at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorBase.intercept(TransactionalInterceptorBase.java:86)
    at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorSupports.intercept(TransactionalInterceptorSupports.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.weld.interceptor.reader.SimpleInterceptorInvocation$SimpleMethodInvocation.invoke(SimpleInterceptorInvocation.java:73)
    at org.jboss.weld.interceptor.proxy.WeldInvocationContextImpl.invokeNext(WeldInvocationContextImpl.java:92)
    at org.jboss.weld.interceptor.proxy.WeldInvocationContextImpl.proceed(WeldInvocationContextImpl.java:124)
    at org.jboss.weld.bean.InterceptorImpl.intercept(InterceptorImpl.java:105)
    at org.jboss.as.weld.ejb.DelegatingInterceptorInvocationContext.proceed(DelegatingInterceptorInvocationContext.java:82)
    at org.jboss.as.weld.interceptors.EjbComponentInterceptorSupport.delegateInterception(EjbComponentInterceptorSupport.java:60)
    at org.jboss.as.weld.interceptors.Jsr299BindingsInterceptor.delegateInterception(Jsr299BindingsInterceptor.java:77)
    at org.jboss.as.weld.interceptors.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:89)
    at org.jboss.as.weld.interceptors.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:102)
    at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.as.ejb3.component.invocationmetrics.ExecutionTimeInterceptor.processInvocation(ExecutionTimeInterceptor.java:43)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:47)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:40)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:53)
    at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:52)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:54)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:252)
    ... 133 more
Caused by: java.lang.ClassCastException: org.eclipse.persistence.indirection.IndirectMap cannot be cast to org.eclipse.persistence.queries.FetchGroupTracker
    at org.eclipse.persistence.descriptors.FetchGroupManager.getObjectFetchGroup(FetchGroupManager.java:697)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.load(ObjectBuilder.java:788)
    at org.eclipse.persistence.internal.sessions.AbstractSession.load(AbstractSession.java:5335)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1249)
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:911)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1191)
    at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:485)
    at org.eclipse.persistence.internal.sessions.AbstractSession.internalExecuteQuery(AbstractSession.java:3356)
    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.indirection.QueryBasedValueHolder.instantiate(QueryBasedValueHolder.java:135)
    at org.eclipse.persistence.internal.indirection.QueryBasedValueHolder.instantiate(QueryBasedValueHolder.java:122)
    at org.eclipse.persistence.internal.indirection.DatabaseValueHolder.getValue(DatabaseValueHolder.java:97)
    at org.eclipse.persistence.internal.indirection.UnitOfWorkValueHolder.instantiateImpl(UnitOfWorkValueHolder.java:175)
    at org.eclipse.persistence.internal.indirection.UnitOfWorkValueHolder.instantiate(UnitOfWorkValueHolder.java:238)
    at org.eclipse.persistence.internal.indirection.DatabaseValueHolder.getValue(DatabaseValueHolder.java:97)
    at org.eclipse.persistence.indirection.IndirectMap.buildDelegate(IndirectMap.java:129)
    at org.eclipse.persistence.indirection.IndirectMap.getDelegate(IndirectMap.java:416)
    at org.eclipse.persistence.indirection.IndirectMap.size(IndirectMap.java:952)
    at org.eclipse.persistence.internal.queries.MapContainerPolicy.sizeFor(MapContainerPolicy.java:848)
    at org.eclipse.persistence.internal.indirection.TransparentIndirectionPolicy.instantiateObject(TransparentIndirectionPolicy.java:392)
    at org.eclipse.persistence.mappings.ForeignReferenceMapping.instantiateAttribute(ForeignReferenceMapping.java:1136)
    at org.eclipse.persistence.mappings.CollectionMapping.load(CollectionMapping.java:1404)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.load(ObjectBuilder.java:813)
    at org.eclipse.persistence.internal.sessions.AbstractSession.load(AbstractSession.java:5335)
    at org.eclipse.persistence.internal.sessions.AbstractSession.load(AbstractSession.java:5327)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1249)
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:911)
    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)
    ... 179 more

问题:

怎么了? subgraph = Score.FETCH_PLAYER_STATS一定有问题.

What's wrong? There must be something going wrong with subgraph = Score.FETCH_PLAYER_STATS.

这是EclipseLink中的错误吗?映射错误?查询+图形错误?

Is this a bug in EclipseLink? Mappings wrong? Query + graph wrong?

EclipseLink版本是2.7.7,请参阅stacktrace.

EclipseLink version is 2.7.7, see stacktrace.

PS:这在Hibernate BTW中有效.

PS: This works in Hibernate BTW.

推荐答案

这是一个错误.

请参见 https://bugs.eclipse.org/bugs/show_bug. cgi?id = 495892

归结为EclipseLink无法使用实体子图处理Map类型.

It boils down to EclipseLink being unable to handle a Map type using entity sub graphs.

使用@MapKey时,CCE为HashtableFetchgroupTracker,如果使用@MapKeyColumn,则CCE为IndirectMapFetchGroupTracker.

When using @MapKey, the CCE is Hashtable to FetchgroupTracker, if you use @MapKeyColumn the CCE is IndirectMap to FetchGroupTracker.

这篇关于不能使用EclipseLink将org.eclipse.persistence.indirection.IndirectMap强制转换为org.eclipse.persistence.queries.FetchGroupTracker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆