Java 从 Hibernate 中的多个表中获取数据并将结果存储在 bean 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21374550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fetching data from multiple tables in Hibernate and storing the result in a bean
提问by Touchstone
I am using Spring and Hibernate.
我正在使用 Spring 和 Hibernate。
NamedQuery:
命名查询:
@NamedQueries({ @NamedQuery(name = "Contact.findByUserId", query = "select cntct.mobileNo,cntct.homeTown,cntct.city,cntct.state,cntct.country,mbr.firstName,mbr.lastName,usr.userName from Contact cntct,Member mbr,User usr where cntct.user = :user")})
@Entity
@Table(name = "Contact")
public class Contact {
@Id
@Column(name="CONTACT_ID")
@GeneratedValue(strategy=GenerationType.AUTO)
private long contactId;
@Column(name="MOBILE_NUMBER", length=30)
private long mobileNo;
@Column(name="HOME_TOWN", length=30)
private String homeTown;
@Column(name="CITY_NAME", length=30)
private String city;
@Column(name="STATE_NAME", length=30)
private String state;
@Column(name="COUNTRY_NAME", length=30)
private String country;
The following code for firing the query and fetching the data.
以下代码用于触发查询并获取数据。
public ContactView getContact(long userId) {
Session session=sessionFactory.openSession();
Query query=session.getNamedQuery("Contact.findByUserId");
query.setLong("user", userId);
List<?> list=query.list();
session.close();
return null;
}
The problem that I am facing is, how to map the list's data to any custom Bean?
我面临的问题是,如何将列表的数据映射到任何自定义 Bean?
Or is there any other viable means?
或者有其他可行的方法吗?
Thanks!!
谢谢!!
采纳答案by Touchstone
query.list();
Returns the query results as a List. If the query contains multiple results per row, the results are returned in an instance of Object[].
以列表形式返回查询结果。如果查询每行包含多个结果,则结果将在 Object[] 的实例中返回。
source: http://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/Query.html#list()
来源:http: //docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/Query.html#list()
So, we need to modify our code in following way to get the intended result:
因此,我们需要按以下方式修改我们的代码以获得预期的结果:
List<Object[]> list=query.list();
for(i=0;i<list.size();i++){
Object obj[]=list.get(i);
for(Object obj1:obj){
//set your beans by using appropriate setters
}
}
Other alternative:
其他选择:
Please use ResultTransformerin hibernate to map the resultset(of a complex select query) to a single entity class.
请在休眠中使用ResultTransformer将结果集(复杂选择查询的)映射到单个实体类。
ProdEntity prod = (ProdEntity)session.createQuery("select e.productId as pId,e.price as pPrice from Product e where e.productId = :productId").setParameter("productId", 103).setResultTransformer(Transformers.aliasToBean(ProdEntity.class)).uniqueResult();
回答by StanislavL
Use generic DAO instead see for example http://yanuar7199.wordpress.com/2013/03/30/hibernate-generic-dao-for-generic-purpose/
使用通用 DAO 来代替例如 http://yanuar7199.wordpress.com/2013/03/30/hibernate-generic-dao-for-generic-purpose/
Define mapping for One-To-Many e.g. List of contacts in UserEntity.
定义一对多的映射,例如 UserEntity 中的联系人列表。