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;