java - Solved - Hibernate (JPA) - Weird update for no reason -
i have application accesses oracle-db storing contacts in table. create, read , update working fine. delete won't work weird reason.
when start application, load current contacts db , put them in javafx-table. let hibernate show me it's sql , thats did until here. did once select. if directly start delete contacts works fine 3-4 contacts , error tells me, hibernate tried run update-statement used id null. why hibernate doing this?
this total nonsense. double , tripple checked , there no db-action running between select statement , deletes. why hibernate update in middle of without reason when tell delete?
here see coude , information can possibly need understand situation
public void refresh() { list<organisationcontact> allcontacts = entitystore.orga_con_repo .readallwithdetails(); contacttable.getitems().setall(allcontacts); } this method in repository
@override public list<organisationcontact> readallwithdetails() { try { jpajinqstream<contact> stream = getstreamfortable(contact.class); list<organisationcontact> organisationcontactlist = new arraylist<organisationcontact>(); try { stream.foreach(con -> organisationcontactlist .add(new organisationcontact(con))); } catch (javax.persistence.persistenceexception exception) { noreplyfromdatabaseexception.showerrordialog(); throw new noreplyfromdatabaseexception(exception); } stream.close(); return organisationcontactlist; } catch (javax.persistence.persistenceexception exception) { noreplyfromdatabaseexception.showerrordialog(); throw new noreplyfromdatabaseexception(exception); } } this method in abstract repository normal repository using
protected <tableentity>jpajinqstream<tableentity> getstreamfortable(final class<tableentity> pentityclass) { if (this.manager != null && this.factory != null && this.provider != null) { if (this.manager.isopen() && this.factory.isopen()) { return this.provider.streamall(this.manager, pentityclass); } } return null; } manager instance of entitymananger
factory instance of entitymanagerfactory
provider instance of jinqjpastreamprovider
this code executed when delete contact
@fxml public void ondelete() { entitystore.orga_con_repo.delete(entitystore.current_contact); if (!unitofwork.closetransaction(entitystore.orga_con_repo, true)) { // error occured } // ignore stuff entitystore.current_contact = null; modemanager.clearmode(); modemanager.refreshtable(); } orga_con_repo repository above
unitofwork knows existing repositories (in case 1 exists) , handles it's transactions
this unitofwork class
public final class unitofwork { private static final map<abstractrepository<?>, entitymanager> units = new hashmap<abstractrepository<?>, entitymanager>(); private unitofwork() { } /* public */ /** * executes commit/rollback , closes transaction passed * repository. * * @param prepository * repository transaction belongs to. * @param pcommit * if parameter <code>true</code>, transaction * commited before closing. if <code>false</code>, * transaction rolled before closing. * @return true if transaction has been closed successfully, false if error occured while closing or manager null */ public synchronized static boolean closetransaction( final abstractrepository<?> prepository, final boolean pcommit) { entitymanager manager = units.get(prepository); if (manager != null) { try { entitytransaction t = manager.gettransaction(); if (t.isactive()) { if (pcommit) { t.commit(); } else { t.rollback(); } } units.remove(prepository); return true; } catch (persistenceexception pexception) { prepository.resetmanager(false); units.remove(prepository); // todo: log , throw } } return false; } /* protected */ /** * starts new transaction in new unit of work. * * @param prepository * repository transaction belongs to. * @param pmanager * entitymanager of passed repository. * @return <code>true</code> if transaction has been started * successfully, <code>false</code> if manager closed or 1 * of parameters null. */ protected synchronized static boolean begintransaction( final abstractrepository<?> prepository, final entitymanager pmanager) { if (prepository != null || pmanager != null) { if (pmanager.isopen()) { if (!units.containskey(prepository)) { entitytransaction t = pmanager.gettransaction(); if (!t.isactive()) { t.begin(); } units.put(prepository, pmanager); } return true; } } return false; } }
this delete method of repository
@override public boolean delete(organisationcontact pentity) { contact contactentity = pentity.getcontact(); return remove(contactentity); } which using method of abstract repository
protected boolean remove(final object pentity) { if (this.canmanagerexecute(pentity)) { if (this.begintransaction()) { this.manager.remove(pentity); return true; } } return false; } private boolean canmanagerexecute(final object pentity) { if (this.manager != null && pentity != null) { return this.manager.isopen(); } return false; } which using hibernate.
, entities
@entity @table(schema = "reskonverw") public class contact { @column(name = "phonenumber") private string phonenumber; @column(name = "firstname") private string firstname; @column(name = "surname") private string surname; @column(name = "email") private string email; @id @generatedvalue(strategy = generationtype.sequence) @column(name="id") private int id; @manytoone(cascade = cascadetype.all) private organisation organisation; @manytoone(cascade = cascadetype.all) private role role; public contact() { } public contact(string phonenumber, string firstname, string surname, string email, organisation organisation, role role) { this.phonenumber = phonenumber; this.firstname = firstname; this.surname = surname; this.email = email; this.organisation = organisation; this.role = role; } public string getphonenumber() { return phonenumber; } public void setphonenumber(string phonenumber) { this.phonenumber = phonenumber; } public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public string getsurname() { return surname; } public void setsurname(string surname) { this.surname = surname; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } public organisation getorganisation() { return organisation; } public void setrole(final role prole) { role = prole; } public role getrole() { return role; } public void setorganisation(organisation organisation) { this.organisation = organisation; } public int getid() { return id; } public void setid(int id) { this.id = id; } @override public string tostring() { return new stringbuilder(surname).append(", ").append(firstname) .tostring(); } } @entity @table(schema = "reskonverw") public class country { @id @generatedvalue(strategy = generationtype.sequence) private int id; private string name; public country() { } public country(string cname) { this.name = cname; } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } @override public string tostring() { return name; } } @entity @table(schema = "reskonverw") public class organisation { @id @generatedvalue(strategy = generationtype.sequence) private int id; private string name; private string zipcode; private string housenumber; private string city; private string street; @manytoone(cascade = cascadetype.all) private country country; public organisation() { } public organisation(string name, string zipcode, string housenumber, string city, string street, country country) { this.name = name; this.zipcode = zipcode; this.housenumber = housenumber; this.city = city; this.street = street; this.country = country; } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getzipcode() { return zipcode; } public void setzipcode(string zipcode) { this.zipcode = zipcode; } public string gethousenumber() { return housenumber; } public void sethousenumber(string housenumber) { this.housenumber = housenumber; } public string getcity() { return city; } public void setcity(string city) { this.city = city; } public string getstreet() { return street; } public void setstreet(string street) { this.street = street; } public country getcountry() { return country; } public void setcountry(country country) { this.country = country; } @override public string tostring() { return name; } } @entity @table(schema = "reskonverw") public class role { @id @generatedvalue(strategy = generationtype.sequence) private int id; private string description; public role() { } public role(string rdescription) { this.description = rdescription; } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getdescription() { return description; } public void setdescription(string description) { this.description = description; } @override public string tostring() { return description; } } my session bean used displayed in javafx table
public class organisationcontact { private contact contact; public organisationcontact(contact contact) { this.contact = contact; } /* entities */ public organisation getorganisation() { return contact.getorganisation(); } public void setorganisation(organisation organisation) { contact.setorganisation(organisation); } public contact getcontact() { return contact; } public void setcontact(contact contact) { this.contact = contact; } public role getrole() { return contact.getrole(); } public void setrole(final role prole) { contact.setrole(prole); } public country getcountry() { return contact.getorganisation().getcountry(); } public void setcountry(final country pcountry) { contact.getorganisation().setcountry(pcountry); } /* entitystats */ // organisation public string getorganisationname() { return contact.getorganisation().getname(); } public void setorganisationname(final string pname) { contact.getorganisation().setname(pname); } public string getorganisationzipcode() { return contact.getorganisation().getzipcode(); } public void setorganisationzipcode(final string pzipcode) { contact.getorganisation().setzipcode(pzipcode); } public string getorganisationhousenumber() { return contact.getorganisation().gethousenumber(); } public void setorganisationhousenumber(final string phousenumber) { contact.getorganisation().sethousenumber(phousenumber); } public string getorganisationcity() { return contact.getorganisation().getcity(); } public void setorganisationcity(final string pcity) { contact.getorganisation().setcity(pcity); } public string getorganisationstreet() { return contact.getorganisation().getstreet(); } public void setorganisationstreet(final string pstreet) { contact.getorganisation().setstreet(pstreet); } // contact public string getfirstname() { return contact.getfirstname(); } public void setfirstname(final string pfirstname) { contact.setfirstname(pfirstname); } public string getsurname() { return contact.getsurname(); } public void setsurname(final string psurname) { contact.setsurname(psurname); } public string getemail() { return contact.getemail(); } public void setemail(final string pemail) { contact.setemail(pemail); } public string getphonenumber() { return contact.getphonenumber(); } public void setphonenumber(final string pphonenumber) { contact.setphonenumber(pphonenumber); } // country public string getorganisationcountryname() { return contact.getorganisation().getcountry().getname(); } // role public string getroledescription() { return contact.getrole().getdescription(); } public void setroledescription(final string pdescription) { contact.getrole().setdescription(pdescription); } } edit: here sql hibernate prints on console first when select @ programmstart:
hibernate: select * ( select contact0_.id id1_0_, contact0_.email email2_0_, contact0_.firstname firstname3_0_, contact0_.organisation_id organisation_id6_0_, contact0_.phonenumber phonenumber4_0_, contact0_.role_id role_id7_0_, contact0_.surname surname5_0_ reskonverw.contact contact0_ ) rownum <= ? hibernate: select organisati0_.id id1_2_0_, organisati0_.city city2_2_0_, organisati0_.country_id country_id7_2_0_, organisati0_.housenumber housenumber3_2_0_, organisati0_.name name4_2_0_, organisati0_.street street5_2_0_, organisati0_.zipcode zipcode6_2_0_, country1_.id id1_1_1_, country1_.name name2_1_1_ reskonverw.organisation organisati0_ left outer join reskonverw.country country1_ on organisati0_.country_id=country1_.id organisati0_.id=? hibernate: select role0_.id id1_3_0_, role0_.description description2_3_0_ reskonverw.role role0_ role0_.id=? here sql hibernate prints on console when delete @ buttonclick (select because update entities afterwards because there multiple clients):
hibernate: delete reskonverw.contact id=? hibernate: select * ( select contact0_.id id1_0_, contact0_.email email2_0_, contact0_.firstname firstname3_0_, contact0_.organisation_id organisation_id6_0_, contact0_.phonenumber phonenumber4_0_, contact0_.role_id role_id7_0_, contact0_.surname surname5_0_ reskonverw.contact contact0_ ) rownum <= ? here sql hibernate prints on console when update instead of delete @ buttonclick (no select because crashes before):
hibernate: update reskonverw.contact set email=?, firstname=?, organisation_id=?, phonenumber=?, role_id=?, surname=? id=? jul 08, 2015 8:05:12 org.hibernate.engine.jdbc.spi.sqlexceptionhelper logexceptions warn: sql error: 1407, sqlstate: 72000 jul 08, 2015 8:05:12 org.hibernate.engine.jdbc.spi.sqlexceptionhelper logexceptions error: ora-01407: aktualisieren von ("reskonverw"."contact"."organisation_id") zu null nicht möglich for not german ppl, 'error: ora-01407: aktualisieren von ("reskonverw"."contact"."organisation_id") zu null nicht möglich' means 'error - setting resconverw.contact.organisation_id null not possible
contact has foreginkey organisation. linked id of organisation. when delete contact, hibernate tries set foreginkey null before deleting it. not reason couldn't figure out yet. in db had setup constraint prevent foregin key becomeing null. , why update failed , got exception. removed constraint , since working.
thanks help
Comments
Post a Comment