问题
I have created a simple form to add a shop, every shop has a manytoone relation with the member Shop.java model
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 model
@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());
}
}
}
but when I am submitting form to add a shop It loads the same page with the entry filled means if condition is always true ,I am using postgresql and this problem didn't occured in my previous database mysql.
Can anybody help me?
回答1:
The @Required
annotation on Shop.email
is probably triggering the validation error that is causing your action to return an HTTP 400 (bad request) each time. I think that the field will be null
following your call to bindFromRequest
. Extra logging or debugging can be used to confirm this.
Even if an incoming HTTP POST request to save a shop contains a field with name email, Play doesn't know how to turn the value of this field (a String) into a Member
object. You have to either remove the validation from this field, or annotate it further with a custom data binder that tells Play how to perform the String to Member conversion.
As an extra observation, I would also question the declaration of the following field in your other form class:
@Id
@Email
@OneToMany(cascade={CascadeType.ALL})
@JoinColumn(name="email")
public String email;
The OneToMany
annotation says that an instance of this entity has many email addresses. The type of the field (String) says that an instance of this entity has one email address. These two statements are contradictory.
来源:https://stackoverflow.com/questions/22879684/if-condition-always-routing-towards-haserror-condition-while-submitting-a-form