SpringBoot CascadeType ALL vs MERGE和独立实体

时间:2019-03-17 18:15:51

标签: spring-boot jpa

我有以下实体:

@Entity
@Getter @Setter @NoArgsConstructor @RequiredArgsConstructor
public class Link extends Auditable {

    @Id
    @GeneratedValue
    private Long id;

    @NonNull
    private String title;


    @NonNull
    private String url;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "link")
    private List<Comment> comments = new ArrayList<>();

    @Transient
    @Setter(AccessLevel.NONE)
    private String userAlias ;

    public String getUserAlias() {

        if(user == null)
            return "";

        return user.getAlias();


    }

    @ManyToOne
    private User user;

    public Long getUser() {
        if(user == null)
            return -1L;

        return user.getId();
    }

    public void addComment(Comment c) {
        comments.add(c);
        c.setLink(this);

    }




}

@Entity
@Getter @Setter @NoArgsConstructor @RequiredArgsConstructor
public class Comment extends Auditable{

    @Id
    @GeneratedValue
    private Long id;

    @NonNull
    private String comment;

    @ManyToOne(fetch = FetchType.LAZY)
    private Link link;

    public Long getLink() {
        return link.getId();
    }


}

如果我创建评论和链接,请将链接与评论关联,然后保存。

例如:

Link link = new Link("Getting started", "url");
Comment c = new Comment("Hello!");
link.addComment(c);
linkRepository.save(link);

但是,如果我先保存评论:

Link link = new Link("Getting started", "url");
Comment c = new Comment("Hello!");
commentRepository.save(c);
link.addComment(c);
linkRepository.save(link);

我知道

Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on uk.me.dariosdesk.dariodemo.domain.Comment.link references an unknown entity: uk.me.dariosdesk.dariodemo.domain.Link
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:97) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processEndOfQueue(InFlightMetadataCollectorImpl.java:1815) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1759) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1646) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:287) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:903) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:934) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]

将级联类型从ALL更改为MERGE似乎可以解决此问题并接受两种实现。 (即:添加预先存在的注释或创建两者,然后通过链接保存)

1)为什么会这样? 2)使用MERGE而不是ALL时,我应该注意什么?

1 个答案:

答案 0 :(得分:1)

存储库保存方法检查实体是否存在。对于新实体,将调用persist,对于永久实体,则调用合并。

@Transactional
public <S extends T> S save(S entity) {

    if (entityInformation.isNew(entity)) {
        em.persist(entity);
        return entity;
    } else {
        return em.merge(entity);
    }
}

在第二个用例中,Link是新实体,因此调用了persist()。使用CascadeType.ALL,persist()级联到Comment实体。注释已经保留,需要合并,persist()失败。

如果使用CascadeType.MERGE,persist()不会级联为Comment。它不会失败。