下面内容仅为自己理解,仅供参考
对象关系映射主要作用
1、将查询结果自动封装为对象
2、将对像持久化到数据库时字段映射。
如何映射?
@Entity @Table(name = "cin_clientupdate_new", catalog = "urapport_config") public class ClientVersion { private Long id; private String clienttype; private String clientversion; private String updatetype; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", unique = true, nullable = false) public Long getId() { return id; } public void setId(Long id) { this.id = id; } }
如果使用注解,示例如上(省略部分属性、方法),必须要有@Entity @Table @Id 注解,其它字段可不要注解,映射时会自动将对象的属性名和数据库的字段名对应。如果名称不一致就必须写明。
使用示例
1、使用原生sql,对象有映射关系时示例如下,会对结果进行自动映射
public List<T> sqlQuery(final String sql, final Class clazz, final Object... values) { return (List) getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws SQLException { List list = null; try { SQLQuery query = session.createSQLQuery(sql); query.addEntity(clazz);//添加实体类,后面会对结果进行映射 if (values != null && values.length > 0) { for (int i = 0; i < values.length; i++) { query.setParameter(i, values[i]); } } list = query.list(); } finally { releaseSession(session); } return list; } }); }
2、使用原生sql查询,对象无映射关系时示例如下,需要自己对结果进行封装
public <T> List<T> sqlQueryPOJO(final String sql, final Class sclass, final Object... objs) { return (List<T>) this.getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws SQLException { List list; List l; try { if (sclass == null) { throw new Exception("Class sclass is null!!!"); } SQLQuery query = session.createSQLQuery(sql); query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);//将结果转为Map对象 for (int i = 0; i < objs.length; i++) { query.setParameter(i, objs[i]); } list = query.list(); l = getDataFromMap(list, sclass);//将Map转为对象 } catch (Exception e) { e.printStackTrace(); return null; } finally { releaseSession(session); } return l; } }); }
private List getDataFromMap(List<Map<String, Object>> list, Class sclass) throws InstantiationException, IllegalAccessException, ClassNotFoundException, InvocationTargetException, NoSuchFieldException { List l = new ArrayList(); if (list != null && list.size() > 0) { Field[] fs = sclass.getDeclaredFields(); for (Map<String, Object> map : list) { Object obj = Class.forName(sclass.getName()).newInstance(); for (Field f : fs) { String n = f.getName(); if ("serialVersionUID".equals(n)) { continue; } Object v = map.get(n); if (v != null) { if ("java.sql.Timestamp".equals(v.getClass().getName()) && "java.util.Date".equals(f.getType().getName())) { java.sql.Timestamp t = (java.sql.Timestamp) v; Calendar c = Calendar.getInstance(); c.setTimeInMillis(t.getTime()); v = c.getTime(); } BeanUtils.setProperty(obj, n, v); } else { Column column = f.getAnnotation(Column.class); String str = n; if (column == null) { str = str.replaceFirst(str.substring(0, 1), str.substring(0, 1).toUpperCase()); try { column = sclass.getMethod("get" + str).getAnnotation(Column.class); } catch (NoSuchMethodException e) { ; } } if (column == null) { continue; } str = column.name(); v = map.get(str); if (v != null) { BeanUtils.setProperty(obj, n, v); } } } l.add(obj); } } return l; }
总结:对象关系映射不一定要有,主要看项目中怎么使用(上面代码仅为示例)