这个共享主键配置比较简单,但是有一点非常要注意,在执行保存执行之前一定要双向set,如下
article.setArticleData(articleData);
articleData.setArticle(article);
public class Article implements Serializable {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@OneToOne(targetEntity=ArticleData.class, cascade={CascadeType.ALL})
@PrimaryKeyJoinColumn
private ArticleData articleData;
}
articleData如下
public class ArticleData implements Serializable {
@Id
@GenericGenerator(name="foreignKey", strategy="foreign", parameters=@Parameter(name="property", value="article"))
@GeneratedValue(generator="foreignKey", strategy=GenerationType.IDENTITY)
private Long id;
@OneToOne(targetEntity=Article.class, cascade={CascadeType.ALL}, mappedBy="articleData")
@PrimaryKeyJoinColumn(name="id", referencedColumnName="id")
private Article article;
}