[英]How to fetch multiple column values using hibernate
我能够获取单个列值并使用控制器类访问该值,但是如何获取多个列值?
DAO方法
@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);
}
控制器方式
private static List<QuickStartSocial> followers = null;
public String aBCForm(@ModelAttribute(value = "abcbean") QuickStartBean quickstartbean,Model model) {
try {
followers=quickStartDataService.getAllSocialData();
model.addAttribute("followers",followers);
}
catch (Exception e) {
e.printStackTrace();
}
return "quickstart/create";
}
数据库
ID - socialId
column1 - followers
column2- tweets
JSP文件
<tr>
<td>
<c:forEach var="in" items="${followers}">
${in.tweets}
</c:forEach>
</td>
</tr>
我需要从数据存储在数据库中的同一社交表中获取假定的关注者和“推文”。 那么我应该使用哪种Hibernate Query,以及如何在控制器类中访问tweets和following的值?
您有几种选择:
返回实体
从“社交社交”中选择“社交”,其中social.socialId = 1
创建新的DTO,然后
从“社交社交”中选择新的SocialDto(social.followers,social.tweets),其中social.socialId = 1
映射到列表
查询q = session.createQuery(“从“社交社交”中选择“ social.followers,social.tweets”,其中“ social.socialId = 1”);
列出员工=(列表)q.list();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.