绑定表单并将数据保存到2个表

时间:2015-12-18 15:58:03

标签: playframework playframework-2.0

我有一个相当简单的联系表格,收集基本联系信息以及网站列表。

Contact模型与Website表格具有FK关系。当我绑定表单并保存时,我还想保存网站表中的任何网站条目。

表单一对一绑定表,1表单类绑定到1个模型,绑定时表单是否也可以访问存在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()
        );
    }
}

}

1 个答案:

答案 0 :(得分:1)

你可以使用: 这guy explains, how it works

@OneToOne(cascade=CascadeType.PERSIST)

诠释。

这也会保存页面(至少在我的应用程序中保存)

如果这不起作用。

您可以像这样访问该页面:

Contact.webpage 

这意味着,你也可以这样做:

Contact.webpage = contactForm.get().websites
contact.save();

在设置网页之前,您调用了保存操作。