我正在用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课程:

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

    @EmbeddedId
    private PostLikeId id;

    //getters and setters
}


我的帖子课程:

@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


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

最佳答案

您可以尝试以下方法:

PostLikeId:

@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<>();

    // ...
}


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

07-26 07:42