Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@ManyToOne mapping fails to save parent ID

I'm using JPA2 with EclipseLink implementation

![Simple table structure][1]

Here are the two tables which I try to map and the JPA annotations.

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<Tip> 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;
}

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:

Story newStory = new Story(title, body, ...);

EntityTransaction transaction = em.getTransaction().begin();
    boolean completed = storyService.create(newStory);
    //The tips are saved as a List<String>. This methods creates the needed List<Tip> from the Strings
    List<Tip> tips = TipUtil.getTipList(tipList);
    newStory.setTipList(tips)
transaction.commit();

I have no errors and all the entities are persisted in the database. The problem is that in the tip table the story_id field is always NULL. I can imagine that JPA is unable to get the new id from the story table. What's the correct approach here?

LE

In the current state of the code, the Tip entities are persisted but the country ID remains null.

like image 370
Ionut Avatar asked Feb 28 '12 10:02

Ionut


2 Answers

With JPA, it is always recommended to update the relationship on both the sides in a bi-directional relationship. This is to ensure that the data is consistent in your application layer and nothing to do with the database.

However it is mandatory that you update the owning side of the relationship in a bidirectional relationship.

So, setting/not setting

story.setTipList(tips)

is up to you. But if you want the changes to reflect properly in DB then you mush call

tip.setStory(story)

as Tip is the owning side here, as per your code. Also your code looks incomplete to me. Reasons is,

  • the entity returned by storyService.create(newStory) is managed but not the newStory. So just setting newStory.setTipList(tips) will not updated the db
like image 109
Pragalathan M Avatar answered Oct 20 '22 14:10

Pragalathan M


Because you need to update the parent link story in each of your child.

The way its is done is to create a addTip(Tip tip) method in your Story class.

This method does :

tip.setStory(this);
tipList.add(tip);

If you don't need bedirectional approach, you can remove the story field in Tip and it will resolve your problem

like image 28
Jerome Cance Avatar answered Oct 20 '22 14:10

Jerome Cance