Null不允许,但应该可以为空

时间:2013-04-24 00:56:43

标签: mysql hibernate jpa h2

我有两个表连接映射表。我收到了一个空约束问题。下面是错误消息和两个映射。由于两者都很多,我的假设是许多可能没有,我怎么能这样做,所以product_id或category_id可以为null?

错误消息

Caused by: org.h2.jdbc.JdbcBatchUpdateException: NULL not allowed for column "PRODUCT_OPTION_ID"; SQL statement:
insert into ImageMapping (product_id, image_id) values (?, ?) [23502-168]

分类

@JoinTable(
        name = "ImageMapping",
        joinColumns = @JoinColumn(name = "category_id"),
        inverseJoinColumns = @JoinColumn(name = "image_id")
)
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
private Set<Image> categoryImageId;

产品

@JoinTable(
        name="ImageMapping",
        joinColumns = @JoinColumn(name = "product_id"),
        inverseJoinColumns = @JoinColumn(name = "image_id")
)
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
private Set<Image> productImageGroup;

产品选项

@JoinTable(
        name="ImageMapping",
        joinColumns = @JoinColumn(name = "product_option_id"),
        inverseJoinColumns = @JoinColumn(name = "image_id")
)
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<Image> productOptionImageGroup;

1 个答案:

答案 0 :(得分:1)

对于每个关联,您应该有一个单独的连接表,而不是尝试为所有关联使用相同的连接。