Hibernate中3种结果转换的详细说明(转)
Hibernate中3种结果转换的详细说明
在hibernate使用的过程中.我们通常需要对结果进行解释.
Hibernate为我们提供了以下3种解释方法:
Transformers.ALIAS_TO_ENTITY_MAP //把输出结果转换成map
Transformers.TO_LIST //把结果按顺序排进List
ransformers.aliasToBean(target) //把结果通过setter方法注入到指定的对像属性中
在Hibernate中Transformers的所有转换都是需要实现ResultTransformer接口
详解ALIAS_TO_ENTITY_MAP ,太简单了就是把key和值直接转换到Map当中
/**
* {@inheritDoc}
*/
//aliases key
//tuple value
public Object transformTuple(Object[] tuple, String[] aliases) {
Map result = new HashMap(tuple.length);
for ( int i=0; i<tuple.length; i++ ) {
String alias = aliases[i];
if ( alias!=null ) {
result.put( alias, tuple[i] );
}
}
return result;
}
详解TO_LIST,转换过程很简单,就是把value转换成List对像
/**
* {@inheritDoc}
*/
//aliases key
//tuple value
public Object transformTuple(Object[] tuple, String[] aliases) {
return Arrays.asList( tuple );
}
详解aliasToBean,转换过程就是通过读取查询后的字段.然后通过使用setter方法注入到目标对像中
public AliasToBeanResultTransformer(Class resultClass) {
if ( resultClass == null ) {
throw new IllegalArgumentException( "resultClass cannot be null" );
}
this.resultClass = resultClass;
//定义属性访问器.
propertyAccessor = new ChainedPropertyAccessor(
new PropertyAccessor[] {
PropertyAccessorFactory.getPropertyAccessor( resultClass, null ),
PropertyAccessorFactory.getPropertyAccessor( "field" )
}
);
}
public Object transformTuple(Object[] tuple, String[] aliases) {
Object result;
try {
if ( setters == null ) {
setters = new Setter[aliases.length];
for ( int i = 0; i < aliases.length; i++ ) {
String alias = aliases[i];
if ( alias != null ) {
//初始指定的setter方法
setters[i] = propertyAccessor.getSetter( resultClass, alias );
}
}
}
//实例实体对像
result = resultClass.newInstance();
for ( int i = 0; i < aliases.length; i++ ) {
if ( setters[i] != null ) {
//向setter方法中指定的属性注入值
setters[i].set( result, tuple[i], null );
}
}
}
catch ( InstantiationException e ) {
throw new HibernateException( "Could not instantiate resultclass: " + resultClass.getName() );
}
catch ( IllegalAccessException e ) {
throw new HibernateException( "Could not instantiate resultclass: " + resultClass.getName() );
}
return result; }
Hibernate中3种结果转换的详细说明(转)的更多相关文章
- Hibernate Transformers之三种结果转换说明
在hibernate使用的过程中,我们通常需要对结果进行解释.Hibernate为我们提供了以下3种查询结果解释方法: Transformers.ALIAS_TO_ENTITY_MAP //把输出结果 ...
- Hibernate-ORM:06.Hibernate中三种状态
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客主要叙述Hibernate中的三种状态:临时状态(瞬时状态),持久状态,游离状态 commit和flu ...
- 面试问题之C++语言:说一说C++中四种cast转换
C++中四种类型转换是:static_cast.dynamic_cast.const_cast.reinterpret_cast 1.const_cast 常量转换,用于将const变量转为非cons ...
- hibernate中一种导致a different object with the same identifier value was already associated with the session错误方式及解决方法
先将自己出现错误的全部代码都贴出来: hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> ...
- Hibernate中两种获取Session的方式
转自:https://www.jb51.net/article/130309.htm Session:是应用程序与数据库之间的一个会话,是hibernate运作的中心,持久层操作的基础.对象的生命周期 ...
- Hibernate中createCriteria即QBC查询的详细用法 .Hibernate中createCriteria即QBC查询的详细用法 .
现在假设有一个Student类,内有id,name,age属性String hql = "from Student s";按照以前的做法,我们通常是Query query = se ...
- Hibernate中createCriteria即QBC查询的详细用法
现在假设有一个Student类,内有id,name,age属性String hql = "from Student s";按照以前的做法,我们通常是Query query = se ...
- Hibernate中两种删除用户的方式
第一种,是比较传统的,先根据主键列进行查询到用户,在进行删除用户 //删除数据 public void deleteStudent(String sno) { init() ; Student qu ...
- Hibernate中的对象图关系转换:游离、持久、自由状态
随机推荐
- 如何删除textarea的移动版Safari的阴影?
如何删除textarea的移动版Safari的阴影? 在iphone的Safari上运行网页,textarea无法使用box-shadow样式,而且顶部有内阴影,如何清除? 添加评论 分享 赞同1 ...
- SIEM
http://en.wikipedia.org/wiki/Security_information_and_event_management http://en.wikipedia.org/wiki/ ...
- HDU 3401 Trade(单调队列优化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:炒股.第i天买入一股的价钱api,卖出一股的价钱bpi,最多买入asi股,最多卖出bsi股 ...
- 学习Haskell的一些资料
趣学Haskell指南: http://fleurer-lee.com/lyah/chapters.htm 对应的英文原文: http://learnyouahaskell.com/chapters ...
- 错误:指定的任务可执行文件位置 D:\Android\platform-tools\aapt.exe 无效
android-apt-compiler: Cannot run program "D:\android-sdk\platform-tools\aapt 装上IntelliJ IDEA /下 ...
- 六款主流免费网络嗅探软件wireshark,tcpdump,dsniff,Ettercap,NetStumbler
1.WireShark WireShark是一个开源免费的高性能网络协议分析软件,它的前身就是非常著名的网络分析软 件Ethereal.你可以使用它来解决网络疑难问题,进行网络协议分析,以及作为软件或 ...
- Zoj3332-Strange Country II(有向竞赛图)
You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 t ...
- 虚拟机环境中安装ubuntu下的mysql-cluster7.3.2(单点服务器)
部署环境: 系统:ubuntu-12.04.2 LTS -server-i386.iso Cluster:mysql-cluster-gpl-7.3.2-linux-glibc23-i686.ta ...
- hdu 2846
字典树的变形,常规字典树用来求前缀的,所以把每个单词拆成len个词建树,为了避免abab这样的查ab时会出现两次,每次加一个标记,如果该节点上次的建树的单词与本次相同就不更新,否则更新 #includ ...
- [Unit Testing] Directive testing, require parent controller
function getCompiledElement() { $scope.chart = { additional: "$ 1.56 / per minute", text: ...