Reputation: 133
I have two ManyToOne relationships in my application which are represented by Lists. For the relationship "ChapterSection - ManyToOne - Chapter, the foreign key is inserted in the table, when persisting the entity (in the table "ChapterSection" the foreign key for "Chapter" is stored). For the other relationship, which is "Chapter - ManyToOne - Document".
I use ddl.generation "drop-and-create-tables". In the Database I can see, that the column "Chapter.fk_document_iddocument" is marked as an indexed foreign key referenced to the document id. (I use EclipseLink and MySQL).
I don't see the difference between these two relationships and why one is working out but the other is not.
Document Entity:
@Entity
public class Document implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="iddocument")
private Long id;
@Column(name="document_name")
private String documentName;
@OneToMany(mappedBy="Document", cascade = CascadeType.PERSIST)
private List<Chapter> chapters;
@Enumerated(EnumType.STRING)
@Column(name="document_type")
private DocumentTypes documentType;
//...getters, setters and other generated methods
Chapter Entity:
@Entity
public class Chapter implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="idchapter")
private Long id;
@Column(name="chapter_order")
private int chapterOrder;
@Column(name="parent_chapter")
private Long parentChapter;
@Column(name="chapter_name")
private String chapterName;
@ManyToOne(optional=false)
@JoinColumn(name="fk_document_iddocument")
private Document document;
@OneToMany(mappedBy="Chapter", cascade=CascadeType.PERSIST)
List<ChapterSection> chapterSections;
//...getters, setters and other generated methods
ChapterSection Entity:
@Entity
public class ChapterSection implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="idchaptersection")
private Long idChapterSection;
@Column(name="section_name")
private String sectionName;
@Column(name="section_order")
private int sectionOrder;
@Column(name="content")
private String content;
@ManyToOne
@JoinColumn(name="fk_chapter_idchapter")
private Chapter chapter;
//...getters, setters and other generated methods
The method I create the document with:
public void createDocument() {
List <Chapter> chapters = new ArrayList<>();
for (int i = 0; i <= 4; i++) {
Chapter chapter = new Chapter();
chapter.setChapterOrder(i);
chapter.setChapterName("Chapter "+i);
List <ChapterSection> chapterSections = new ArrayList<>();
for (int j = 0; j <= 4; j++) {
ChapterSection chapterSection = new ChapterSection();
chapterSection.setChapter(chapter);
chapterSection.setSectionName("Chapter "+i+" Section");
chapterSection.setSectionOrder(j);
chapterSection.setContent("Kapitel "+i+ ", Section "+j+" Content!");
chapterSections.add(chapterSection);
}
chapter.setChapterSections(chapterSections);
chapters.add(chapter);
}
document.setDocumentName("My Doc");
document.setChapters(chapters);
document.setDocumentType("My Doc Type");
documentDAO.persistDocument(document);
}
Upvotes: 1
Views: 2038
Reputation: 3309
mappedBy
element of the @OneToMany
annotation is defined in the JPA spcification as follows:The field or property that owns the relationship. Required unless the relationship is unidirectional.
According to this definition your mappedBy
elements must be set as (the value should be field name but not the class name):
@OneToMany(mappedBy="document", cascade = CascadeType.PERSIST)
private List<Chapter> chapters;
@OneToMany(mappedBy="chapter", cascade=CascadeType.PERSIST)
List<ChapterSection> chapterSections;
In you createDocument()
method you haven't created the relationship between the Document
and the Chapter
. So you should tie them as follows:
chapter.setDocument(document);
Upvotes: 3