Reputation: 31
I have a problem with my hibernate casscade. I'm trying to persist a set of document. The data model is the following:
My scenario is the 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 the first block I create a new TextBlock. With the cascade the rest should be created too. In the second block I create another textblock within the same document. In the last block I create also antoher textblock but with the same speaker. But I constantly getting the following exception:
Caused by: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : de.uniba.speechanalyser.persist.model.Document.corpus -> de.uniba.speechanalyser.persist.model.Corpus
Here you can see my 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 from Speaker s where s.firstName = :firstName and 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 from Party p where 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 also strugeling with the relations between the object/tables. Especially with the cascades. I read a lot on stackoverflow but nothing helped really. This is my current approach. When a 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 then add it to the TextBlock it works without any problem. So can somebody help me?
Greeting, David
Upvotes: 0
Views: 429
Reputation: 31
Ok, cool I narrowed the problem to only the Speaker-Party-Side of the Relations!
My actual new classes are: 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 = new ArrayList<Document>();
public void addDocument(Document document) {
if (documentList != null) {
documentList.add(document);
}
}
}
Document Class
@Entity
public class Document implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "document_id")
private int id;
@ManyToOne
@JoinColumn(name = "corpus_id")
private Corpus corpus;
@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "document")
private List<TextBlock> textBlockList = new ArrayList<TextBlock>();
public Document() {
}
public Document(Corpus corpus) {
this.corpus = corpus;
}
public void addTextBlock(TextBlock textBlock) {
if (textBlockList != null) {
textBlockList.add(textBlock);
}
}
}
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
@JoinColumn(name = "document_id")
private Document document;
@ManyToOne
private Speaker speaker;
public TextBlock() {
}
public TextBlock(Speaker speaker, String content, Document document) {
this.speaker = speaker;
this.content = content;
this.document = document;
}
}
Speaker Class
@Entity
@NamedQueries({
@NamedQuery(name = "Speaker.findSpeakerByName", query = "select s from Speaker s where s.firstName = :firstName and 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
private Party party;
public Speaker() {
}
public Speaker(String firstName, String lastName, Party party) {
this.firstName = firstName;
this.lastName = lastName;
this.party = party;
}
}
Party Class
@Entity
@NamedQueries({
@NamedQuery(name = "Party.findPartyByName", query = "select p from Party p where 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 = new ArrayList<Speaker>();
public Party() {
}
public Party(String name) {
this.name = name;
}
}
How do I set the CascadeType on Speaker and Party that when I persist a Corpus the Speaker and the Party is persited too.
My test code is the following:
CorpusFacade cf = new CorpusFacade();
Corpus corpus = new Corpus();
Document document = new Document(corpus);
corpus.addDocument(document);
Party party = new Party("ASDF");
Speaker speaker = new Speaker("David", "Müller", party);
TextBlock textBlock1 = new TextBlock(speaker, "TB1", document);
document.addTextBlock(textBlock1);
TextBlock textBlock2 = new TextBlock(speaker, "TB1", document);
document.addTextBlock(textBlock2);
cf.createCorpus(corpus);
Edit For those who are interessted in my solution of the problem. I create first a new Speaker and a new Party. Then I persist the Party (the Speaker persists automatically). After that I add the Speaker to the TextBlock. Thats all. I couldn't find asolution where the corpus creates the Speaker and Party too. I hope it works for someone.
Upvotes: 0
Reputation: 9775
Your Corpus
does not get persisted. You cascade only MERGE. See the annotation definition in your 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 simply say CascadeType.ALL if you are sure what you are doing
private Corpus corpus;
Upvotes: 1