是否可以将Hibernate ResultsToutput映射到pojo
以下代码来自hibernate文档是否可以将Hibernate ResultsToutput映射到pojo,hibernate,Hibernate,以下代码来自hibernate文档 Session session = entityManager.unwrap(Session.class); ProcedureCall call = session.createStoredProcedureCall( "sp_person_phones"); call.registerParameter(1, Long.class, ParameterMode.IN).bindValue(1L); call.registerParameter(2, Cla
Session session = entityManager.unwrap(Session.class);
ProcedureCall call = session.createStoredProcedureCall( "sp_person_phones");
call.registerParameter(1, Long.class, ParameterMode.IN).bindValue(1L);
call.registerParameter(2, Class.class, ParameterMode.REF_CURSOR);
Output output = call.getOutputs().getCurrent();
List<Object[]> postComments = ( (ResultSetOutput) output ).getResultList();
assertEquals(2, postComments.size());
Session Session=entityManager.unwrap(Session.class);
ProcedureCall call=session.createStoredProcedureCall(“sp_person_phones”);
call.registerParameter(1,Long.class,ParameterMode.IN).bindValue(1L);
call.registerParameter(2,Class.Class,ParameterMode.REF\u游标);
输出=调用.getOutputs().getCurrent();
List postComments=((ResultSetOutput)输出).getResultList();
assertEquals(2,postComments.size());
是否可以通过指定映射将对象转换为pojo
我知道使用JPA和@SqlResultSetMapping是可能的,但我通过手动映射来寻找使用HibernateAPI的解决方案
@Retention(RetentionPolicy.RUNTIME)
public @interface ColumnIndex {
int value();
}
然后使用注释指定要映射的数组索引
@ColumnIndex(value = 4)
private Date eventDate;
还创建了一个util方法来为任何对象进行映射
public static <T> List<T> createBeanList(List<Object[]> propertyValues, Class<T> clazz) {
List<T> list = new ArrayList<T>();
for (Object[] singleObjectProperties : propertyValues) {
T object;
try {
object = clazz.newInstance();
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
int fieldIndex = field.getAnnotation(ColumnIndex.class).value();
set(object, field.getName(), singleObjectProperties[fieldIndex]);
}
} catch (Exception e) {
object = null;
}
list.add(object);
}
return list;
}
private static void set(Object object, String fieldName, Object fieldValue) {
Class<?> clazz = object.getClass();
try {
if (fieldValue != null) {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(object, fieldValue);
}
} catch (NoSuchFieldException e) {
clazz = clazz.getSuperclass();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
公共静态列表createBeanList(列出属性值,类clazz){
列表=新的ArrayList();
对于(对象[]singleObjectProperties:PropertyValue){
T对象;
试一试{
object=clazz.newInstance();
Field[]fields=object.getClass().getDeclaredFields();
用于(字段:字段){
int fieldIndex=field.getAnnotation(ColumnIndex.class).value();
set(object,field.getName(),singleObjectProperties[fieldIndex]);
}
}捕获(例外e){
object=null;
}
列表。添加(对象);
}
退货清单;
}
私有静态无效集(对象对象、字符串字段名、对象字段值){
Class clazz=object.getClass();
试一试{
如果(字段值!=null){
字段字段=clazz.getDeclaredField(字段名);
字段。setAccessible(true);
设置(对象、字段值);
}
}捕获(无此字段例外){
clazz=clazz.getSuperclass();
}捕获(例外e){
抛出新的非法状态异常(e);
}
}
调用实用程序方法
List<TCalendar> calendars = ArrayToBeanPropertiesUtil.createBeanList(arrayListResult,
TCalendar.class);
List calendars=ArrayToBeanPropertiesUtil.createBeanList(arrayListResult,
TCalendar.class);