简体   繁体   English

在Struts 2中使用嵌套域对象进行CRUD的正确方法是什么?

[英]What's the correct way to do CRUD with nested domain objects in Struts 2?

I have seen examples that show how to do CRUD with one domain object without nested domain objects (just primitives). 我已经看到了一些示例,展示了如何使用一个没有嵌套域对象(只是基元)的域对象来进行CRUD。 The problem is how to do the same thing with a domain object that has references to other domain objects. 问题是如何使用引用其他域对象的域对象执行相同的操作。 Giving the following example: 给出以下示例:

@Entity
public class Person implements Serializable {
    @Id
    private Long id;
    private String name;
    private Integer age;
    private Address address;
    private MaritalStatus maritalStatus;
... //getters/setters
}

@Entity
public class MaritalStatus implements Serializable {
    @Id
    private Long id;
    private String description;
... //getters/setters
}

@Entity
public class Address implements Serializable {
    @Id
    private Long id;
    private String street;
    private String state;
    private String zip;
... //getters/setters
}

Let's say I have a form which creates or update a Person and ask for the following inputs: 假设我有一个创建或更新Person的表单并要求输入以下内容:

Name: _ _ 名称: _ _

Age: _ ____ 年龄: _ ____

Street: _ __ 街: _ __

State: _ ____ 州: _ ____

Zip: _ __ _ 邮编: _ __ _

Marital Status: (a select input with the corresponding key(id of the entity)/value) 婚姻状况:(具有相应密钥(实体的ID)/值的选择输入)

So how do you create or update having nested properties which has their own identity too (persisted in another table). 那么如何创建或更新具有自己标识的嵌套属性(在另一个表中保留)。

I was thinking something like this using the prepare method and paramsPrepareParamsStack: 我正在使用prepare方法和paramsPrepareParamsStack来思考这样的事情:

public class PersonAction extends ActionSupport {
    public String save() {
        personService.save(person);
        return SUCCESS;
    }

    public String update() {
        personService.update(person);
        return SUCCESS;
    }

    public void prepare() {
        if (person.getId() != null) {
            //find the person using the id.
            Person p = personService.findById(person.getId());

            //Update the reference to the selected martial status
            //find the maritalstatus selected from the select box
            MaritalStatus maritalStatus = 
                maritalStatusSerivce.findById(person.getMaritalStatus().getId());
            //set the reference to the obtained person
            p.setMaritalStatus(maritalStatus);

            //find the address (in case it already exist)
            if (person.getAddress().getId() != null) {
                //find the address
                Address address = addressService.findById(person.getAddress().getId());
                //set the reference
                p.setAddress(address);
            }

            //finally set the obtained reference to the action person property
            this.person = p;
        } else { //New person
            //Find the address for the person
            if (person.getAddress().getId() != null) {
                //Only set the reference to the selected marital status
                //find the maritalstatus selected from the select box
                MaritalStatus maritalStatus = 
                    maritalStatusSerivce.findById(person.getMaritalStatus().getId());
                //set the reference to the person
                person.setMaritalStatus(maritalStatus);
            }
        }
    }

    private Person person;
    //getter/setters
}

Is that the correct way? 这是正确的方法吗? Any other suggested approach? 任何其他建议的方法?

Thanks 谢谢

I have a few suggestions 我有一些建议

  1. I question whether MaritalStatus and Address need to be their own entities. 我怀疑MaritalStatus和Address是否需要成为他们自己的实体。 Would you ever have an address or marital status independently from a Person? 您是否拥有独立于某人的地址或婚姻状况? If yes, then ok, if no, you should make MaritalStatus and Address components 如果是,那么好吧,如果没有,你应该制作MaritalStatus和地址组件

  2. Most of the code in your action should be in another service. 您的操作中的大多数代码都应该在另一个服务中。 I would recommend creating some sort of facade service that coordinates all those operations and move it into a separate layer. 我建议创建一些门面服务,协调所有这些操作并将其移动到一个单独的层。 I am basically saying move the prepare method to a service. 我基本上是说将prepare方法移动到服务中。 A Struts action is really for just invoking services, which you are doing, however the way you are invoking the services is business logic, which should be in a service. Struts操作实际上只是用于调用您正在执行的服务,但是您调用服务的方式是业务逻辑,应该在服务中。 Actions should just handle the requests, invoke the business logic, and return a response. 操作应该只处理请求,调用业务逻辑并返回响应。 Your facade service would have all the services it needs to do its stuff. 您的门面服务将拥有完成其所需的所有服务。

One benefit of that approach is that you can then reuse the operation. 该方法的一个好处是您可以重用该操作。 As it stands right now, if you wanted another action to do the same business operation you would have some refactoring to do. 就目前而言,如果你想要另一个行动来做同样的业务操作,你可能需要进行一些重构。

The answer to your specific question is mostly in number 2 -- you create a service that takes all the arguments it needs to coordinate the creation and/or updates of the underlying entities. 您的具体问题的答案主要在第2位 - 您创建的服务可以获取协调基础实体的创建和/或更新所需的所有参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用多个ID时在JPQL中执行CRUD操作的正确方法是什么 - What is correct way to do CRUD operation in JPQL when working with multiple IDs 创建用于创建域对象的工厂的正确方法 - The correct way of creating a Factory for creating domain objects 正确的方法是什么? - What is the correct way to do this? 在 Struts 2 中初始化 Action 类的字段的正确方法是什么? - What is the correct way to initialize fields of Action classes in Struts 2? 在JSF中使用CRUD的正确方法 - Correct way of using CRUD in JSF 建模聚合对象的正确方法是什么? - What is the correct way of modeling aggregate objects? 实现AsyncTask的正确方法是什么?静态或非静态嵌套类? - What's the correct way to implement AsyncTask? static or non static nested class? 在Struts 1.3中,Controller用变量填充View的最佳方法是什么? - In Struts 1.3, what's the best way for the Controller to fill the View with variables? 使用struts框架创建等待页面的最佳方法是什么 - What's the best way to create a waiting page using the struts framework 在Ebean中创建嵌套对象的最简单方法是什么? - What is the simplest way to create nested objects in Ebean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM