jpa - how to bind/ unbind a Date type attribute to a DatePicker object -
i have following code:
@fxml private datepicker birthday; //other code private final changelistener<person> personlistener = (value, oldvalue, newvalue) -> { //other code birthday.valueproperty().unbindbidirectional(oldvalue.getbirthday()); //other code };
birthday property of type java.time.localdate, , belongs class person. because use jpa, want not use javafx properties. above code fails compile. compiler's error message is:
error: no suitable method found unbindbidirectional(localdate) birthdaypicker.valueproperty().unbindbidirectional(oldv.getbirthday()); method property.unbindbidirectional(property<localdate>) not applicable (argument mismatch; localdate cannot converted property<localdate>) method objectproperty.unbindbidirectional(property<localdate>) not applicable (argument mismatch; localdate cannot converted property<localdate>)
how can solve problem?
update: person class has followin code:
@entity @table(name = "person") @namedqueries({ @namedquery(name = "person.findall", query = "select p person p"), @namedquery(name = "person.findbyid", query = "select p person p p.id = :id"), @namedquery(name = "person.findbyfirstname", query = "select p person p p.firstname = :firstname"), @namedquery(name = "person.findbylastname", query = "select p person p p.lastname = :lastname"), @namedquery(name = "person.findbymail", query = "select p person p p.mail = :mail"), @namedquery(name = "person.findbybirthday", query = "select p person p p.birthday = :birthday")}) public class person implements serializable { private static final long serialversionuid = 1l; @id @basic(optional = false) @column(name = "id") private integer id; @basic(optional = false) @column(name = "firstname") private string firstname; @column(name = "lastname") private string lastname; @column(name = "mail") private string mail; @column(name = "birthday") @temporal(temporaltype.date) private localdate birthday; public person() { } public person(integer id) { this.id = id; } public person(integer id, string firstname) { this.id = id; this.firstname = firstname; } public integer getid() { return id; } public void setid(integer id) { integer oldid = this.id; this.id = id; listenerlist.firepropertychange("id", oldid, id); } public string getfirstname() { return firstname; } public void setfirstname(string firstname) { string oldfirstname = this.firstname; this.firstname = firstname; listenerlist.firepropertychange("firstname", oldfirstname, firstname); } public string getlastname() { return lastname; } public void setlastname(string lastname) { string oldlastname = this.lastname; this.lastname = lastname; listenerlist.firepropertychange("mail", oldlastname, lastname); } public string getmail() { return mail; } public void setmail(string mail) { string oldmail = this.mail; this.mail = mail; listenerlist.firepropertychange("mail", oldmail, mail); } public localdate getbirthday() { return birthday; } public void setbirthday(localdate birthday) { localdate oldbirthday = this.birthday; this.birthday = birthday; listenerlist.firepropertychange("birthday", id, birthday); } @override public int hashcode() { int hash = 0; hash += (id != null ? id.hashcode() : 0); return hash; } @override public boolean equals(object object) { // todo: warning - method won't work in case id fields not set if (!(object instanceof person)) { return false; } person other = (person) object; if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { return false; } return true; } @override public string tostring() { return "jpa.entities.person[ id=" + id + " ]"; } @transient final private propertychangesupport listenerlist = new propertychangesupport(this); public void addpropertychangelistener(propertychangelistener listener) { listenerlist.addpropertychangelistener(listener); } public void removepropertychangelistener(propertychangelistener listener) { listenerlist.removepropertychangelistener(listener); } }
the controller class:
public class personcontroller implements initializable { @fxml private tableview<person> personstable; @fxml private tablecolumn<person, integer> idcolumn; @fxml private tablecolumn<person, string> firstcolumn; @fxml private tablecolumn<person, string> lastcolumn; @fxml private tablecolumn<person, localdate> birthdaycolumn; @fxml private tablecolumn<person, string> mailcolumn; @fxml private textfield id; @fxml private textfield firstname; @fxml private textfield lastname; @fxml private textfield mail; @fxml private datepicker birthdaypicker; private entitymanagerfactory emf; private entitymanager em; private observablelist<person> data; private localdate birthday; /** * initializes controller class. */ @override public void initialize(url url, resourcebundle rb) { emf = persistence.createentitymanagerfactory("persistencetest"); em = emf.createentitymanager(); data = fxcollections.observablearraylist(); birthdaypicker.setonaction((actionevent evnt) -> { birthday = birthdaypicker.getvalue(); }); personstable.getselectionmodel().selecteditemproperty().addlistener(personlistener); configurecolumn(); populate(); } @fxml private void addperson(actionevent event) { em.gettransaction().begin(); person p = new person(integer.parseint(id.gettext()), firstname.gettext()); p.setlastname(lastname.gettext()); p.setbirthday(birthdaypicker.getvalue()); p.setmail(mail.gettext()); em.persist(p); data.add(p); em.gettransaction().commit(); } @fxml private void saveperson(actionevent event) { } @fxml private void deleteperson(actionevent event) { em.gettransaction().begin(); person p = personstable.getselectionmodel().selecteditemproperty().getvalue(); data.remove(p); em.remove(p); em.gettransaction().commit(); } private void populate() { typedquery<person> query = em.createquery( "select e person e", jpa.entities.person.class); list<person> list = query.getresultlist(); data.addall(list); personstable.setitems(data); } private void configurecolumn() { idcolumn.setcellvaluefactory(new propertyvaluefactory<person, integer>("id")); firstcolumn.setcellvaluefactory(new propertyvaluefactory<person, string>("firstname")); lastcolumn.setcellvaluefactory(new propertyvaluefactory<person, string>("lastname")); birthdaycolumn.setcellvaluefactory(new propertyvaluefactory<person, localdate>("birthday")); birthdaycolumn.setcellfactory(p -> { return new tablecell<person, localdate>() { @override protected void updateitem(localdate item, boolean empty) { super.updateitem(item, empty); if (item == null || empty) { settext(null); } else { final datetimeformatter format = datetimeformatter.ofpattern("dd/mm/yyyy"); settext(item.format(format)); } } }; }); mailcolumn.setcellvaluefactory(new propertyvaluefactory<person, string>("mail")); } private final changelistener<person> personlistener = (value, oldv, newv) -> { if (oldv != null) { id.textproperty().unbindbidirectional(oldv.getid()); firstname.textproperty().unbindbidirectional(oldv.getfirstname()); lastname.textproperty().unbindbidirectional(oldv.getlastname()); birthdaypicker.valueproperty().unbindbidirectional(oldv.getbirthday()); // error mail.textproperty().unbindbidirectional(oldv.getmail()); } if (newv != null) { try { id.textproperty().bindbidirectional(javabeanintegerpropertybuilder.create().bean(newv).name("id").build(), new numberstringconverter()); firstname.textproperty().bindbidirectional(javabeanstringpropertybuilder.create().bean(newv).name("firstname").build()); lastname.textproperty().bindbidirectional(javabeanstringpropertybuilder.create().bean(newv).name("lastname").build()); birthdaypicker.valueproperty().bindbidirectional(); // error mail.textproperty().bindbidirectional(javabeanstringpropertybuilder.create().bean(newv).name("mail").build()); } catch (nosuchmethodexception e) { system.out.println("erreur : " + e.getmessage()); } } }; }
the easiest thing use javafx properties in person
class. works jpa long use "property access" instead of "field access". i.e.:
public class person { private final stringproperty firstname = new simplestringproperty(); public stringproperty firstnameproperty() { return firstname ; } @basic(optional = false) @column(name = "firstname") public final string getfirstname() { return firstnameproperty().get(); } public final void setfirstname(string firstname) { firstnameproperty().set(firstname); } private final objectproperty<localdate> birthday = new simpleobjectproperty<>(); public objectproperty<localdate> birthdayproperty() { return birthday ; } @column(name="birthday") @temporal(temporaltype.date) public localdate getbirthday() { return birthdayproperty().get(); } public void setbirthday(localdate birthday) { birthdayproperty().set(birthday); } // etc }
(note hibernate still objects final
get
, set
methods, have make methods non-final if use hibernate, less ideal. if use jpa-compliant orm shouldn't cause problem.)
if cannot use javafx properties in entity reason, can manage binding javabeanobjectproperty
. code along following lines should work:
public class personcontroller { private javabeanobjectproperty birthdaypropertyadapter ; // ... private final changelistener<person> personlistener = (value, oldv, newv) -> { if (oldv != null) { id.textproperty().unbindbidirectional(oldv.getid()); firstname.textproperty().unbindbidirectional(oldv.getfirstname()); lastname.textproperty().unbindbidirectional(oldv.getlastname()); mail.textproperty().unbindbidirectional(oldv.getmail()); } if (birthdaypropertyadapter != null) { birthdaypicker.valueproperty().unbindbidirectional(birthdaypropertyadapter); } if (newv != null) { try { id.textproperty().bindbidirectional(javabeanintegerpropertybuilder.create().bean(newv).name("id").build(), new numberstringconverter()); firstname.textproperty().bindbidirectional(javabeanstringpropertybuilder.create().bean(newv).name("firstname").build()); lastname.textproperty().bindbidirectional(javabeanstringpropertybuilder.create().bean(newv).name("lastname").build()); mail.textproperty().bindbidirectional(javabeanstringpropertybuilder.create().bean(newv).name("mail").build()); birthdaypropertyadapter = javabeanobjectpropertybuilder.create() .bean(newv) .name("birthday") .build(); birthdaypicker.valueproperty().bindbidirectional(birthdaypropertyadapter); } catch (nosuchmethodexception e) { system.out.println("erreur : " + e.getmessage()); } } };
Comments
Post a Comment