Java 带有Hibernate多个联接查询的ClassCastException

Java 带有Hibernate多个联接查询的ClassCastException,java,postgresql,hibernate,many-to-many,named-query,Java,Postgresql,Hibernate,Many To Many,Named Query,我有两个实体,奖励和用户,它们是相关的manytomy,如下所示: public class Reward { @Id @Column(name="reward_id") private long rewardId; @ManyToMany(mappedBy="favourite") @LazyCollection(LazyCollectionOption.FALSE) private Collection<User> favourit

我有两个实体,奖励和用户,它们是相关的
manytomy
,如下所示:

public class Reward {
    @Id
    @Column(name="reward_id")
    private long rewardId;
    @ManyToMany(mappedBy="favourite")
    @LazyCollection(LazyCollectionOption.FALSE)
    private Collection<User> favouriteUsers;
}
问题是,当我运行此查询并将查询结果等同于键入
奖励
时,我得到以下异常:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.xxxxxxx.model.Reward
    at com.xxxxxxx.sevice.RewardService.addFavourite(RewardService.java:26)
    at com.xxxxxxx.resources.RewardResource.addFavourite(RewardResource.java:36)
    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.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
此查询的hibernate查询日志如下:

Hibernate: select favouriteu0_.favourite_id as favourit2_3_0_, favouriteu0_.user_id as user_id1_3_0_, user1_.user_id as user_id1_2_1_ from FAVOURITE favouriteu0_ inner join USER user1_ on favouriteu0_.user_id=user1_.user_id where favouriteu0_.favourite_id=?
我想不出这个错误。有人能帮忙吗

编辑:我执行查询的部分是:

public Reward addFavourite(long userId, Reward favouriteReward) {
        Object<Reward> rewardObject = new ObjectImpl<Reward>();
        long favouriteId = favouriteReward.getRewardId();
        // Check if favourited before. 
        // Below is the line that throws the exception.
        Reward checkReward = rewardObject.getObjectByNamedQueryLongLong("Favourite.byUserAndReward", userId, favouriteId)
        if (checkReward != null) {
            return favouriteReward;
        }
}
公共奖励addFavorite(长用户ID,奖励Favorite奖励){
Object rewardObject=new ObjectImpl();
long-favoriteId=favoriteReward.getRewardId();
//检查之前是否喜欢。
//下面是引发异常的行。
奖励检查奖励=rewardObject.GetObjectByNamedQueryLong(“Favorite.ByUserAndWard”,用户ID,FavoriteID)
if(checkReward!=null){
回报优待奖励;
}
}

[Ljava.lang.Object
表示您的查询返回对象的数组,您试图将其强制转换为对象

更新: 由于您在不使用
SELECT
子句的情况下进行查询,并加入
User
,Hibernate将返回
奖励
用户
对象,这就是您获得
对象[]
的原因。我可以想出两个解决方法

  • 您可以在
    SELECT
    子句中提到要获取的对象,如
    从奖励r中选择r加入r.FavoriteUsers u,其中u.userId=?和r.rewardId=?
  • 或者,您可以在查询中使用
    fetch
    ,如
    from Reward r join fetch r.favoriteUsers u,其中u.userId=?和r.rewardId=?

  • 你能把代码贴在执行代码的地方吗query@wolverine我添加了代码的一部分。您发布的查询日志似乎不完整。这是唯一执行的查询吗?它甚至不是从奖励表中进行查询,但在查询的定义中,我具体执行了
    object=(T)query.uniqueResult();
    谢谢,使用了您提到的选项1。
    Hibernate: select favouriteu0_.favourite_id as favourit2_3_0_, favouriteu0_.user_id as user_id1_3_0_, user1_.user_id as user_id1_2_1_ from FAVOURITE favouriteu0_ inner join USER user1_ on favouriteu0_.user_id=user1_.user_id where favouriteu0_.favourite_id=?
    
    public Reward addFavourite(long userId, Reward favouriteReward) {
            Object<Reward> rewardObject = new ObjectImpl<Reward>();
            long favouriteId = favouriteReward.getRewardId();
            // Check if favourited before. 
            // Below is the line that throws the exception.
            Reward checkReward = rewardObject.getObjectByNamedQueryLongLong("Favourite.byUserAndReward", userId, favouriteId)
            if (checkReward != null) {
                return favouriteReward;
            }
    }