简体   繁体   English

JPA外键为空

[英]JPA Foreign Key is Null

I am working on a web app and I am using JSF and JPA(EclipseLink). 我正在开发一个Web应用程序,并且正在使用JSF和JPA(EclipseLink)。 I have the tables story and story_translate , which are mapped as follows: 我有表storystory_translate ,它们的映射如下:

@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: ManagedBean我正在执行以下操作:

 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. 当我尝试创建一个新的StoryTranslate ,我得到了DatabaseException ,说story_id不能为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). 编辑:对不起,但我已经忘记了映射的另一部分(必须是缺乏睡眠)。

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. 问题是,您在StoryTranslate类中为STORY_ID列声明了storyId属性,但是在添加新的StoryTranslate ,您未将任何值设置为其storyId属性,并且我相信STORY_ID列具有NOT NULL约束,这就是为什么说story_id不能为null的异常。

The problem should be fixed once you set the storyId property of the StoryTranslate instance before committing the transaction . 一旦在提交事务之前设置了StoryTranslate实例的storyId属性,就应该解决该问题。

But it is strange that you map the STORY_ID column to two different properties ( storyId and story ) of the StoryTranslate class . 但令人奇怪的是,你在地图STORY_ID列到两个不同的特性( storyIdstory中的) StoryTranslate类。 Actually you do not need to declare storyId property as this value can be retrieved from the story instance . 实际上,您无需声明storyId属性,因为可以从story实例中检索此值。 I suggest you change the mapping of StoryTranslate to the following and your code should work fine without any changes. 我建议您将StoryTranslate的映射更改为以下内容,并且您的代码应该可以正常工作,而无需进行任何更改。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM