在使用ssh框架开发web的时候,有的时候使用model driven方式接收参数,在通常情况下没有什么问题。当执行更新操作的时候,由于执行update的时候,实体的外键关系,容易被删除掉,这个问题需要注意。例如
有用户表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方式更新
- 大小: 44 KB
- 大小: 51.9 KB
分享到:
相关推荐
Model Driven Architecture.pdf
Model-Driven Architecture
MDA Explained: The Practice and Promise of the Model Driven Architecture
Model-driven engineering technologies offer a promising approach to address the inability of third-generation languages to alleviate the complexity of platforms and express domain concepts effectively...
(Addison) MDA Explained--Model Driven Architecture, Practice & Promise.chm
MDA 模型驱动开发 UML
A Model Driven Architecture for Enterprise Application Integration. A Model Driven Architecture for Enterprise Application Integration.
Model Driven Architecture Applying MDA to Enterprise Computing
模型驱动系统开发 (Model Driven Systems Development)
Model-driven applications: Using model-driven mechanism to bridge the gap between business and IT. In Díaz, V. et al. eds, Advances and Application in Model-Driven Software Engineering, ch. 3, pp. ...
Graph Transformations and Model-Driven Engineering -- Essays Dedicated to Manfred Nagl on the Occasion of his 65th Birthday Lecture Notes in Computer Science 5765
Modeling variability in the context of software product-lines has been around for about 25 years in the research community. It started with Feature Modeling and soon enough was extended to handle many...
MDA Distilled is an accessible ... You will also learn how to define a model-driven process for a project involving multiple platforms, implement that process, and then test the resulting system.
详细介绍了OMG的战略性开发方法学——MDA。 阐述了UML和MDA的关系,并介绍了MOF、XMI和CWM等与MDA相关的技术。 分析MDA如何运用基于UML的技术,将基于组件开发、设计模式、中间件等一系列重要的趋势性技术整合到...
ONAP Model Driven Orchestration with TOSCA - Why TOSCA is one of ONAP’s main Pillars?
Model Driven Development with Executable UML
Patterns: Model-Driven Development Using IBM Rational Software Architect First Edition (December 2005) This edition applies to Version 6.0.0.1 of Rational Software Architect (product number 5724-I70).
Model-Driven Mechanism in Information Systems and Enterprise Engineering 暨大2007讲座中部分内容的英文版
uml,model driven 模型驱动,UML设计