JPA @ManyToOne foreign key not generated?
<p>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". </p>

<p>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).</p>

<p>I don't see the difference between these two relationships and why one is working out but the other is not.</p>

<p>Document Entity:</p>

<pre><code>@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&lt;Chapter&gt; chapters;

    @Enumerated(EnumType.STRING)
    @Column(name="document_type")
    private DocumentTypes documentType;

//...getters, setters and other generated methods
</code></pre>

<p>Chapter Entity:</p>

<pre><code>@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&lt;ChapterSection&gt; chapterSections;

//...getters, setters and other generated methods
</code></pre>

<p>ChapterSection Entity:</p>

<pre><code>@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
</code></pre>

<p>The method I create the document with:</p>

<pre><code>public void createDocument() {

       List &lt;Chapter&gt; chapters = new ArrayList&lt;&gt;();

            for (int i = 0; i &lt;= 4; i++) {
                Chapter chapter = new Chapter();
                chapter.setChapterOrder(i);
                chapter.setChapterName("Chapter "+i);
                List &lt;ChapterSection&gt; chapterSections = new ArrayList&lt;&gt;();
                for (int j = 0; j &lt;= 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);
}
</code></pre>