Note that there are some explanatory texts on larger screens.

plurals
  1. PO@ManyToOne mapping fails to save parent ID
    primarykey
    data
    text
    <p><em>I'm using JPA2 with EclipseLink implementation</em></p> <p>![Simple table structure][1]</p> <p>Here are the two tables which I try to map and the JPA annotations.</p> <pre><code>public class Story implements Serializable{ @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) Integer id; @Temporal(TemporalType.TIMESTAMP) @Column (name="DATE_CREATED") Date dateCreated; String title; String description; @Column(name="AUTHOR_ID") Integer authorId; @Column(name="COUNTRY_ID") Integer countryId; private String reviews; @OneToMany(mappedBy = "story", cascade=CascadeType.ALL) private List&lt;Tip&gt; tipList; } public class Tip implements Serializable{ @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) private Integer id; private String description; private Integer vote; @ManyToOne (cascade=CascadeType.ALL) @JoinColumn(name="STORY_ID", referencedColumnName="ID") private Story story; } </code></pre> <p>As a simple example I would like to persist a story and some story related tips in the same transaction. Here is the section of code which does that:</p> <pre><code>Story newStory = new Story(title, body, ...); EntityTransaction transaction = em.getTransaction().begin(); boolean completed = storyService.create(newStory); //The tips are saved as a List&lt;String&gt;. This methods creates the needed List&lt;Tip&gt; from the Strings List&lt;Tip&gt; tips = TipUtil.getTipList(tipList); newStory.setTipList(tips) transaction.commit(); </code></pre> <p>I have no errors and all the entities are persisted in the database. The problem is that in the <strong>tip table</strong> the <code>story_id</code> field is always <code>NULL</code>. I can imagine that JPA is unable to get the new <code>id</code> from the story table. What's the correct approach here?</p> <p><strong>LE</strong></p> <p>In the current state of the code, the <code>Tip</code> entities are persisted but the country ID remains null.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload