简体   繁体   中英

Save an object in multiple other objects using hibernate

I currently try to make a simple Catalog web view and I am using Spring and Hibernate to save the data. I want to have a Template which is a Product and in this Template there should be multiple Ingredients which are also Products .

These are my classes:

Template.java

@Entity
public class Template extends Product implements Serializable {

    @OneToMany(fetch=FetchType.LAZY, cascade = {CascadeType.ALL, CascadeType.MERGE, CascadeType.PERSIST})
    private List<Ingredient> ingredients = new ArrayList<>();

    public Template(String name, Money price, ArrayList<Ingredient> ingredients) {
        super(name, price);
        this.ingredients.addAll(ingredients)
    }

    // Getters and setters...

}

Ingredient.java

@Entity
public class Ingredient extends Product implements Serializable {
    public Ingredient(String name, Money price) {
        super(name, price);
    }
}

Product.java

This class comes from the salespoint framework. It handles saving of the name and price but it also handles things like the ID for Hibernate.

public void initialize()

This method provides dummy data for testing purposes.

public void initialize() {
    Ingredient cheese = new Ingredient("Cheese", Money.of(1, "EUR");
    Ingredient bacon = new Ingredient("Bacon", Money.of(1, "EUR");
    Ingredient tomato = new Ingredient("Tomato", Money.of(1, "EUR");

    // add these ingredients into ArrayLists (3 Lists)
    // list1: tomato, bacon
    // list2: bacon, tomato, cheese
    // list3: cheese, tomato

    Template pizza1 = new Template("Pizza 1", Money.of(1, "EUR"), list1);
    Template pizza2 = new Template("Pizza 2", Money.of(1, "EUR"), list2);
    Template salad = new Template("Salad", Money.of(1, "EUR"), list3);

    // saving these in a catalog
}

When running this code I always get an error of this sort:

Unique index or primary key violation: "UK_TAGDVK3J2NBLU2MQMBL8HBJV9_INDEX_1 ON PUBLIC.PRODUCT_INGREDIENTS(INGREDIENTS_PRODUCT_ID) VALUES ('f6f05610-ca1f-40b6-b0a8-abaccbc0452b', 2)"; SQL statement:
insert into product_ingredients (template_product_id, ingredients_product_id) values (?, ?) [23505-197]

I interpret this error like this: Hibernate tries to create a table to link templates with ingredients but the ingredients keys appear more than once.

When I tried adding only one ingredient per list (so one lsit with only cheese, one with bacon, ...) it works perfectly fine.

I don't quite know how to search for this topic and I tried googling it but I did not find anything yet. What changes do I have to make to make this code work, so that one Ingredient Object can appear in multiple Templates?

您在ProductsIngredients之间定义了@OneToMany关系,实际上是@ManyToMany

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM