英文:
Hibernate: Save multiple association issue
问题
我有三个带有关联的实体。
public class Site {
@OneToMany
private List<Page> pages;
}
public class Page {
@OneToMany
private List<Article> articles;
@OneToMany
private List<TopArticle> topArticles;
}
public class Article {
@ManyToOne
private Page page;
}
public class TopArticle {
@ManyToOne
@JoinColumn(name = "PageId")
private Page page;
@OneToOne
@JoinColumn(name = "ArticleId")
private Article article;
}
TopArticle 中的字段 page 和 article 有数据库限制 - NOT NULL。(需要限制)
我必须保存具有所有关联的 Site。
Article article = new Article();
TopArticle topArticle = new TopArticle();
Page page = new Page();
page.getArticles().add(article);
page.getTopArticles().add(topArticle);
Site site = new Site();
site.getPages().add(page);
siteDAO.save(site);
有时它会成功保存站点。
但有时会抛出错误。
com.microsoft.sqlserver.jdbc.SQLServerException: 无法将值 NULL 插入列 'ArticleId',表 '...TopArticle';列不允许为空。插入失败。
似乎在保存 Page 之后,它尝试保存关联的 List
也许,在 List
以另一种方式则会失败。
问题:
在 JPA 中保存属性顺序存在问题吗?
如何强制 Hibernate 先保存 articles,然后再保存 topArticles?
还有其他解决方案吗?
谢谢。
英文:
I have three entities with relations.
public class Site {
@OneToMany
private List<Page> pages;
}
public class Page {
@OneToMany
private List<Article> articles;
@OneToMany
private List<TopArticle> topArticles;
}
public class Article {
@ManyToOne
private Page page;
}
public class TopArticle {
@ManyToOne
@JoinColumn(name = "PageId")
private Page page;
@OneToOne
@JoinColumn(name = "ArticleId")
private Article article;
}
Fields page and arctile in TopArticle have database restrictions - NOT NULL. (Resctrictions are required)
I have to save Site with all associations.
Article article = new Article();
TopArticle topArticle = new TopArticle();
Page page = new Page();
page.getArticles().add(article);
page.getTopArticles().add(topArticle);
Site site = new Site();
site.getPages().add(page);
siteDAO.save(site);
Sometimes it saves site.
But sometimes it throws an error.
com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert the value NULL into column 'ArticleId', table '...TopArticle'; column does not allow nulls. INSERT fails
It seems that after Page is saved, it tries to save associations List<Article> articles and List<TopArticle> topArticles.
Perhaps, when List<Article> articles are saved before List<TopArticle> topArticles it works.
In another way it fails.
Questions:
Is an issue in saving properties order in JPA?
How can I enforce Hibernate to save articles before topArticles?
Is there any other solutions?
Thanks
答案1
得分: 2
有了双向关系,您有责任确保内存中的模型的双方都设置正确,否则您将会看到您所见到的错误。
因此,您需要调用:
article.setPage(page);
而且最好将操作封装起来,以确保内存中的模型始终保持一致:
public class Page{
public List<Article> getArticles(){
return Collections.unmodifiableList(articles); //force through add method
}
public void addArticle(Article article){
articles.add(article);
article.setPage(this);
}
}
英文:
With a bi-directional relationship it is your responsibility to ensure both sides are set correctly on the in-memory model otherwise you will get the error you are seeing.
So you need to call:
article.setPage(page);
And ideally you should encapsulate the operation so the in-memory model is always consistent:
public class Page{
public List<Article> getArticles(){
return Collections.unmodifiableList(articles); //force through add method
}
public void addArticle(Article article){
articles.add(article);
article.setPage(this);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论