Hibernate不是持久的嵌套关系

时间:2017-04-16 23:00:56

标签: java hibernate jpa persistence

我有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

1 个答案:

答案 0 :(得分:0)

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