Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 使用带有双边双向关联的@EmbeddedId时存在EntityExistsException_Hibernate_Spring Boot_Spring Data Jpa - Fatal编程技术网

Hibernate 使用带有双边双向关联的@EmbeddedId时存在EntityExistsException

Hibernate 使用带有双边双向关联的@EmbeddedId时存在EntityExistsException,hibernate,spring-boot,spring-data-jpa,Hibernate,Spring Boot,Spring Data Jpa,javax.persistence.EntityExistsException:具有相同标识符值的不同对象已与会话关联 此异常的答案被Stackoverflow淹没,但没有一个与我的问题案例相匹配。我有两个实体Post和User,它们有manytomy关系,但关系表有两个额外字段:vote_at和vote 因此,我需要一个额外的实体类postdoite,它将具有EmbeddedIdPostVotetable是一个表格,其中包含投票给post public class PostVote imple

javax.persistence.EntityExistsException:具有相同标识符值的不同对象已与会话关联

此异常的答案被Stackoverflow淹没,但没有一个与我的问题案例相匹配。我有两个实体
Post
User
,它们有
manytomy
关系,但关系表有两个额外字段:
vote_at
vote

因此,我需要一个额外的实体类
postdoite
,它将具有
EmbeddedId
PostVote
table是一个表格,其中包含投票给
post

public class PostVote implements Serializable {

    @EmbeddedId
    private PostVoteId id;

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

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("userId")
    @JoinColumn(name = "user_id")
    private User user;

    @NotNull
    @CreationTimestamp
    private LocalDateTime votedAt;

    @NotNull
    private Boolean vote = true;

    public PostVote() {
    }

    PostVote(Post post, User user) {
        this.post = post;
        this.user = user;
        this.id = new PostVoteId(post.getPostId(), user.getUserId());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PostVote that = (PostVote) o;
        return Objects.equals(post, that.post) &&
                Objects.equals(user, that.user);
    }
    @Override
    public int hashCode() {
        return Objects.hash(post, user);
    }
}
PostVoteId
实体

@Embeddable
public class PostVoteId implements Serializable {

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

    @Column(name = "user_id")
    private Integer userId;

    PostVoteId(Integer postId, Integer userId) {
        this.postId = postId;
        this.userId = userId;
    }

    public PostVoteId() {
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PostVoteId that = (PostVoteId) o;
        return Objects.equals(postId, that.postId) &&
                Objects.equals(userId, that.userId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(postId, userId);
    }
}
我跟在后面。现在,为了向
post
添加投票,我在
post
中使用了
addVote
方法

public class Post implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    @Column(name = "post_id", unique = true, nullable = false)
    private Integer postId;

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

    public void addVote(User user){
        PostVote postVote = new PostVote(this, user);
        voters.add(postVote);
        user.getVotedPosts().add(postVote); //problem here
    }
}
上面的代码给出了错误

javax.persistence.EntityExistsException:具有相同标识符值的不同对象已与会话[com.sample.entities.PostVote#com.sample.entities]关联。PostVoteId@4ba].

但是如果我删除
user.getVotedPosts().add(postVote)
(保持单边双向关联),那么它可以正常工作


现有的解决方案没有帮助。Maximum表示要检查
GenerationType
策略和
equals
hashcode
的实现。但在我的例子中,两者都设置得很好。

您使用什么hibernate版本?@SternK
5.3.9.Final
Post post = postRepository.findById(postId);
final User user = userRepository.findById(123);
post.addVote(user);

postRepository.save(post);