【问题标题】:Hibernate is not persisting nested relationshipHibernate 没有保持嵌套关系
【发布时间】:2017-04-16 23:00:56
【问题描述】:

我有 4 个实体:Play、Actor、Play-representation 和 Category。 每部戏剧都属于一个类别,并且戏剧表现将一部戏剧与一个剧院和给定时间的许多演员联系起来。 以下是实体:

@Entity
@Table(name = "category")
public class Category {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "name")
    private String name;

    @OneToMany(mappedBy="category")
    private List<Play> playList = new ArrayList<Play>();

@Entity
@Table(name = "actor")
public class Actor {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "description")
    private String description;

    @Column(name = "profile_picture")
    private String profilePicturePath;

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "play_representation_category",
            joinColumns = {@JoinColumn(name = "actor_id")},
            inverseJoinColumns = {@JoinColumn(name = "play_representation_id")})
    private Set<PlayRepresentation> playRepresentations = new HashSet<>(0);

@Entity
@Table(name = "play")
public class Play {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @NotNull
    @Column(name = "name")
    private String name;

    @NotNull
    @Column(name = "description")
    private String description;

    @Column(name = "image_paths")
    private String imagePaths;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private Category category;

@Entity
@Table(name = "play_representation")
public class PlayRepresentation {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "play_id")
    private Play play;

    @OneToOne
    @JoinColumn(name = "theater_id")
    private Theater theater;

    @Column(name = "date")
    private Timestamp airingDate;

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "play_representation_category",
            joinColumns = {@JoinColumn(name = "play_representation_id", nullable=false)},
            inverseJoinColumns = {@JoinColumn(name = "actor_id", nullable=false)})
    private Set<Actor> actors = new HashSet<>(0);

我遇到的问题是 hibernate 试图找到 play_representation 和类别之间的关系!我一直在努力维持戏剧的关系,但似乎我错了,无法找出最好的方法……顺便说一句,这是一个 postgresql 数据库。

我还在学习中,所以如果您对我分享的代码有任何其他提示,请告诉我!

编辑:错误是:

org.postgresql.util.PSQLException: ERROR: relation "play_representation_category" does not exist
  Position: 281

【问题讨论】:

  • 错误信息是?!
  • 您的 ManyToMany 是双向的,但您尚未使用 mappedBy 链接它们

标签: java hibernate jpa persistence


【解决方案1】:

我不需要 mappedBy,它实际上是一个错字 - 我写的是 play_representation_category 而不是 play_representation_actors。很傻是吧?至少我终于找到了:)

【讨论】:

  • 如果该关系是双向的,那么您确实需要 mappedBy。否则是两个独立的关系,你以后会遇到问题
  • 好的,我去看看!谢谢!
猜你喜欢
  • 2017-09-30
  • 2021-06-26
  • 2010-11-19
  • 2017-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-28
  • 1970-01-01
相关资源
最近更新 更多