Reputation: 1275
I have a fairly simple contact form that collects basic contact information along with a list of website(s).
The Contact model has a FK relation to a Website table. When I bind the form and save I would also like to save any website entries in the Website table.
Is the binding of a form one to one with a table, 1 form class binds to 1 model or can a form when bound also access models where a FK exists?
Contact Model:
@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();
}
}
Website Model:
@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;
}
Contacts Controller:
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()
);
}
}
}
Upvotes: 0
Views: 59
Reputation: 2302
You can use the: This guy explains, how it works
@OneToOne(cascade=CascadeType.PERSIST)
Annotation.
This will save the page as well ( at least in my app it does )
If this doesn't work.
You can access the page like so:
Contact.webpage
That means, that you could also do:
Contact.webpage = contactForm.get().websites
contact.save();
You called the save action, before you set the webpage.
Upvotes: 1