`
cjnetwork
  • 浏览: 179533 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

model driven 更新实体 外键集合丢失问题

阅读更多
在使用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) - Key Concepts and Overview #### Introduction to Model Driven Architecture (MDA) Model Driven Architecture, or MDA, is an approach to software design that ...

    Model-Driven Development Using IBM

    ### Model-Driven Development Using IBM: An In-depth Exploration #### 标题解读:Model-Driven Development Using IBM 在软件工程领域,模型驱动开发(Model-Driven Development, MDA)是一种先进的软件设计...

    Sigrity-AMM Model Driven PI Analysis Flow.rar

    Sigrity-AMM Model Driven PI Analysis Flow.rar 本教程描述了通过为多个应用程序创建子模型来重用模型的任务和步骤。 本教程最多需要两个小时,具体取决于您是否 无论是否遵循所有步骤。 在分析模型管理器中分配子...

    MDA Explained: The Practice and Promise of the Model Driven Architecture

    《MDA Explained: The Practice and Promise of the Model Driven Architecture》是一本深入探讨模型驱动架构(Model Driven Architecture,简称MDA)及其应用的书籍。本书由Anneke Kleppe、Jos Warmer和Wim Bast三...

    Model-driven engineering

    ### 模型驱动工程(Model-driven Engineering) #### 引言 模型驱动工程(Model-driven Engineering,MDE)是一种软件开发方法论,旨在通过提高抽象层次来简化复杂性,并使开发人员能够更加专注于业务逻辑而非底层...

    (Addison) MDA Explained--Model Driven Architecture, Practice & Promise.chm

    (Addison) MDA Explained--Model Driven Architecture, Practice & Promise.chm

    Model-Driven Architecture

    ### 模型驱动架构(Model-Driven Architecture,MDA):深入解析与工业应用 模型驱动架构(MDA)是一种由对象管理组织(Object Management Group,OMG)推动的IT系统开发方法论,其核心理念在于将系统的本质功能...

    Model-Driven Applications Using Model-Driven Mechanism to Bridge

    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. ...

    Model-Driven Engineering for Software Product Lines

    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: Principles of Model-Driven Architecture

    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.

    Sculpture 2.1 Model-driven development Generators(User Guide)

    ### Sculpture 2.1 Model-driven Development Generators (用户指南) #### 什么是Sculpture? Sculpture是一款强大的开源模型驱动开发生成器工具,适用于从简单的CRUD应用程序到复杂的大型企业级应用的开发。它能够...

    Graph Transformations and Model-Driven Engineering

    这本书的主题围绕图变换(Graph Transformations)和模型驱动工程(Model-Driven Engineering, MDA)两个重要领域,涵盖了这两个领域内的一些重要研究和应用。 模型驱动工程(MDA)是一种基于模型的方法,它专注于...

    Patterns: Model-Driven Development Using IBM Rational Software Architect

    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

    Model-Driven Mechanism in Information Systems and Enterprise Engineering 暨大2007讲座中部分内容的英文版

    Model-Driven Software Engineering in Practice

    英文原版,模型驱动软件建模领域经典书, 作者:Marco Brambilla Politecnico di Milano Jordi Cabot École des Mines de Nantes ManuelWimmer Vienna University of Technology

    Executable UML: A Foundation for Model-Driven Architecture(cn)

    Executable UML: A Foundation for Model-Driven Architecture,这本书虽然是02年出的,但是是MDA中很重要的研究方向,我们讨论组主要翻译了第一、二和章。

    Modelling and Generating Ajax Applications A Model-Driven Approach

    ### Modelling and Generating AJAX Applications: A Model-Driven Approach #### Introduction AJAX (Asynchronous JavaScript and XML) represents a significant shift in the way interactive web applications...

    Model-driven deep-learning.pdf

    With the arrival of the big data era, data requirements are gradually no longer an obstacle (at least for many areas), but the determination of network topology is still a bottleneck....

Global site tag (gtag.js) - Google Analytics