我有一个相当简单的联系表格,收集基本的联系信息,连同一个(S)网站列表。
接触模型与网站表有FK关系。当我绑定表单并保存时,我也希望将任何网站条目保存在网站表中。
表单的绑定是与表的一对一绑定,还是一个表单类绑定到一个模型,或者绑定时表单也可以访问存在FK的模型?
接触模型:
@Entity
public class Contact extends Model {
public static Model.Finder<Long, Contact> finder = new Model.Finder<Long, Contact>(Long.class, Contact.class);
public static Finder<String,Contact> find = new Finder<String,Contact>(String.class, Contact.class);
public Contact(String prefix, String firstName, String lastName, String company, String title, String primaryEmail, String primaryPhone, Collection<Website> websites){
this.prefix = prefix;
this.firstName = firstName;
this.lastName = lastName;
this.company = company;
this.title = title;
this.primaryEmail = primaryEmail;
this.primaryPhone = primaryPhone;
this.websites = new LinkedList<Website>(websites);
}
@Id
public Long id;
public String prefix;
@Constraints.Required
@Constraints.MinLength(2)
@Constraints.MaxLength(25)
public String firstName;
@Constraints.Required
@Constraints.MinLength(2)
@Constraints.MaxLength(25)
public String lastName;
public String company;
public String title;
public String ledes;
@Constraints.Email
@Column(columnDefinition = "varchar(50)")
public String primaryEmail;
@Column(columnDefinition = "varchar(30)")
public String primaryPhone;
@Formats.NonEmpty
@Column(columnDefinition = "integer default 1")
@OneToOne
public ContactType contactType;
@ManyToOne
public Firm firm;
@ManyToMany(cascade = CascadeType.ALL, mappedBy = "contact")
public List<Matter> matter = new ArrayList<>();
@OneToMany(mappedBy = "contact")
public List<Child> child = new ArrayList<>();
@OneToMany(mappedBy = "contact")
public List<Location> location = new ArrayList<>();
@OneToMany(mappedBy = "contact")
public List<Phone> phone = new ArrayList<>();
@OneToMany(mappedBy = "contact")
public List<Email> email = new ArrayList<>();
@OneToMany(mappedBy = "contact")
public List<Website> websites = new ArrayList<>();
@OneToMany(mappedBy = "contact")
public List<InstantMessenger> messenger = new ArrayList<>();
@Column(name = "created_at")
public Date createdAt;
@Column(name = "updated_at")
public Date updatedAt;
@Override
public void save() {
createdAt();
super.save();
}
@Override
public void update() {
updatedAt();
super.update();
}
@PrePersist
void createdAt() {
this.createdAt = this.updatedAt = new Date();
}
@PreUpdate
void updatedAt() {
this.updatedAt = new Date();
}
} 网站模型:
@Entity
public class Website extends Model {
public Website() {
}
public Website(String website, String websiteType) {
this.website = website;
this.websiteType = websiteType;
}
@Id
public Long id;
@Required
@URL
@MaxLength(100)
@Column(columnDefinition = "varchar(100) not null")
public String website;
@Required
@MaxLength(20)
@Column(columnDefinition = "varchar(20) not null")
public String websiteType;
@Column(columnDefinition = "integer default 1")
public Boolean isActive;
@Column(name = "created_at")
public Date createdAt;
@Column(name = "updated_at")
public Date updatedAt;
@Override
public void save() {
createdAt();
super.save();
}
@Override
public void update() {
updatedAt();
super.update();
}
@PrePersist
void createdAt() {
this.createdAt = this.updatedAt = new Date();
}
@PreUpdate
void updatedAt() {
this.updatedAt = new Date();
}
@ManyToOne()
public Contact contact;
}联系人控制器:
public class Contacts extends Controller {
@Security.Authenticated(Secured.class)
public Result allContacts() {
return ok(contactsAll.render(
Contact.finder.where().gt("id", 0).findList()
));
}
@Security.Authenticated(Secured.class)
public Result newContact() {
return ok(contactNew.render(form(Contact.class)));
}
@Security.Authenticated(Secured.class)
public Result addContact() {
play.data.Form<Contact> contactForm = form(Contact.class).bindFromRequest();
if (contactForm.hasErrors()) {
flash("error", "Please correct the form below.");
return ok(contactNew.render(contactForm));
} else {
// Bind form and save
Contact contact = contactForm.get();
contact.save();
// if (contactForm.get().websites != null) {
// new Website(contactForm.get().websites).save();
// }
flash("success", String.format("Successfully added %s", contactForm.get().firstName + ' ' + contactForm.get().lastName));
return redirect(
routes.Contacts.allContacts()
);
}
}
}发布于 2015-12-21 16:50:14
您可以使用:这个guy explains, how it works
@OneToOne(cascade=CascadeType.PERSIST)注释。
这样也可以保存页面(至少在我的应用程序中是这样的)
如果这行不通的话。
您可以这样访问该页面:
Contact.webpage 这意味着,你也可以:
Contact.webpage = contactForm.get().websites
contact.save();在设置网页之前,您调用了保存操作。
https://stackoverflow.com/questions/34359436
复制相似问题