Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JPA@manytone外键未生成?_Jpa_Eclipselink - Fatal编程技术网

JPA@manytone外键未生成?

JPA@manytone外键未生成?,jpa,eclipselink,Jpa,Eclipselink,我的应用程序中有两个由列表表示的多通关系。对于“ChapterSection-ManyToOne-Chapter”关系,在持久化实体时将外键插入表中(在表“ChapterSection”中存储“Chapter”的外键)。对于其他关系,即“Chapter-ManyToOne-Document” 我使用ddl.generation“drop and create tables”。在数据库中,我可以看到“Chapter.fk_document_iddocument”列被标记为引用文档id的索引外键(我

我的应用程序中有两个由列表表示的多通关系。对于“ChapterSection-ManyToOne-Chapter”关系,在持久化实体时将外键插入表中(在表“ChapterSection”中存储“Chapter”的外键)。对于其他关系,即“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
使用以下方法创建文档:

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);
}
public void createDocument(){
列表章节=新建ArrayList();
对于(int i=0;i
  • @OneToMany
    注释的
    mappedBy
    元素在JPA规范中定义如下:
拥有关系的字段或属性。除非关系是单向的,否则为必填项

根据此定义,您的
mappedBy
元素必须设置为(值应为字段名,而不是类名):


如果您能改进您的问题,可能很容易解决。问题到底是什么?好的,我明白了。所以JPA不会通过只在文档中添加章节来识别相应文档中的内容?我必须始终明确设置它?在您的情况下,是的。您已经定义了双向关系,以便您可以从w访问每个实体我在另一个。
@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="document", cascade = CascadeType.PERSIST)
private List<Chapter> chapters;

@OneToMany(mappedBy="chapter", cascade=CascadeType.PERSIST)
List<ChapterSection> chapterSections;
chapter.setDocument(document);