简体   繁体   English

从休眠获取数据时发生java.lang.ClassCastException

[英]java.lang.ClassCastException while fetching data from hibernate

I am trying to print details of fetched data using HQL query as follow 我正在尝试使用HQL查询打印获取的数据的详细信息,如下所示

List list = sess.createQuery("from PostMessages as pm "
                    + " left join pm.postImageses as pi "
                    + " left join pm.videosDescriptions as vd "
                    + "where pm.messageid=:msgId")
                       .setParameter("msgId", msgId).list();

            List<PostMessages> result = (List<PostMessages>) list;
            Iterator itr = result.iterator();
            while (itr.hasNext()) {
                PostMessages pm = (PostMessages) itr.next();
                System.out.print("msg: " + pm.getMessage());
                System.out.print(" type: " + pm.getMessageType());
                Set images = pm.getPostImageses();
                Iterator itr1 = images.iterator();
                while (itr1.hasNext()) {
                    PostImages pi = (PostImages) itr1.next();
                    System.out.print("id: " + pi.getImageId());
                    System.out.print("desc: " + pi.getImgDesc());
                }
                Set videos = pm.getVideosDescriptions();
                Iterator itr2 = videos.iterator();
                while (itr2.hasNext()) {
                    VideosDescription vd = (VideosDescription) itr1.next();
                    System.out.print("id: " + vd.getVideoId());
                    System.out.print("desc: " + vd.getDescription());
                }
            }

But this is showing following exception 但这显示了以下异常

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to 
hibernetMappings.PostMessages
    at hibernetMappings.TestPost.main(TestPost.java:40)

How to resolve this exception. 如何解决此异常。

Add a select clause to your query: 在查询中添加选择子句:

select distinct pm from PostMessages as pm ...

Note that the left joins are useless. 请注意,左联接是无用的。 If the goal is to retrieve the post messages with their images and video descriptions in a single query, you should use left join fetch . 如果目标是在单个查询中检索包含其图像和视频描述的帖子,则应使用left join fetch

You should also use an Iterator<PostMessages> , instead of a raw Iterator. 您还应该使用Iterator<PostMessages> ,而不是原始Iterator。 And the entity should be named PostMessage , and not PostMessages : each instance is one message, not several. 并且该实体应命名为PostMessage ,而不是PostMessages :每个实例是一条消息,而不是几条。 getImageses() should be getImages() . getImageses()应该是getImages()

Try to do this. 尝试这样做。

  Iterator itr = result.iterator();

to

  Iterator<PostMessages> itr = result.iterator();

Also change 也改变

List<PostMessages> list = sess.createQuery("from PostMessages as pm "
                    + " left join fetch pm.postImageses as pi "
                    + " left join fetch pm.videosDescriptions as vd "
                    + "where pm.messageid=:msgId")
                       .setParameter("msgId", msgId).list();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM