0

我有两个表加入一个映射表。不过,我遇到了一个空约束问题。下面是错误消息和两个映射。由于两者都是 manyTomany,我的假设是 many 可能没有,我怎样才能做到这样 product_id 或 category_id 都可以为空?

错误信息

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;
4

1 回答 1

1

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

于 2013-04-24T12:19:04.457 回答