org.onetwo.jpa.hibernate.HibernateNestedBeanTransformer Maven / Gradle / Ivy
package org.onetwo.jpa.hibernate;
import java.util.List;
import java.util.Map;
import org.hibernate.transform.ResultTransformer;
import org.onetwo.dbm.annotation.DbmResultMapping;
import org.onetwo.dbm.jdbc.mapper.nested.AbstractNestedBeanMapper;
import org.onetwo.dbm.jdbc.spi.ColumnValueGetter;
import org.onetwo.dbm.mapping.DbmMappedField;
import org.springframework.util.Assert;
import com.google.common.collect.Maps;
/**
* @author wayshall
*
*/
@SuppressWarnings({ "rawtypes", "serial" })
public class HibernateNestedBeanTransformer extends AbstractNestedBeanMapper implements ResultTransformer {
private Map names;
public HibernateNestedBeanTransformer(Class mappedClass, DbmResultMapping dbmResultMapping) {
super(null, mappedClass, dbmResultMapping);
}
@Override
public Object transformTuple(Object[] tuple, String[] aliases) {
Assert.state(this.mappedClass != null, "Mapped class was not specified");
Map names = initNames(aliases);
ColumnValueGetter columnValueGetter = new HibernateColumnValueGetter(tuple);
Object mappedObject = this.resultClassMapper.mapResult(null, names, columnValueGetter, -1);
return mappedObject;
}
private Map initNames(String[] aliases){
if(names!=null){
return names;
}
Map names = Maps.newHashMapWithExpectedSize(aliases.length);
for (int i = 0; i < aliases.length; i++) {
names.put(aliases[i], i);
}
return names;
}
@Override
public List transformList(List collection) {
return collection;
}
public static class HibernateColumnValueGetter implements ColumnValueGetter {
private Object[] tuple;
public HibernateColumnValueGetter(Object[] tuple) {
super();
this.tuple = tuple;
}
@Override
public Object getColumnValue(int index, Class> requiredType) {
return tuple[index];
}
@Override
public Object getColumnValue(int index, DbmMappedField field) {
return tuple[index];
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy