Hibernate 不持久嵌套关系
Hibernate is not persisting nested relationship
我有 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
我不需要 mappedBy,它实际上是一个拼写错误 - 我写了 play_representation_category 而不是 play_representation_actors。很愚蠢,是吧?至少我终于找到了:)
我有 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
我不需要 mappedBy,它实际上是一个拼写错误 - 我写了 play_representation_category 而不是 play_representation_actors。很愚蠢,是吧?至少我终于找到了:)