Ionut
Ionut

Reputation: 2858

@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.

Upvotes: 15

Views: 6130

Answers (4)

Pragalathan  M
Pragalathan M

Reputation: 1781

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

Upvotes: 11

Pau
Pau

Reputation: 813

Remove the

@Column(name = "STORY_ID") private Integer storyId;

You are already declaring it in @JoinColumn(name="STORY_ID", referencedColumnName="ID")

That is why you are getting the error Multiple writable mappings exist for the field [tip.STORY_ID]

Upvotes: 1

James
James

Reputation: 18389

You should not be using PrimaryKeyJoinColumn, just JoinColumn, but having your complete class would help giving a certain answer.

PrimaryKeyJoinColumn would only be used if the story_id was also the id of the Tip (no id in Tip) and there was a duplicate basic mapping for it. It should rarely be used, and is not required in JPA 2.0 anymore as duplicate id mappings are no longer required.

Upvotes: 0

Jerome Cance
Jerome Cance

Reputation: 8183

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

Upvotes: 7

Related Questions