在Hibernate中从多个表中提取数据并将结果存储在一个bean中 [英] Fetching data from multiple tables in Hibernate and storing the result in a bean
问题描述
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联系{
@Id
@Column(name =CONTACT_ID)
@GeneratedValue(strategy = GenerationType.AUTO)
private contactId;
@Column(name =MOBILE_NUMBER,长度= 30)
private long mobileNo;
@Column(name =HOME_TOWN,length = 30)
private String homeTown;
@Column(name =CITY_NAME,长度= 30)
私人字符串城市;
@Column(name =STATE_NAME,length = 30)
私有字符串状态;
@Column(name =COUNTRY_NAME,长度= 30)
私人字符串国家;
以下用于激发查询和获取数据的代码。
public ContactView getContact(long userId){
Session session = sessionFactory.openSession();
Query query = session.getNamedQuery(Contact.findByUserId);
query.setLong(user,userId);
列表<?>列表= query.list();
session.close();
返回null;
}
我面对的问题是如何映射列表数据到任何定制Bean?
或者是否有其他可行的方法?
谢谢! !
query.list();
以列表形式返回查询结果。如果查询每行包含多个结果,则结果将返回到Object []的实例中。
来源: http://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/Query.html#list()
因此,我们需要按照以下方式修改我们的代码以获得预期结果:
List< Object []>列表= query.list();
for(i = 0; i< list.size(); i ++){
Object obj [] = list.get(i);
for(Object obj1:obj){
//使用适当的setter设置你的bean
}
}
其他选择:
请在hibernate中使用 ResultTransformer 结果集(复杂的选择查询)到单个实体类。
ProdEntity prod =(ProdEntity)session.createQuery(select e.productId as pId,e.price as pPrice from Product e其中e.productId =:productId)。setParameter(productId,103).setResultTransformer(Transformers.aliasToBean(ProdEntity.class))。uniqueResult();
I am using Spring and 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?
Or is there any other viable means?
Thanks!!
解决方案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[].
source: 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 ResultTransformer in hibernate to map the resultset(of a complex select query) to a single entity class.
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();
这篇关于在Hibernate中从多个表中提取数据并将结果存储在一个bean中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!