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中的对象图关系转换:游离、持久、自由状态
随机推荐
- 逻辑数据库设计 - 需要ID(谈主键Id)
本文的目标就是要确认那些使用了主键,却混淆了主键的本质而造成的一种反模式. 一.确立主键规范 每个了解数据库设计的人都知道,主键对于一张表来说是一个很重要,甚至必需的部分.这确实是事实,主键是好的数据 ...
- fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'
这个问题很奇怪.原来是/machine:X86 /machine:X64这两个链接器选项一起使用了.所以就冲突了.接手别人的项目就是晕啊.不知道为什么在VS中linker commandline的ad ...
- 使用httpclient抓取时,netstat 发现很多time_wait连接
http://wiki.apache.org/HttpComponents/FrequentlyAskedConnectionManagementQuestions 1. Connections in ...
- linux sed 使用
sed对文本的处理很强大,并且sed非常小,参数少,容易掌握,他的操作方式根awk有点像.sed按顺序逐行读取文件.然后,它执行为该行指定的所有操作,并在完成请求的修改之后的内容显示出来,也可以存放到 ...
- Copy Constructor in Java
Reference: TutorialPoints, GeekforGeeks The copy constructor is a constructor which creates an objec ...
- 你被adblock坑过吗?
最近上线一个新版本,需要在导航增加一个app的下载入口(一个图片链接),然后经过了正常的原型图.设计.切图.上线的过程,一切都是那么顺利.上线之后,像往常一下会让产品进行确认,所有的调整和优化 ...
- java.lang.IllegalStateException at org.apache.catalina.connector.ResponseFacade
2012-10-4 19:50:37 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() for se ...
- android中创建模拟器的 SDCard
在eclipse中安装了android环境后,可以直接创建AVD和sdcard的,windows->Android Virtual Device Manager,创建一个AVD时,可以同时创建s ...
- GridView视图
本文实现如下效果 Test_Grid.java public class Test_Grid extends Activity { private GridView gridview; private ...
- iOS 推送证书制作 (JAVA/PHP)
// aps_development.cer 转化成pem openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem ...