Dragos
Dragos

Reputation: 2981

JPA Foreign Key is Null

I am working on a web app and I am using JSF and JPA(EclipseLink). I have the tables story and story_translate, which are mapped as follows:

@Entity
@Table(name = "story")
public class Story{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer id;
private String title;
private String description;
@OneToMany(mappedBy = "story", cascade=CascadeType.ALL)
private List<StoryTranslate> translateList;

    //getters and setters
 }

@Entity
@Table(name = "story_translate")
public class StoryTranslate{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer id;
@Column(name="STORY_ID")
private Integer storyId;
    @ManyToOne
@JoinColumn(name="story_id", referencedColumnName="id", updatable=false, insertable=false)
private Story story;

   //some other fields, getters and setters
}

In a ManagedBean I am doing the following:

 StoryTranslate translate = new StoryTranslate(null, sessionController.getEntity().getId(), getAuthUser().getId(), language, 
                    title, description, new Date(), false);
 EntityTransaction transaction = TransactionSingleton.getActiveInstance();
            Story story = storyService.read(sessionController.getEntity().getId());
            if (story != null){
                if (story.getTranslateList() == null){
                    story.setTranslateList(new ArrayList<StoryTranslate>());
                }
                story.getTranslateList().add(translate);
                translate.setStory(story);
            }
            transaction.commit();

When I try to create a new StoryTranslate, I get a DatabaseException, saying the story_id cannot be null.

I have managed relationships before, but I have never seen this error.

Where is the problem?

EDIT: I am sorry, but I have forgotten about another part of the mapping(must be the lack of sleep).

Upvotes: 4

Views: 10890

Answers (1)

Ken Chan
Ken Chan

Reputation: 90417

The problem is that your declare the storyId property in the StoryTranslate class for the STORY_ID column but when adding a new StoryTranslate , you do not set any value to its storyId property and I believe STORY_ID column has a NOT NULL constraint and that why you get the exception saying that story_id cannot be null.

The problem should be fixed once you set the storyId property of the StoryTranslate instance before committing the transaction .

But it is strange that you map the STORY_ID column to two different properties ( storyId and story) of the StoryTranslate class . Actually you do not need to declare storyId property as this value can be retrieved from the story instance . I suggest you change the mapping of StoryTranslate to the following and your code should work fine without any changes.

@Entity
@Table(name = "story_translate")
public class StoryTranslate{
   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE)
   private Integer id;

   @ManyToOne
   @JoinColumn(name="story_id")
   private Story story;

   //some other fields, getters and setters
}

Upvotes: 4

Related Questions