我使用JPA2与EclipseLink的实施@ManyToOne映射无法保存父ID
![简单的表结构] [1]
这里有两个表,我试图映射和JPA注释。
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;
}
作为一个简单的例子,我想在同一个事务中坚持一个故事和一些故事相关的技巧。 这里的代码确实是,部分:
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();
我没有错误,所有的实体在数据库中坚持着。问题是在小费表story_id
字段总是NULL
。我可以想象,JPA无法从故事表中获得新的id
。这里的正确方法是什么?
LE
在代码的当前状态,该Tip
实体仍然存在,但该国ID仍无效。
我想知道是否没有更自动的方式来做到这一点。更重要的是,我真的不认为删除提示中的故事字段是一个聪明的想法,因为这样,他将需要摆脱'@PrimaryKeyJoinColumn(name =“STORY_ID”,referencedColumnName =“ID” )',让JPA不知道应该如何进行加入。这正是我通过阅读这个主题所了解的。如果我错了,请让我知道。 – Dragos 2012-02-28 11:50:26
Ebean有自动解决方案,但JPA实现不提供自动解决方案。 – Palesz 2012-03-08 15:10:14