- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有两个实体“文章”和“评论”。 Article 与 Comment 具有 OneToMany 关系。
@Entity
@Table(name = "article")
public class Article implements Serializable
{
public Article()
{
}
private static final long serialVersionUID = 1L;
@Id
@Column(name = "article_id")
private int articleId;
@Column(name = "title")
private String title;
@Column(name = "category")
private String category;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "articleId", cascade = CascadeType.ALL)
private List<Comment> comments = new ArrayList<>();
}
@Entity
@Table(name = "comment")
public class Comment
{
private static final long serialVersionUID = 1L;
public Comment()
{
}
@Id
@Column(name = "comment_id")
private int commentId;
@Column(name = "author")
private String author;
@Column(name = "text")
private String text;
@JoinColumn(name = "article_id", nullable = false)
private int articleId;
}
我正在使用 JPA EntityManager 对文章执行 CRUD 操作。
我在“文章”表中有一篇文章,其中包含以下数据。
article_id title category
1 Java 8 in 30 days Java
我在“评论”表中有两条评论,数据如下。
comment_id author text article_id
1 ABC Java is awesome ! 1
2 XYZ Nice Article !!! 1
这是我的 EntityManager 代码,当包含评论的文章有更新时,它会被调用。
public Article update(Article article)
{
return entityManager.merge(article);
}
我在这里面临的问题是,每当我使用上述方法调用从现有文章中删除评论时,评论实际上并没有从表中删除。我知道“合并”与“更新插入”相同,但我没有在 EntityManager 界面中找到任何其他方法来实现评论删除以及对文章的其他更改。
最佳答案
在您的代码中,@OneToMany(... cascade = CascadeType.ALL)
意味着无论何时对父级进行修改(保留、删除等),它都会级联到 children 也是如此。因此,如果一个 Article
被保存,其所有对应的 Comments
也会被保存。或者,如果一个 Article
被删除,其所有相应的 Comments
也被删除。
在你的例子中,你想要的只是删除一个 Comment
,与其 Article
中发生的操作无关。
一个简单的方法是使用 @OneToMany(... cascade = CascadeType.ALL, orphanRemoval = true)
然后当你决定触发 delete
例如,第一个 Comment
,只需在其上使用 Collection.remove()
:
Article article = ...;
//...
Comment targetComment = article.getComments().iterator().next();
article.getComments().remove(targetComment);
// now Hibernate considers `targetComment` as orphan and it executes an SQL DELETE for it
确保您的集合是唯一持有对 targetComment
引用的对象的引用的集合,否则您将在内存中出现不一致。
关于java - JPA 级联更新/删除 OneToMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47091366/
我有如下三个实体相关:A(一对多) B(一对多) C我如何从 C 中获取基于 A->id 的所有记录?????? 最佳答案 类似于: $entityManager = $this->getDoctri
代码之间有什么区别? @LazyCollection(LazyCollectionOption.FALSE) @OneToMany(mappedBy = "siteesTypeSite", casca
public class ClientEntity { @Id @Column(name="id", nullable = false, unique = true) @Gen
现在,我明白这是不可能的,但我无法想象这是一个从未遇到过的问题。 我在不同的 PU 中有两个实体。我想从一个外键到另一个。我想知道谁已经遇到过这个问题,他们是否找到了一个好的解决方案? 最佳答案 Ec
我有两个与 OneToMany - ManyToOne 映射关联的类。当我选择父实体时,它也会选择子实体,但是分配给它的所有子实例都是每个父实例,而不是分配相关实例。 采购入口.java @Entit
我在 OneToMany 字段的持久性方面遇到了麻烦。这是两个简化的类 我是用来做测试的。 public class User implements Serializable { ... privat
我有一个持久化实体,它有一个 @OneToMany另一个实体的列表,我需要列表顺序才能由用户编辑,这非常有效。我可以完全重新排序内存中的 java 列表,当我 save() 对象时,链接表中的链接顺序
我是 Hibernate 的新手,正在尝试一些应该很容易的事情,但我无法让它工作。 有两个表,一个人和一个地址。一个人可以有一个或多个地址,即:一个 OneToMany 映射。当我尝试将相同的地址添加
我有双向、一对多和多对一的关系。说,一个公司有很多人,一个人有一个公司,所以,在公司中, @OneToMany(mappedBy = "company", fetch = FetchType.EAGE
在JPA中设置一对多关系时如何设置外键的列名? 我想将“items_id”的名称更改为“item_id” @OneToMany private List items; 我尝试了以下注释但没有成功: @
我有2张 table : 第一个是“人”: person_id, 人名 第二个是“PersonsGraphs”: person_id1, person_id2, 关系类型 我正在寻找一种建立“家谱”的
请帮我解决这个问题。我尝试了很多组合,但似乎没有任何效果。我正在尝试使用注释实现 hibernate 映射,但在保存我的父对象及其子对象期间,我注意到正在调用更新语句而不是插入语句。 我有两个彼此具有
我是 JPA 新手。假设我有这两个实体: //Imports @Entity @Table(name="article", schema = "sch_client") public class Ar
我有使用 Hibernate 归档 @ManyToOne 关系的代码。我有两个类 Package 和 Address。在 Address 中,我希望有唯一的条目,其中每个地址都与其他地址有所不同。然后
在订单表中插入时遇到此错误 org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to
我是 Hibernate 新手,我正在尝试在 Person 和 Vehicle 类之间建立 OneToMany/ManyToOne 双向关系。在我的示例中,一个人可以拥有许多车辆,而一辆车辆只属于一个
我们是 JPA 新手,尝试建立一个非常简单的一对多关系,其中名为 Message 的 pojo 可以具有由名为 GROUP_ASSOC 的联接表定义的整数组 id 列表。 。这是 DDL: CREAT
我在使用 JPA 时遇到了一些小问题。我有三个表(OTHER 表与此处无关,但我添加它只是为了解释为什么存在没有 PK 的表 USERS): ADDRESS id (PK) user_id (FK)
请帮我解决这个问题。我尝试了很多组合,但似乎没有任何效果。我正在尝试使用注释实现 hibernate 映射,但在保存我的父对象及其子对象期间,我注意到正在调用更新语句而不是插入语句。 我有两个彼此具有
我的 Task 实体表示为: @Entity @Getter @NoArgsConstructor public class Task { @Id @GeneratedValue(st
我是一名优秀的程序员,十分优秀!