【发布时间】:2012-03-17 20:10:55
【问题描述】:
我正在使用 JPA2 和 EclipseLink 实现
![简单表结构][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();
我没有错误,所有实体都保存在数据库中。问题在于,在 tip 表 中,story_id 字段始终为 NULL。我可以想象 JPA 无法从故事表中获取新的id。这里的正确方法是什么?
LE
在代码的当前状态下,Tip 实体保持不变,但国家/地区 ID 保持为空。
【问题讨论】:
标签: java jpa entity-relationship