[英]JPA @ManyToOne foreign key not generated?
我的應用程序中有兩個ManyToOne關系,由列表表示。 對於關系“ChapterSection - ManyToOne - Chapter”,外鍵插入表中,當持久化實體時(在表“ChapterSection”中存儲“Chapter”的外鍵)。對於另一種關系,即“Chapter” - ManyToOne - 文件“。
我使用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);
}
@OneToMany
批注的mappedBy
元素在JPA @OneToMany
中定義如下: 擁有這種關系的領域或財產。 除非關系是單向的,否則是必需的。
根據此定義,您的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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.