java - Hibernate only saving 2 of 3 values in database -
i trying create entity called muziekstuk entitys called instrument , componist created join-table 3 foreign keys id's of muziekstuk instrument , componist.
muziekstuk , instrument created , componist select dropdown
this action code:
muziekstukdaoimpl reg = new muziekstukdaoimpl(); componistdaoimpl com = new componistdaoimpl(); int componistid = integer.parseint(mcomponist); componist componist = (componist) com.getbyid(componistid); instrument i1 = new instrument(minstrument1, integer.parseint(maantal1)); muziekstuk muziekstuk = new muziekstuk(mtitel,mgenre,componist,i1); reg.save(muziekstuk);
it make new instrument , muziekstuk in join table id of componist null :s know why?
here rest of code: muziekstuk:
@entity @table(name = "stud1630651.mfmuziekstuk") public class muziekstuk { @id @generatedvalue(strategy=generationtype.auto, generator="my_entity_seq_gen") @sequencegenerator(name="my_entity_seq_gen", sequencename="hibernate_sequence_muziekstuk") @column(name = "id") private int id; @column(name = "titel") private string titel; @column(name = "genre") private string genre; @onetoone(cascade=cascadetype.all) @jointable(name="mfmuziekstukjoin",joincolumns= {@joincolumn(name="muziekstukid")},inversejoincolumns= {@joincolumn(name="componistid")}) private componist componist; @onetoone(cascade=cascadetype.all) @jointable(name="mfmuziekstukjoin",joincolumns= {@joincolumn(name="muziekstukid")},inversejoincolumns= {@joincolumn(name="instrumentid")}) private instrument instrument; public muziekstuk(string titel, string genre, componist componist, instrument instrument){ this.titel = titel; this.genre = genre; this.componist = componist; this.instrument = instrument; } public muziekstuk(int id, string titel, string genre, componist componist, instrument instrument){ this.id = id; this.titel = titel; this.genre = genre; this.componist = componist; this.instrument = instrument; } //getters , setters
componist:
@entity @table(name = "stud1630651.mfcomponist") public class componist { @id @generatedvalue(strategy=generationtype.auto, generator="my_entity_seq_gen") @sequencegenerator(name="my_entity_seq_gen", sequencename="hibernate_sequence_componist") @column(name = "id") private int id; @column(name = "naam") private string naam; @column(name = "land") private string land; @column(name = "tijdvlak") private string tijdvlak; @column(name = "composities") private string composities; public componist(int id, string naam, string land, string tijdvlak, string composities){ this.id = id; this.naam = naam; this.land = land; this.tijdvlak = tijdvlak; this.composities = composities; } public componist(string naam, string land, string tijdvlak, string composities){ this.naam = naam; this.land = land; this.tijdvlak = tijdvlak; this.composities = composities; } public componist(){ } //getters , setters
instrument:
@entity @table(name = "stud1630651.mfcomponist") public class componist { @id @generatedvalue(strategy=generationtype.auto, generator="my_entity_seq_gen") @sequencegenerator(name="my_entity_seq_gen", sequencename="hibernate_sequence_componist") @column(name = "id") private int id; @column(name = "naam") private string naam; @column(name = "land") private string land; @column(name = "tijdvlak") private string tijdvlak; @column(name = "composities") private string composities; public componist(int id, string naam, string land, string tijdvlak, string composities){ this.id = id; this.naam = naam; this.land = land; this.tijdvlak = tijdvlak; this.composities = composities; } public componist(string naam, string land, string tijdvlak, string composities){ this.naam = naam; this.land = land; this.tijdvlak = tijdvlak; this.composities = composities; } public componist(){ } //getters , setters
save function in dao:
@override public void save(object t) { muziekstuk muziekstuk = (muziekstuk) t; system.out.println(muziekstuk.getcomponist().getid()); session session = null; transaction transaction = null; try { session = hibernateutil.getsessionfactory().opensession(); transaction = session.begintransaction(); session.saveorupdate(muziekstuk); transaction.commit(); } catch (exception e) { if (transaction != null) { transaction.rollback(); system.out.println("exception"); e.printstacktrace(); } } { if (session != null) { session.close(); } } }
this sql trace:
hibernate: select componist0_.id id1_2_0_, componist0_.composities composit2_2_0_, componist0_.land land3_2_0_, componist0_.naam naam4_2_0_, componist0_.tijdvlak tijdvlak5_2_0_ stud1630651.mfcomponist componist0_ componist0_.id=? hibernate: select hibernate_sequence_muziekstuk.nextval dual hibernate: select hibernate_sequence_instrument.nextval dual hibernate: insert stud1630651.mfinstrument (hoeveelheid, naam, id) values (?, ?, ?) hibernate: insert stud1630651.mfmuziekstuk (genre, titel, id) values (?, ?, ?) hibernate: insert mfmuziekstukjoin (instrumentid, muziekstukid) values (?, ?)
ther should 3 ? in muziekstukjoin
it turns out join table can refer 1 table not 2 should make 2 join tables if wondering.
Comments
Post a Comment