java - 如何映射这个复合主键?

标签 java spring postgresql hibernate jpa

我正在使用 spring 创建一个社交媒体网站,但我遇到了复合主键的问题。 我有 User、Post 和 PostLike 表,我需要跟踪哪个用户喜欢哪些帖子。我还有一个 UserData 和 Post 关系,它告诉我帖子作者,但这不会造成任何问题。基本上,我的 PostLike 表应该包含 post_id 和 user_id 列,并且它们应该充当复合主键。

我尝试通过创建可嵌入的 PostLikeId 来做到这一点:

我的 PostLikeId 类:

@Embeddable
public class PostLikeId implements Serializable {

    @Column(name = "post_id")
    private Post post;

    @Column(name = "user_id")
    private UserData user;

    // getters and setters

}

我的 PostLike 类(class):

@Entity
@Table(name = "post_like")
public class PostLike {

    @EmbeddedId
    private PostLikeId id;

    //getters and setters
}

我的帖子类(class):

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

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "post_id_seq_generator")
    @SequenceGenerator(name = "post_id_seq_generator", sequenceName = "post_id_seq", allocationSize = 10)
    @Column(name = "id", updatable = false)
    private Long id;

    @OneToMany(cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    @JoinColumns({
            @JoinColumn(name = "post_id",
                    referencedColumnName = "post_id"),
            @JoinColumn(name = "user_id",
                    referencedColumnName = "user_id")
    })
    private List<PostLike> likes = new ArrayList<>();

}

...和我的 UserData 类:

@Entity
@Table(name = "user_data")
public class UserData {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_data_id_seq_generator")
    @SequenceGenerator(name = "user_data_id_seq_generator", sequenceName = "user_data_id_seq", allocationSize = 10)
    @Column(name = "id", updatable = false)
    private Long id;

    @OneToMany(
            mappedBy = "user",
            cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    private List<Post> posts = new ArrayList<>();

    @OneToMany(
            mappedBy = "id",
            cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    private List<PostLike> likedPosts = new ArrayList<>();

当我尝试运行并映射它时,出现以下异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unable to map collection com.budano.SocialStudent.model.Post.likes
.
.
.
Caused by: org.hibernate.AnnotationException: Unable to map collection com.budano.SocialStudent.model.Post.likes
.
.
.
Caused by: org.hibernate.cfg.RecoverableException: Unable to find column with logical name: post_id in org.hibernate.mapping.Table(public.post) and its related supertables and secondary tables

有人可以解释一下这个日志告诉我什么以及我应该如何解决这个问题吗? 谢谢!

最佳答案

这是您可以尝试的一种方法:

帖子喜欢 ID:

@Embeddable
public class PostLikeId implements Serializable {

    @Column(name = "post_id")
    private Long postId; // corresponds to PK type of Post

    @Column(name = "user_id")
    private Long userId; // corresponds to PK type of User

    // ...

}

帖子喜欢:

@Entity
@Table(name = "post_like")
public class PostLike {

    @EmbeddedId
    private PostLikeId id;

    @MapsId("postId") // maps postId attribute of embedded id
    @ManyToOne
    private Post post;

    @MapsId("userId") // maps userId attribute of embedded id
    @ManyToOne
    private UserData user;

    // ...
}

帖子:

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

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "post_id_seq_generator")
    @SequenceGenerator(name = "post_id_seq_generator", sequenceName = "post_id_seq", allocationSize = 10)
    @Column(name = "id", updatable = false)
    private Long id;

    @ManyToOne
    private UserData user;

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

    // ...
}

用户数据:

@Entity
@Table(name = "user_data")
public class UserData {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_data_id_seq_generator")
    @SequenceGenerator(name = "user_data_id_seq_generator", sequenceName = "user_data_id_seq", allocationSize = 10)
    @Column(name = "id", updatable = false)
    private Long id;

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Post> posts = new ArrayList<>();

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<PostLike> likedPosts = new ArrayList<>();

    // ...
}

JPA 2.2 spec 中讨论了派生身份(并附有示例)在第 2.4.1 节中。

关于java - 如何映射这个复合主键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60048301/

相关文章:

java - 有条件地遍历 JSON 列表并将值反序列化为 Java POJO 字符串

java spring BCryptPasswordEncoder "SecureRandom random"构造函数参数

java - 单点登录在 Spring Web 应用程序中不起作用

java - Ebean 中的条件@OnetoMany 关系

java - iOS 的 Java 编程有什么进展吗?

java - 无法发布使用 eclipse 在 java 中创建的 Web 服务

java - 我想在另一个 Activity 中将图像传递到我的 ImageView ?

spring - Spring Batch 的问题

java - 我应该将时区与 Postgres 和 JDBC 的时间戳分开存储吗?

postgresql - 我应该在 PostgreSQL 还是服务器后端过滤数据?