Using dynamic instantiation In Hibernate
来源:互联网 发布:eclipse编译java web 编辑:程序博客网 时间:2024/06/05 13:29
hibernate中用select new 动态构造对象,如果select出的对象不是一个具体mapped对象,则hibernate返回一个
对象数组的list,要对每个数组元素cast,势必代码显得不够简捷和coarse-grained,下面的例子描述了如何实时构造java对象
Iterator i = session.createQuery(
"select item.id, item.description, bid.amount " +
"from Item item join item.bids bid " +
"where bid.amount > 100"
)
.list()
.iterator();
while ( i.hasNext() ) {
Object[] row = (Object[]) i.next();
Long id = (Long) row[0];
String description = (String) row[1];
BigDecimal amount = (BigDecimal) row[2];
// ... show values in a report screen
}
Since the previous example was verbose and not very object-oriented (working
with a tabular data representation in arrays), we can define a class to represent
each row of results and use the HQL select new construct:
select new ItemRow( item.id, item.description, bid.amount )
from Item item join item.bids bid
where bid.amount > 100
Assuming that the ItemRow class has an appropriate constructor (you have to write
that class), this query returns newly instantiated (transient) instances of ItemRow,
as you can see in the next example:
Iterator i = session.createQuery(
"select new ItemRow( item.id, item.description, bid.amount ) " +
"from Item item join item.bids bid " +
"where bid.amount > 100"
)
.list()
.iterator();
while ( i.hasNext() ) {
ItemRow row = (ItemRow) i.next();
// Do something
}
The custom ItemRow class doesn’t have to be a persistent class; it doesn’t have to be
mapped to the database or even be known to Hibernate. ItemRow is therefore only
a data-transfer class, useful in report generation.
- Using dynamic instantiation In Hibernate
- using dynamic library in c++
- How to Make Dynamic Hyperlinks Using C# in ASP.NET
- Creating Dynamic Web Project using Maven in Eclipse
- Issues in persisting dynamic entity and view objects using MDS
- hibernate dynamic
- Using Dynamic Libraries(Updating)
- Using Dynamic Libraries
- Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
- Using inheritance and dynamic binding
- Dynamic Code Generation using CodeDOM
- Using a Dynamic Class Name
- Rod cutting - Using Dynamic Programming
- Hibernate的dynamic-update
- Dynamic developments in Java
- Dynamic Equivalence in Translating
- Dynamic Lightmaps in OpenGL
- Dynamic templateUrl in directives
- 网络打印机安装脚本(rundll32 printui.dll,PrintUIEntry用法示例)
- java中的时间操作一
- 巧治恶意网页病毒:让病毒自动还原被恶意修改键值
- java中的时间操作2 [转]
- java中的类是动态加载的,我们先看一下我们常用的类加载方式,先有一个感性的认识,才能进一步
- Using dynamic instantiation In Hibernate
- 多态性C++[转]
- 外包,不仅仅是为了赚钱(《程序员》2006年8月评论文章)
- 动态联编过程[转]
- 在那遥远的地方
- 17 个基于 Web 的 MS Office 竞争对手
- 民间炒股高手的绝招
- AD访问类库
- 如何在VS2005中自动添加事件处理函数?