java - Persist Object with Hibernate in OneToMany -


i have problem hibernate casscade. i'm trying persist set of document. data model following:

  • corpus 1 --- n document 1 --- n textblock n --- 1 speaker n --- 1 party

my scenario following:

speakerfacade sf = new speakerfacade(); textblockfacade tf = new textblockfacade();  corpus corpus = new corpus();  document doc1 = new document(corpus); textblock tb1 = new textblock(new speaker("david", "müller", new party("asdf")), "tb1", doc1); tf.createtextblock(tb1); textblock tb2 = new textblock(new speaker("benedikt", "müller", new party("jklÖ")), "tb2", doc1); tf.createtextblock(tb2); textblock tb3 = new textblock(sf.findpersonbyid(1), "tb3", doc1); tf.createtextblock(tb3); 

so in first block create new textblock. cascade rest should created too. in second block create textblock within same document. in last block create antoher textblock same speaker. getting following exception:

caused by: org.hibernate.transientpropertyvalueexception: object references unsaved transient instance - save transient instance before flushing : de.uniba.speechanalyser.persist.model.document.corpus -> de.uniba.speechanalyser.persist.model.corpus 

here can see model classes (in short form):

corpus class

@entity public class corpus implements serializable {  private static final long serialversionuid = 1l;  @id @generatedvalue @column(name = "corpus_id") private int id;  @onetomany(cascade = { cascadetype.all }, mappedby = "corpus") private list<document> documentlist; } 

document class

@entity public class document implements serializable {  private static final long serialversionuid = 1l;  @id @generatedvalue @column(name = "document_id") private int id;  @manytoone(cascade = { cascadetype.merge },fetch= fetchtype.eager) private corpus corpus;  @onetomany(cascade = { cascadetype.all }, mappedby = "document") private list<textblock> textblocklist; } 

textblock class

@entity public class textblock implements serializable {  private static final long serialversionuid = 1l;  @id @generatedvalue @column(name = "textblock_id") private int id;  @lob string content;  @manytoone(cascade = { cascadetype.merge },fetch= fetchtype.eager) private document document;  @manytoone(cascade = { cascadetype.merge },fetch= fetchtype.eager) private speaker speaker; } 

speaker class

@entity @namedqueries({     @namedquery(name = "speaker.findspeakerbyname", query = "select s speaker s s.firstname = :firstname , s.lastname = :lastname") }) public class speaker implements serializable {  private static final long serialversionuid = 1l; public static final string find_by_name = "speaker.findspeakerbyname";  @id @generatedvalue @column(name = "speaker_id") private int id;  private string firstname; private string lastname;  @onetomany(cascade = { cascadetype.all }, mappedby = "speaker") private list<textblock> textblock;  @manytoone(cascade = { cascadetype.merge },fetch= fetchtype.eager) private party party; } 

party class

@entity @namedqueries({ @namedquery(name = "party.findpartybyname", query = "select p party p p.name = :name") })  public class party implements serializable {  private static final long serialversionuid = 1l; public static final string find_by_name = "party.findpartybyname";  @id @generatedvalue @column(name = "party_id") private int id;  private string name;  @onetomany(cascade = { cascadetype.all}, mappedby = "party") private list<speaker> speakerlist; } 

i'm strugeling relations between object/tables. cascades. read lot on stackoverflow nothing helped really. current approach. when create each object on it's own like:

speaker speaker= new speaker("david", "müller", pf.findbyid(1)); sf.createspeaker(speaker); speaker = sf.findspeakerbyid(1); 

and add textblock works without problem. can me?

greeting, david

your corpus not persisted. cascade merge. see annotation definition in document class:

@manytoone(cascade = { cascadetype.merge }, fetch= fetchtype.eager) private corpus corpus; 

should be:

@manytoone(cascade = { cascadetype.merge, cascadetype.persist }, fetch= fetchtype.eager) //you can cascadetype.all if sure doing private corpus corpus; 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -