如何与已经存在的寄存器建立单向多对多关系?

时间:2019-01-01 17:29:59

标签: java hibernate jpa spring-data-jpa

我是Spring / JPA的新手,我正在尝试建立一种与Vlad的this post类似的关系,但有一个区别。我的标签已经存在于另一个表中。

因此,如果我像弗拉德(Vlad)在其帖子中所做的那样,创建一个帖子,向其中添加一些标签,然后对其进行持久化,那么一切都会按预期进行。我在Post上注册了一个,在Tag上注册了两个,在PostTag上注册了两个。

Post newPost = new Post("Title");
newPost.addTag(new Tag("TagName"));
newPost.addTag(new Tag("TagName2"));
this.postRepository.save(newPost);

但是,如果我尝试创建标签并在创建帖子之前将其保存,则会收到错误消息。

Tag tag = new Tag("TagAlreadyCreated");
this.tagRepository.save(tag);
Post newPost = new Post("Title");
newPost.addTag(tag);
this.postRepository.save(newPost);
// Error: detached entity passed to persist: com.***.***.Tag

我知道我不想创建Tag(如果它已经存在),并且分离的消息表示我的Tag已经具有ID,因此我尝试将CascadeType更改为MERGE,但后来却不明白在PostTag上创建的寄存器。类的代码:

发布

@Entity(name = "Post")
@Table(name = "post")
public class Post {

    @Id
    @GeneratedValue
    private Long id;

    private String title;

    @OneToMany(
            mappedBy = "post",
            cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    private List<PostTag> tags = new ArrayList<>();

    public Post() {
    }

    public Post(String title) {
        this.title = title;
    }

    public void addTag(Tag tag) {
        PostTag postTag = new PostTag(this, tag);
        tags.add(postTag);
    }

    public void removeTag(Tag tag) {
        for (Iterator<PostTag> iterator = tags.iterator();
             iterator.hasNext(); ) {
            PostTag postTag = iterator.next();

            if (postTag.getPost().equals(this) &&
                    postTag.getTag().equals(tag)) {
                iterator.remove();
                postTag.setPost(null);
                postTag.setTag(null);
            }
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass())
            return false;

        Post post = (Post) o;
        return Objects.equals(title, post.title);
    }

    @Override
    public int hashCode() {
        return Objects.hash(title);
    }

    public Long getId() {
        return id;
    }
}

标记

@Entity(name = "Tag")
@Table(name = "tag")
@NaturalIdCache
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Tag {

    @Id
    @GeneratedValue
    private Long id;

    public Long getId() {
        return id;
    }

    @NaturalId
    private String name;

    public Tag() {
    }

    public Tag(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass())
            return false;

        Tag tag = (Tag) o;
        return Objects.equals(name, tag.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}

PostTag

@Entity(name = "PostTag")
@Table(name = "post_tag")
public class PostTag {

    @EmbeddedId
    private PostTagId id;

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("postId")
    private Post post;

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("tagId")
    private Tag tag;

    @Column(name = "created_on")
    private Date createdOn = new Date();

    private PostTag() {}

    public void setPost(Post post) {
        this.post = post;
    }

    public void setTag(Tag tag) {
        this.tag = tag;
    }

    public PostTag(Post post, Tag tag) {
        this.post = post;
        this.tag = tag;
        this.id = new PostTagId(post.getId(), tag.getId());
    }

    //Getters and setters omitted for brevity

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass())
            return false;

        PostTag that = (PostTag) o;
        return Objects.equals(post, that.post) &&
                Objects.equals(tag, that.tag);
    }

    public Post getPost() {
        return post;
    }

    public Tag getTag() {
        return tag;
    }

    @Override
    public int hashCode() {
        return Objects.hash(post, tag);
    }
}

PostTagId

@Embeddable
public class PostTagId
        implements Serializable {

    @Column(name = "post_id")
    private Long postId;

    @Column(name = "tag_id")
    private Long tagId;

    private PostTagId() {}

    public PostTagId(
            Long postId,
            Long tagId) {
        this.postId = postId;
        this.tagId = tagId;
    }

    //Getters omitted for brevity

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass())
            return false;

        PostTagId that = (PostTagId) o;
        return Objects.equals(postId, that.postId) &&
                Objects.equals(tagId, that.tagId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(postId, tagId);
    }
}

2 个答案:

答案 0 :(得分:0)

spring-data-jpa是JPA之上的一层。每个实体都有其自己的存储库,您必须处理该存储库。我已经看过上面提到的教程,它是针对JPA的,并且还将ID设置为null,这似乎有点不正确,可能是导致错误的原因。我没那么近看。为了处理spring-data-jpa中的问题,您需要为链接表提供单独的存储库。

@Entity
public class Post {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<PostTag> tags;

@Entity
public class Tag {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @OneToMany(mappedBy = "tag", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<PostTag> posts;

@Entity
public class PostTag {
    @EmbeddedId
    private PostTagId id = new PostTagId();

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("postId")
    private Post post;

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("tagId")
    private Tag tag;

    public PostTag() {}
    public PostTag(Post post, Tag tag) {
        this.post = post;
        this.tag = tag;
    }

@SuppressWarnings("serial")
@Embeddable
public class PostTagId implements Serializable {
    private Long postId;
    private Long tagId;
    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        PostTagId that = (PostTagId) o;
        return Objects.equals(postId, that.postId) && Objects.equals(tagId, that.tagId);
    }
    @Override
    public int hashCode() {
        return Objects.hash(postId, tagId);
    }

并使用它,如上所示:

@Transactional
private void update() {
    System.out.println("Step 1");
    Tag tag1 = new Tag();
    Post post1 = new Post();
    PostTag p1t1 = new PostTag(post1, tag1);
    tagRepo.save(tag1);
    postRepo.save(post1);
    postTagRepo.save(p1t1);

    System.out.println("Step 2");
    Tag tag2 = new Tag();
    Post post2 = new Post();
    PostTag p2t2 = new PostTag(post2, tag2);
    postRepo.save(post2);
    tagRepo.save(tag2);
    postTagRepo.save(p2t2);

    System.out.println("Step 3");
    tag2 = tagRepo.getOneWithPosts(2L);
    tag2.getPosts().add(new PostTag(post1, tag2));
    tagRepo.save(tag2);

    System.out.println("Step 4 -- better");
    PostTag p2t1 = new PostTag(post2, tag1);
    postTagRepo.save(p2t1);
}

请注意,更改很少。我没有明确设置PostTagId的ID。这些由持久层(在这种情况下为休眠)处理。

还请注意,由于已设置PostTag,因此可以使用其自己的存储库显式更新CascadeType.ALL条目,也可以通过从列表中添加和删除它们来进行更新,如图所示。对spring-data-jpa使用CascadeType.ALL的问题在于,即使您预取了连接表实体,spring-data-jpa仍将再次执行此操作。尝试通过CascadeType.ALL更新新实体的关系是有问题的。

没有CascadeTypeposts列表(应为Set)都不是该关系的所有者,因此,在持久性和仅用于查询结果。

在阅读tags关系时,由于没有PostTag,因此需要专门获取它们。 FetchType.EAGER的问题是开销,如果您不希望联接,并且同时将其放在FetchType.EAGERTag上,那么您将创建一个递归获取来获取所有{{ 1}}和Post进行任何查询。

Tags

最后,始终检查日志。请注意,创建关联需要spring-data-jpa(我认为是JPA)读取现有表以查看该关系是新建的还是更新的。无论您自己创建和保存Posts还是预取列表,都会发生这种情况。 JPA有单独的合并,我认为您可以更有效地使用它。

@Query("select t from Tag t left outer join fetch t.posts tps left outer join fetch tps.post where t.id = :id")
Tag getOneWithPosts(@Param("id") Long id);

答案 1 :(得分:0)

我想我找到了答案。

首先要知道的是,如果您将视图中打开设置为true ,Spring将在请求的整个生命周期中保持JPA /休眠会话处于打开状态。这意味着您的代码(在服务方法内部可以说)可以正常工作。当然这不是很有效(请参阅此处的原因): https://vladmihalcea.com/the-open-session-in-view-anti-pattern/

现在,如果将视图中打开设置为false ,似乎Spring会为每个存储库调用打开一个新的Session。因此,一个新的会话用于获取标签,然后一个新的会话用于保存帖子。这就是为什么在保存帖子时分离标签实体的原因。

要解决此问题,您需要使用 @Transactional 注释服务呼叫。 Spring将尝试在事务内重用相同会话,以使实体不会分离。参见这里例如: How does @Transactional influence current session in Hibernate?

最后要知道哪一个至关重要,那就是服务方法必须是 public ! 如果您的方法具有其他可见性,则@Transactional将被忽略,不会引发任何错误: https://docs.spring.io/spring/docs/current/spring-framework-reference/data-access.html#transaction-declarative-annotations(请参阅方法可见性和@