2つのエンティティ間に@ManyToMany
の関係を作成しようとしています。ManyToMany RelationShipカラムにNULLを使用できません
ポストエンティティ:
@Entity
@Table(name="Post")
public class Post {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "post_label", joinColumns = @JoinColumn(name = "POST_ID", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "LABEL_ID", referencedColumnName = "id"))
List<Label> labels = new ArrayList<Label>() ;
// getters and setters
}
そしてラベルエンティティ:
@Entity
@Table(name="Label")
public class Label{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
long id;
String name;
String color;
@ManyToMany(mappedBy = "labels")
List<Post> posts = new ArrayList<Post>() ;
// getters and setters.
}
私はPost
を保存しようとすると、このエラーが表示されます:
org.h2.jdbc.JdbcSQLException: NULL not allowed for column "LABEL_ID"; SQL statement:
insert into noticia (id, date, description, facebookid, postedonfacebook, postedonfake, publishdate, subtitle, title, type, visitorscount) values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [23502-194]
および類似エラー
org.h2.jdbc.JdbcSQLException: NULL not allowed for column "NOTICIA_ID"; SQL statement:
insert into label (id, color, name) values (null, ?, ?) [23502-194]
ポイント1:私はLabel
を保存しようとすると、私は何のLabel
(同じエラー)を添加しないなしPost
を保存しようとしました。 私はPost
(同じエラー)
ポイント2を添加しないことなくLabel
を保存しようとした:私はPost
追加Label
(同じエラー)を保存しようとしました。 Label
を保存しようとしましたPost
(同じエラー)
'strategy = GenerationType.AUTO' –
の代わりに' strategy = GenerationType.IDENTITY'を使用しようとしたとします。 –