我的应用程序中有两个由列表表示的ManyToOne关系。对于关系" ChapterSection - ManyToOne - Chapter,当持久化实体时(表#34; ChapterSection""""的外键),将外键插入表中。被储存了)。对于另一种关系,即" Chapter - ManyToOne - Document"。
我使用ddl.generation" drop-and-create-tables"。在数据库中,我可以看到,该列" Chapter.fk_document_iddocument"被标记为引用文档ID的索引外键。 (我使用EclipseLink和MySQL)。
我没有看到这两种关系之间的区别以及为什么一个人正在锻炼但另一个没有。
文件实体:
@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
章实体:
@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
章节实体:
@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
我用以下方法创建文档的方法:
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);
}
答案 0 :(得分:3)
mappedBy
注释的@OneToMany
元素在JPA规定中定义如下:拥有该关系的字段或属性。除非关系是单向的,否则是必需的。
根据此定义,您的mappedBy
元素必须设置为(值应为字段名称,而不是类名称):
@OneToMany(mappedBy="document", cascade = CascadeType.PERSIST)
private List<Chapter> chapters;
@OneToMany(mappedBy="chapter", cascade=CascadeType.PERSIST)
List<ChapterSection> chapterSections;
在createDocument()
方法中,您尚未创建Document
和Chapter
之间的关系。所以你应该把它们绑在一起:
chapter.setDocument(document);