有用户表User和用户组表UserGroup
User(id,name,userGroupId)
UserGroup(id,name)
public class UserGroup { private int id; private String name; private List<User> user; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @OneToMany(fetch=FetchType.LAZY,mappedBy="userGroup") public List<User> getUser() { return user; } public void setUser(List<User> user) { this.user = user; } }
public class User { private int id; private String name; private UserGroup userGroup; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="userGroupId") public UserGroup getUserGroup() { return userGroup; } public void setUserGroup(UserGroup userGroup) { this.userGroup = userGroup; } }
那么在更新UserGroup的时候就需要注意,如果jsp页面上只显示并可以修改UserGroup的name字段,那么在更新UserGroup的时候,就可能会同时删除该用户组下的所有用户。
原因是由于使用Model Driven方式的时候,jsp页面传值时获取到的实体,并不是从数据库中得到的,而是new出来的,jsp页面没有传值,则实体的对应的List属性为空,在hibernate执行update方式的时候,会将对应的改组的用户删除。
解决办法
update之前,先从数据库中载入对应主键的实体,将出去特殊字段的属性,从model driven的那个实体,转移到数据库中获取到的时候中取,然后update从数据库中载入的实体,即可完成更新操作。并且这种综合性的方法,完全可以放入一个BaseDao的基类中,这样,各个实体的操作都可以完成。(注:本例子中还是假定了每个实体都有一个id字段,并且都是主键,当然,也可以根据具体的项目使用实例方式(注解、xml配置),反射或是通过xml配置文件直接获取到主键)
@Component public class BaseDao extends HibernateDaoSupport{ @Resource(name="sessionFactory") public void setHibernateTemplate(SessionFactory sessionFactory) { super.setSessionFactory(sessionFactory); } public boolean update(Object obj){ boolean result = false; try { //根据主键加载对应实体 Object dbObj = getHibernateTemplate().get(obj.getClass(), Integer.parseInt(obj.getClass().getDeclaredMethod("getId").invoke(obj, new Object[]{}).toString())); Method[] allMethod = obj.getClass().getDeclaredMethods(); Map<String, Method> allGetMethod = new HashMap<String, Method>(); Map<String, Method> allSetMethod = new HashMap<String, Method>(); for(int i = 0; i < allMethod.length; i++){ Method method = allMethod[i]; if(method.getName().startsWith("get")){ allGetMethod.put(method.getName().substring(3), method); }else if(method.getName().startsWith("set")){ allSetMethod.put(method.getName().substring(3), method); } } //将model driven的实体属性赋值给从数据库载入的实体(List、Set属性除去) for(int i = 0; i < allMethod.length; i++){ Method method = allMethod[i]; if(method.getName().startsWith("set")){ Class[] paraType = method.getParameterTypes(); if(paraType.length > 0){ if(paraType[0] != List.class && paraType[0] != Set.class){ method.invoke(dbObj, new Object[]{allGetMethod.get(method.getName().substring(3)).invoke(obj, new Object[]{})}); } } } } getHibernateTemplate().update(dbObj); } catch (DataAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } return result; } }
画两个图,说明一下:
普通model driven方式更新
加入自定义载入步骤后的model driven方式更新