2

I have an image mapping table that maps images to the various items that have images. This works as expected when I make the entries in the database by hand, and am retrieving, however when I try to do tests they fail with the below errors. I am using hibernate with h2 for test, and hibernate with mysql for dev.

Here are the errors the table creation is getting.

ERROR - Unsuccessful: create table ImageMapping (image_mapping_id integer not null, category_id integer, image_id integer, product_id integer, product_item_id integer, product_option_id integer generated by default as identity, primary key (product_option_id, image_id), unique (image_id))
ERROR - Attempt to define a second primary key; SQL statement:
create table ImageMapping (image_mapping_id integer not null, category_id integer, image_id integer, product_id integer, product_item_id integer, product_option_id integer generated by default as identity, primary key (product_option_id, image_id), unique (image_id)) [90017-168]

ImageMapping entity.

@JsonAutoDetect
@JsonIgnoreProperties(ignoreUnknown = true)
@Entity
@Table(name="ImageMapping")
public class ImageMapping {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "image_mapping_id")
    private int imageMappingId;

    @Column(name="product_option_id")
    private int productOptionId;

    @Column(name="product_item_id")
    private int productItemId;

    @Column(name="product_id")
    private int productId;

    @Column(name="image_id")
    private int imageId;

    @Column(name="category_id")
    private int categoryId;

Since the other field the error is mentioning is the product_option_id I am including that entity.

@JsonAutoDetect
@JsonIgnoreProperties(ignoreUnknown = true)
@Entity
@Table(name="ProductOptions")
public class ProductOptions implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "product_option_id")
    private int productOptionId;

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

    @JoinColumn(name = "image_id")
    @OneToOne
    private Image optionImage;

    @Column(name = "product_option_description")
    private String productOptionDescription;
4

1 回答 1

2

您似乎错误地使用了 JoinTable 注释。您实际上创建了两次 ImageMapping 表:一次用于您的实体,一次用于 ProductOptions 的 JoinTable 注释。

查看这篇文章,了解连接表注释的含义:JPA "@JoinTable" 注释

他给出了一个很好的例子来说明如何使用它。我不完全是您的域对象的用途,ImageMapping 中似乎发生了很多事情。但是使用 JoinTable 的一种方案可能是

Table: Image
id      integer
data    blob
name    varchar

Table: ProductOption
id      integer
desc    varchar
partnum varchar

Table: Image_Option
image_id     integer foreign key
prod_opt_id  integer foreign key

所以在这个例子中,我有 Image 和 ProductOption 的 Hibernate 实体,并且表 Image_Option 只会出现在 JoinTable 注释中。

于 2013-04-23T02:04:49.820 回答