Spring 以一种形式创建两个不同对象的正确方法

Spring 以一种形式创建两个不同对象的正确方法,spring,model-view-controller,thymeleaf,Spring,Model View Controller,Thymeleaf,我正在努力解决一个问题——在一种形式中创建两个对象的正确(有效)方法是什么。我试过谷歌的很多解决方案,但都找不到有效的 我有三个模型和两个超类: 超类1: // Lots of Lombok annotations public class BaseEntity implements Serializable{ @Id @GeneratedValue(strategy=GenerationType.IDENTITY) protected Long id; pu

我正在努力解决一个问题——在一种形式中创建两个对象的正确(有效)方法是什么。我试过谷歌的很多解决方案,但都找不到有效的

我有三个模型和两个超类:

超类1:

// Lots of Lombok annotations
public class BaseEntity implements Serializable{

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    protected Long id;

    public boolean isNew() {
        return this.id == null;
    }

}
超类2:

// Lots of Lombok annotations
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type")
public class Ingredient extends BaseEntity {

    @Column(name="name")
    protected String name;

    @OneToMany(mappedBy = "ingredient")
    private Set<BatchIngredient> batches = new HashSet<>();

}
批次成分模型-批次和成分之间的关联:

// Lots of Lombok annotations
@Table(name="batch_ingredient")
@IdClass(BatchIngredientId.class)
public class BatchIngredient {

    @Id
    private Long ingredientId;

    @Id
    private Long batchId;

    @Id
    @Column(name="amount")
    private int amount;

    @Id
    @Column(name = "way_of_serving")
    private String wayOfServing;

    @ManyToOne
    @JoinColumn(name = "ingredientId", updatable = false, insertable = false, referencedColumnName = "id")
    private Ingredient ingredient;

    @ManyToOne
    @JoinColumn(name = "batchId", updatable = false, insertable = false, referencedColumnName = "id")
    private Batch batch;

}
我正在使用Batch controller输入表单:

@Controller
@RequestMapping("/batch")
class BatchController {

    @ModelAttribute("malts")
    public Set<Malt> populateMalts() {
        return maltService.findByOrderByNameAsc();
    }

    @GetMapping("/new")
    public String initCreationForm(Model model) {

        model.addAttribute("batch", new Batch());
        model.addAttribute("batchIngredient", new BatchIngredient());

        return VIEWS_BATCH_CREATE_OR_UPDATE_FORM;
    }
    // other methods...
}
@控制器
@请求映射(“/batch”)
类批处理控制器{
@模型属性(“malts”)
公共集populateMalts(){
返回maltService.findByOrderByNameAsc();
}
@GetMapping(“/new”)
公共字符串initCreationForm(模型){
model.addAttribute(“batch”,new batch());
model.addAttribute(“BatchComponent”,新的BatchComponent());
返回视图\u批处理\u创建\u或\u更新\u表单;
}
//其他方法。。。
}
此表单用于填充一些批处理数据:

<body>

    <form th:action="@{'/batch/saveBatch'}" method="post">
        <input type="hidden" name ="id" th:field="*{batch.id}">

                <label>Batch number:</label>
                <input type="text" class="form-control" th:field="*{batch.batchNumber}"/>

                // lots of other input fields to populate Batch model...

                And here we have table in the form - here a I want to create "BatchIngredient" object, and in this example - pass to this object "Malt" from the drop-down field
                <table class="table">
                    <thead class="thead">
                    <tr>
                        <th>Name</th>
                        <th>Amount</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr th:remove="all">
                        <td>Pale Ale</td>
                        <td>4000</td>
                    </tr>
                    <tr>
                        <td>
                            <select class="form-control" th:value="*{malt.id}">
                                <option th:each="malt : ${malts}"
                                        th:value="${malt?.id}"
                                        th:text="${malt?.name}">
                                </option>
                            </select>
                        </td>
                    </tr>

                    </tbody>
                </table>

        <button class="submit-button" type="submit">Submit</button>
    </form>
</body>

批号:
//许多其他输入字段用于填充批处理模型。。。
这里我们有一个表格,在这里我想创建一个“BatchComponent”对象,在这个例子中,从下拉字段传递给这个对象“Malt”
名称
数量
淡啤酒
4000
提交

我不知道如何做到这一点,或者这是否可能。一些建议?

正确的方法是创建一个具有两个对象作为其属性的单个对象。例如,类似于:

public class BatchForm {
  private Batch batch;
  private BatchIngredient batchIngredient;

  // Getters and setters
}
然后将其添加到您的模型中,并从那里开始工作。示例表单可能如下所示(尽管我不太确定
malt.id
在批次或批次配料中的位置):



Malt是添加到BatchComponent的对象-否则,我无法将多个Malt对象与“amount”值关联到“Batch”。我将在周一尝试使用这种方法。仍然按照你的建议尝试,但没有运气。这是回购协议:(邮政分行),如果您有时间看一下,我将非常感激。
<body>

    <form th:action="@{'/batch/saveBatch'}" method="post">
        <input type="hidden" name ="id" th:field="*{batch.id}">

                <label>Batch number:</label>
                <input type="text" class="form-control" th:field="*{batch.batchNumber}"/>

                // lots of other input fields to populate Batch model...

                And here we have table in the form - here a I want to create "BatchIngredient" object, and in this example - pass to this object "Malt" from the drop-down field
                <table class="table">
                    <thead class="thead">
                    <tr>
                        <th>Name</th>
                        <th>Amount</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr th:remove="all">
                        <td>Pale Ale</td>
                        <td>4000</td>
                    </tr>
                    <tr>
                        <td>
                            <select class="form-control" th:value="*{malt.id}">
                                <option th:each="malt : ${malts}"
                                        th:value="${malt?.id}"
                                        th:text="${malt?.name}">
                                </option>
                            </select>
                        </td>
                    </tr>

                    </tbody>
                </table>

        <button class="submit-button" type="submit">Submit</button>
    </form>
</body>
public class BatchForm {
  private Batch batch;
  private BatchIngredient batchIngredient;

  // Getters and setters
}
<form th:action="@{'/batch/saveBatch'}" th:object="batchForm" method="post">
  <input type="hidden" name ="id" th:field="*{batch.id}">

  <select class="form-control" th:value="*{batchIngredient.malt.id}">
    <option th:each="malt : ${malts}" th:value="${malt?.id}" th:text="${malt?.name}" />
  </select>
</form>