我创建了一个简单的表单来添加商店,每个商店都与会员 Shop.java模型
有很多关系 package models;
@Entity
public class Shop extends Model {
@Id@SequenceGenerator(name = "shop_gen", sequenceName = "shop_id_seq", allocationSize = 1)@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "shop_gen")@Column(name = "id")
public Long id;
@Required
public String name;
@Required
public String addressLine1;
public String addressLine2;
public String addressLine3;
@Required
public String city;
@Required
public String town;
@Required
public String phoneNumber;
@ManyToOne@JoinColumn(name = "email",
insertable = false, updatable = false,
nullable = false)@Required
public Member email;
public static Model.Finder < Long, Shop > find = new Model.Finder(Long.class, Shop.class);
public static Shop create(Shop shop) {
shop.save();
return shop;
}
}
Member.java模型
@Entity
public class User extends Model {
@Id
@Email
@OneToMany(cascade = {CascadeType.ALL})
@JoinColumn(name = "email")
public String email;
@Required
public String password;
@Required
public String firstName;
@Required
public String lastName;
}
ShopController.java
package controllers;
public class ShopController extends Controller {
static Form<Shop> shopForm = Form.form(Shop.class);
public static Result submit()
{
Form<Shop> filledForm = shopForm.bindFromRequest();
if(filledForm.hasErrors()) {
return badRequest(views.html.shop.create.render(filledForm, Member.names()));
}
else {
Shop shop = filledForm.get();
Shop.create(shop);
return redirect(routes.ProductController.blank());
}
}
}
但是当我提交表单来添加商店时,如果条件总是正确的话,它会加载带有条目填充的相同页面,我使用的是postgresql,这个问题在我以前的数据库mysql中没有出现。
有人能帮助我吗?
答案 0 :(得分:0)
@Required
上的Shop.email
注释可能会触发导致您的操作每次都返回HTTP 400(错误请求)的验证错误。我认为在您致电null
后,该字段将为bindFromRequest
。额外的日志记录或调试可用于确认这一点。
即使用于保存商店的传入HTTP POST请求包含名为 email 的字段,Play也不知道如何将此字段的值(字符串)转换为{{1对象。您必须从此字段中删除验证,或使用custom data binder进一步注释,以告知Play如何执行字符串到成员的转换。
作为额外的观察,我还会质疑您在其他表单类中声明以下字段:
Member
@Id
@Email
@OneToMany(cascade={CascadeType.ALL})
@JoinColumn(name="email")
public String email;
注释表示此实体的实例有许多电子邮件地址。字段的类型(String)表示该实体的实例具有一个电子邮件地址。这两个陈述是矛盾的。