Java Hibernate delete Object -
today did experiments hibernate. first of there no deeper sense behind program. wanted try framework. planed following db tables:
- car (auto)
- driver (fahrer)
- wohnung (flat)
- guest (fahrgast)
with following bidirectional mappings:
- driver – flat onetoone
- driver – car onetomany
- car – guest manytomany
after preparing single classes wrote worker insert demodata. point works expected. remove 1 of drivers. hibernate tells me, re-saved guest. unfortunately don’t understand why. expected fine after removing driver driver collection of corresponding cars.
class car
package mycode; import java.time.localdate; import java.util.arraylist; import java.util.list; import javax.persistence.cascadetype; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.manytomany; import javax.persistence.onetomany; import javax.persistence.table; @entity @table(name="auto") public class auto { @id @generatedvalue private int id; @column(name="nummernschild", nullable = false) private string nummernschild; @onetomany(cascade=cascadetype.all, mappedby="auto") private list<fahrer>fahrers = new arraylist<fahrer>(); @manytomany(cascade=cascadetype.all) private list<fahrgast>fahrgasts = new arraylist<fahrgast>(); public list<fahrgast> getfahrgasts() { return fahrgasts; } public void setfahrgasts(list<fahrgast> fahrgasts) { this.fahrgasts = fahrgasts; } public list<fahrer> getfahrers() { return fahrers; } public void setfahrers(list<fahrer> fahrers) { this.fahrers = fahrers; } private localdate kaufdatum; public localdate getkaufdatum() { return kaufdatum; } public void setkaufdatum(localdate kaufdatum) { this.kaufdatum = kaufdatum; } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getnummernschild() { return nummernschild; } public void setnummernschild(string nummernschild) { this.nummernschild = nummernschild; } }
class driver
package mycode; import javax.persistence.cascadetype; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.joincolumn; import javax.persistence.manytoone; import javax.persistence.onetoone; import javax.persistence.table; @entity @table(name="fahrer") public class fahrer { @id @generatedvalue() private int id; private string vorname, nachname; private int alter; @onetoone (cascade=cascadetype.all) @joincolumn(name="id") private wohnung wohnung; @manytoone(cascade=cascadetype.all) private auto auto; public auto getauto() { return auto; } public void setauto(auto auto) { this.auto = auto; } public wohnung getwohnung() { return wohnung; } public void setwohnung(wohnung wohnung) { this.wohnung = wohnung; } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getvorname() { return vorname; } public void setvorname(string vorname) { this.vorname = vorname; } public string getnachname() { return nachname; } public void setnachname(string nachname) { this.nachname = nachname; } public int getalter() { return alter; } public void setalter(int alter) { this.alter = alter; } }
class flat
package mycode; import javax.persistence.cascadetype; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.joincolumn; import javax.persistence.onetoone; import javax.persistence.table; import org.hibernate.annotations.genericgenerator; import org.hibernate.annotations.parameter; @entity @table(name="wohnung") public class wohnung { @id @generatedvalue(generator = "newgenerator") @genericgenerator(name="newgenerator", strategy="foreign" , parameters= {@parameter(value="fahrer", name="property")}) private int id; @column(nullable=false) private string ort, straße; @onetoone(cascade=cascadetype.all) @joincolumn(name="id") private fahrer fahrer; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getort() { return ort; } public void setort(string ort) { this.ort = ort; } public string getstraße() { return straße; } public void setstraße(string straße) { this.straße = straße; } public fahrer getfahrer() { return fahrer; } public void setfahrer(fahrer fahrer) { this.fahrer = fahrer; } }
class guest
package mycode; import java.util.arraylist; import java.util.list; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.manytomany; import javax.persistence.table; @entity @table(name="fahrgast") public class fahrgast { @id @generatedvalue private int id; @column(nullable=false) private int kundennummmer; private string vornname, nachname; @manytomany(mappedby="fahrgasts") private list<auto>autos = new arraylist<auto>(); public list<auto> getautos() { return autos; } public void setautos(list<auto> autos) { this.autos = autos; } public int getid() { return id; } public void setid(int id) { this.id = id; } public int getkundennummmer() { return kundennummmer; } public void setkundennummmer(int kundennummmer) { this.kundennummmer = kundennummmer; } public string getvornname() { return vornname; } public void setvornname(string vornname) { this.vornname = vornname; } public string getnachname() { return nachname; } public void setnachname(string nachname) { this.nachname = nachname; } }
class worker
package mycode; import java.time.localdate; import java.util.arraylist; import java.util.list; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.boot.registry.standardserviceregistrybuilder; import org.hibernate.cfg.configuration; public class worker { private session session; private sessionfactory sf; public static void main(string[] args) { worker worker = new worker(); worker.work(); } private void init() { configuration configuration = new configuration().configure(); sf = configuration.buildsessionfactory(); session = sf.opensession(); } private void work() { init(); auto auto = new auto(); auto.setnummernschild("hh:mk:"+1); localdate ld = localdate.now(); auto.setkaufdatum(ld); session.begintransaction(); (int i=0; i<10; i++) { auto = new auto(); auto.setnummernschild("hh:mk:"+i); ld = localdate.now(); auto.setkaufdatum(ld); auto auto2 = new auto(); auto2.setnummernschild("hh:mk:"+i); ld = localdate.now(); auto2.setkaufdatum(ld); //auto.setid(i); fahrer fahrer = new fahrer(); fahrer.setvorname("hans"); fahrer.setnachname("huber"); fahrer fahrer2 = new fahrer(); fahrer2.setvorname("anna"); fahrer2.setnachname("schmidt"); double temp = math.random(); int alter = (int)(temp*50); fahrer.setalter(alter); fahrer2.setalter(alter); fahrer.setauto(auto); fahrer2.setauto(auto2); wohnung wohnung = createwohnung(i); wohnung.setfahrer(fahrer); fahrer.setwohnung(wohnung); wohnung wohnung2 = createwohnung(i*10); fahrer2.setwohnung(wohnung2); wohnung2.setfahrer(fahrer2); auto.getfahrers().add(fahrer); auto2.getfahrers().add(fahrer2); double zufall = math.random()*100; int zu = (int)zufall; (int z=0; z<zu; z++) { fahrgast fahrgast = new fahrgast(); fahrgast.setvornname("hans"+z); fahrgast.setnachname("dampf"+z); double kundennummer = math.random()*10000; fahrgast.setkundennummmer((int)kundennummer); fahrgast.getautos().add(auto); fahrgast.getautos().add(auto2); auto.getfahrgasts().add(fahrgast); auto2.getfahrgasts().add(fahrgast); } // session.save(fahrer); // session.save(fahrer2); session.save(auto); session.save(auto2); } fahrer abfrage = session.get(fahrer.class, 2); list<fahrer>fahrers = session.createcriteria(fahrer.class).list(); list<fahrer>tobedeletet = new arraylist<fahrer>(); (fahrer aktuell : fahrers) { auto car = aktuell.getauto(); list<fahrer>cardriver = car.getfahrers(); fahrer temp = null; (fahrer driver: cardriver) { if (driver.getid()==abfrage.getid()) { tobedeletet.add(aktuell); temp = driver; } } cardriver.remove(temp); session.update(car); } (fahrer aktuell : tobedeletet) { session.remove(aktuell); } system.out.println(abfrage.getvorname()+ " "+abfrage.getnachname()); session.gettransaction().commit(); session.close(); sf.close(); } private wohnung createwohnung(int i) { wohnung wohnung = new wohnung(); wohnung.setort("bla"+i); wohnung.setstraße("blub"+i); return wohnung; } }
finally configuration file
<?xml version='1.0' encoding='utf-8'?> <hibernate-configuration> <session-factory> <!-- database connection settings --> <property name="connection.driver_class">org.postgresql.driver</property> <property name="connection.url">jdbc:postgresql://192.168.2.252:5432/test</property> <property name="connection.username">postgres</property> <property name="connection.password">postgres</property> <!-- jdbc connection pool (use built-in) --> <property name="connection.pool_size">1</property> <!-- sql dialect --> <property name="hibernate.dialect">org.hibernate.dialect.postgresqldialect</property> <!-- enable hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- disable second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.nocacheprovider</property> <!-- echo executed sql stdout --> <property name="show_sql">true</property> <!-- drop , re-create database schema on startup --> <property name="hbm2ddl.auto">create</property> <mapping class="mycode.auto"/> <mapping class="mycode.fahrer"/> <mapping class="mycode.wohnung"/> <mapping class="mycode.fahrgast"/> </session-factory>
can tell me, how delete 1 of drivers?
the error message: error: hhh000346: error during managed flush [deleted object re-saved cascade (remove deleted object associations): [mycode.fahrgast#3]] exception in thread "main" javax.persistence.entitynotfoundexception: deleted object re-saved cascade (remove deleted object associations): [mycode.fahrgast#3] @ org.hibernate.internal.exceptionconverterimpl.convert(exceptionconverterimpl.java:126) @ org.hibernate.internal.exceptionconverterimpl.convert(exceptionconverterimpl.java:155) @ org.hibernate.internal.exceptionconverterimpl.convert(exceptionconverterimpl.java:162) @ org.hibernate.internal.sessionimpl.doflush(sessionimpl.java:1441) @ org.hibernate.internal.sessionimpl.managedflush(sessionimpl.java:491) @ org.hibernate.internal.sessionimpl.flushbeforetransactioncompletion(sessionimpl.java:3201) @ org.hibernate.internal.sessionimpl.beforetransactioncompletion(sessionimpl.java:2411) @ org.hibernate.engine.jdbc.internal.jdbccoordinatorimpl.beforetransactioncompletion(jdbccoordinatorimpl.java:467) @ org.hibernate.resource.transaction.backend.jdbc.internal.jdbcresourcelocaltransactioncoordinatorimpl.beforecompletioncallback(jdbcresourcelocaltransactioncoordinatorimpl.java:146) @ org.hibernate.resource.transaction.backend.jdbc.internal.jdbcresourcelocaltransactioncoordinatorimpl.access$100(jdbcresourcelocaltransactioncoordinatorimpl.java:38) @ org.hibernate.resource.transaction.backend.jdbc.internal.jdbcresourcelocaltransactioncoordinatorimpl$transactiondrivercontrolimpl.commit(jdbcresourcelocaltransactioncoordinatorimpl.java:220) @ org.hibernate.engine.transaction.internal.transactionimpl.commit(transactionimpl.java:68) @ mycode.worker.work(worker.java:133) @ mycode.worker.main(worker.java:19)
first things first, hope know alter
reserved word, alter table <table_name>;
, have change column name in fahrer class:
@column(name = "_alter") // choose name want private int alter;
after that, why need many bidirectional relationships? , look, have:
class fahrer { // ... @manytoone(cascade = cascadetype.all) private auto auto;
that means, when delete fahrer, auto deleted to. realy want?
now @ code:
// @ first check if id equal abfrage , add list if (driver.getid() == abfrage.getid()) { tobedeletet.add(aktuell); temp = driver; } // @ end formed list tobedeleted witch contains elements same id. (fahrer aktuell : tobedeletet) { session.remove(aktuell); }
to honest, i'm java beginner, may miss something. deleting entity same id value few times propably not necessary.
Comments
Post a Comment