Java 如何使用hibernate获取多个列值

Java 如何使用hibernate获取多个列值,java,mysql,spring,hibernate,spring-mvc,Java,Mysql,Spring,Hibernate,Spring Mvc,我能够获取单列值并使用控制器类访问该值,但是应该如何获取多个列值呢 道方法 @Transactional(readOnly = true) public List<Social> getAllSocialData() throws FastestDAOException { try { Session session = sessionFactory.getCurrentSession(); Query query = session.cr

我能够获取单列值并使用控制器类访问该值,但是应该如何获取多个列值呢

道方法

  @Transactional(readOnly = true)
public List<Social> getAllSocialData() throws FastestDAOException {
    try {
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("Select social.followers From Social social where social.socialId=1");
        return query.list();
    }
    catch (Exception ex) {
        System.out.println("Unable to get data"+ex);
}
JSP文件

<tr>

                            <td>

                                <c:forEach var="in" items="${followers}">

                                         ${in.tweets}

                                </c:forEach>

                            </td>               
                        </tr>

${in.tweets}

我需要从数据库中保存数据的同一个社交表中获取假设关注者和“tweets”。那么,我应该使用什么Hibernate查询,以及如何在我的控制器类中访问tweets和following的值呢?

您有几个选项:

  • 返回实体

    从social.socialId=1的social-social中选择social

  • 创建新的DTO,然后

    从social social中选择new SocialTo(social.followers,social.tweets),其中social.socialId=1

  • 将其映射到列表

    Query q=session.createQuery(“选择social.followers,social.tweets From social,其中social.socialId=1”)

    List employees=(List)q.List()


  • 将查询更改为此-
    选择social.followers,social.tweets FROM social social WHERE social.social ID=1
    ?并相应地更改您的
    列表
    以同时保存
    followers
    &
    tweets
    ?我尝试过,但没有成功我正在列表中对两列值进行排序,但无法使用
    ID - socialId
    column1 - followers
    column2- tweets
    
    <tr>
    
                                <td>
    
                                    <c:forEach var="in" items="${followers}">
    
                                             ${in.tweets}
    
                                    </c:forEach>
    
                                </td>               
                            </tr>