【问题标题】:Saving a list of related objects in Play! Framework 1.x from a form在 Play 中保存相关对象的列表!来自表单的框架 1.x
【发布时间】:2018-12-15 20:27:04
【问题描述】:

我在 Play 模型中有一个 Offer 对象,它可以有一个或多个 OfferLines,所以我有

@Required
@ManyToOne
public Offer offer;

在OfferLine上

和

@OneToMany(mappedBy="offer", cascade= CascadeType.ALL)
public List<OfferLine> offerLines;

提供。

当我打开我的表单时,我有以下内容来填充渲染内容

Offer myOffer = new Offer();
myOffer.offerFrom = currentUser;
myOffer.targetSwag = targetSwag;
myOffer.offerTo = targetSwag.Owner;
myOffer.offerLines = new ArrayList<OfferLine>();

for (Swag swagline : mySwag)
{
    OfferLine offerLine = new OfferLine();
    offerLine.quantity=0;
    offerLine.swag = swagline;
    myOffer.offerLines.add(offerLine);
}

myOffer.save();
renderArgs.put("myOffer",myOffer);
render();

最后,我的表单看起来像这样(尽管有缩进......)

#{form @submitOffer(), id:'submitOffer'}
<table border="1">
    <thead>
    <tr>
        <th width="30" align="left">ID</th>
        <th width="80" align="left">Swag</th>
        <th width="30" align="left">Quantity</th>
    </tr>
    </thead>
    <tbody>
    #{list items:myOffer.offerLines, as:'offerline'}

    <tr>
    <td height="32" width="30"><input type="text" id="offerline.id" 
name="offerline.id" value="${offerline.id}"/></td>
    <td height="32" width="80">
        ${offerline.swag.Name}
    </td>
    <td height="32">
        <input class="field" id="offerline.quantity" name="offerline.quantity" type="number" min="0" max="${offerline.swag.Quantity}" value="${offerline.quantity}"/>
    </td>
</tr>
    #{/list}
    </tbody>
</table>

当我提交时,我会调用这样的方法...

public static void submitOffer(Integer amountRequested, Offer myOffer)
{
    myOffer.save();
    System.out.println("Requested " + amountRequested);
    System.out.println("My offer coming out " + myOffer.toString());


    renderJSON(myOffer);

}

但我的 Offerlines 不在 JSON 中,也就是说,它们似乎没有在 Offer 对象中传递回。如何在一个屏幕中编辑和保存与优惠相关的优惠行列表?

[更新] 我测试了传递给表单的 MyOffer 对象,它具有报价线,但它不会再次出现..

    {
  "offerLines": [
    {
      "swag": {
        "Name": "Test Swag",
        "Description": "Big ol' test swag",
        "Quantity": 10,
        "Owner": {
          "username": "robcowell",
          "password": "REDACTED",
          "email": "REDACTED",
          "fullname": "Rob Cowell",
          "isAdmin": true,
          "isActive": true,
          "Photo": {
            "bucket": "REDACTED",
            "key": "d5654f8f-0cd0-45a2-a77f-3edda01feca0",
            "contentLength": 0
          },
          "hasChangedPassword": true,
          "id": 1
        },
        "Photo": {
          "bucket": "REDACTED",
          "key": "b0774667-a32a-4508-81ba-cdb84ebc689f",
          "contentLength": 0
        },
        "id": 52
      },
      "quantity": 0,
      "id": 134
    },
    {
      "swag": {
        "Name": "Swag 2",
        "Description": "Swag 2",
        "Quantity": 5,
        "Owner": {
          "username": "robcowell",
          "password": "REDACTED",
          "email": "REDACTED",
          "fullname": "Rob Cowell",
          "isAdmin": true,
          "isActive": true,
          "Photo": {
            "bucket": "REDACTED",
            "key": "d5654f8f-0cd0-45a2-a77f-3edda01feca0",
            "contentLength": 0
          },
          "hasChangedPassword": true,
          "id": 1
        },
        "id": 54
      },
      "quantity": 0,
      "id": 135
    },
    {
      "swag": {
        "Name": "Swag 3",
        "Description": "Mo' Swag",
        "Quantity": 1,
        "Owner": {
          "username": "robcowell",
          "password": "REDACTED",
          "email": "REDACTED",
          "fullname": "Rob Cowell",
          "isAdmin": true,
          "isActive": true,
          "Photo": {
            "bucket": "REDACTED",
            "key": "d5654f8f-0cd0-45a2-a77f-3edda01feca0",
            "contentLength": 0
          },
          "hasChangedPassword": true,
          "id": 1
        },
        "id": 55
      },
      "quantity": 0,
      "id": 136
    },
    {
      "swag": {
        "Name": "Test swag with an inordinately long name purely for the purposes of testing the rendering of these columns",
        "Description": "As above",
        "Quantity": 1,
        "Owner": {
          "username": "robcowell",
          "password": "REDACTED",
          "email": "REDACTED",
          "fullname": "Rob Cowell",
          "isAdmin": true,
          "isActive": true,
          "Photo": {
            "bucket": "REDACTED",
            "key": "d5654f8f-0cd0-45a2-a77f-3edda01feca0",
            "contentLength": 0
          },
          "hasChangedPassword": true,
          "id": 1
        },
        "id": 60
      },
      "quantity": 0,
      "id": 137
    }
  ],
  "offerTo": {
    "username": "todd",
    "password": "REDACTED",
    "email": "REDACTED",
    "fullname": "Todd Halfpenny",
    "isAdmin": false,
    "isActive": true,
    "hasChangedPassword": true,
    "id": 20
  },
  "offerFrom": {
    "username": "robcowell",
    "password": "REDACTED",
    "email": "REDACTED",
    "fullname": "Rob Cowell",
    "isAdmin": true,
    "isActive": true,
    "Photo": {
      "bucket": "REDACTED",
      "key": "d5654f8f-0cd0-45a2-a77f-3edda01feca0",
      "contentLength": 0
    },
    "hasChangedPassword": true,
    "id": 1
  },
  "targetSwag": {
    "Name": "Todd's tshirts",
    "Description": "Nice tshirts by Todd",
    "Quantity": 10,
    "Owner": {
      "username": "todd",
      "password": "REDACTED",
      "email": "REDACTED",
      "fullname": "Todd",
      "isAdmin": false,
      "isActive": true,
      "hasChangedPassword": true,
      "id": 20
    },
    "Photo": {
      "bucket": "REDACTED",
      "key": "f627cbcf-7eba-400d-a1f9-dcf1ab457709",
      "contentLength": 0
    },
    "id": 53
  },
  "id": 133
}

【问题讨论】:

  • 在进入表单的途中测试了对象JSON,并且Offerlines存在..
  • 我没有测试过,但我的猜测是在返回 submitOffer 的路上,没有myoffer.id 可以链接回您提交的offerlines。也许添加一个带有报价 id 的隐藏字段。您在打印行中看到了什么?
  • 是的,传递了 myOffer.Id,但 println 给出了前后的差异 - Offerlines passed to form - [149(Test Swag - 0), 150(Swag 2 - 0), 151(Swag 3 - 0), 152(Test swag with an inordinately long name purely for the purposes of testing the rendering of these columns - 0)] Download for swag 1 Swag photo utils.S3Blob@faec98 Requested 1 My offer coming out 148 Todd's tshirts Offerlines []
  • 所以在 submitOffer 方法上,myOffer 的“父”属性似乎已设置,但我的 offerLines 集合被清除了
  • 如果您查看playframework.com/documentation/1.5.x/controllers#binding,它建议使用 POJO 数组,它需要 http 数组表示法。

标签: jpa playframework-1.x


【解决方案1】:

您可以尝试以下方法来设置索引以应对数组吗?

#{list items:myOffer.offerLines, as:'offerline'}

<tr>
<td height="32" width="30"><input type="text" id="offerline.id" name="offerline[${offerline_index-1}].id" value="${offerline.id}"/></td>
<td height="32" width="80">
    ${offerline.swag.Name}
</td>
<td height="32">
    <input class="field" id="offerline.quantity" name="offerline[${offerline_index-1}].quantity" type="number" min="0" max="${offerline.swag.Quantity}" value="${offerline.quantity}"/>
</td>
</tr> 
#{/list}

【讨论】:

  • 没有改变。我将名称更改为myOfffer.offerLines[${offerline_index-1}] 以进一步尝试强制绑定,现在我得到org.hibernate.PersistentObjectException: detached entity passed to persist: models.OfferLine - 称它为一个晚上并现在睡在上面
  • 我假设该错误消息至少意味着您现在在调用 submitOffer 操作时正在接收 Offer 对象上的 offerLines?我认为您现在正在进入超出我深度范围的 JPA 领域(出于这些确切原因,我已将 JPA 抛在后面并开始在 Play2 和 Kotlin Web 框架上手动滚动 SQL)。
  • Fix 似乎将 CascadeType 设置为关系的 Offer 端的 MERGE,基于此答案 - stackoverflow.com/a/29235227/139530
猜你喜欢
  • 2023-03-21
  • 1970-01-01
  • 2013-10-22
  • 1970-01-01
  • 1970-01-01
  • 2013-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多