0

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?

3
  • Looks more like a @ManyToMany relation instead of @OneToMany Commented Nov 8, 2018 at 10:16
  • please include complete entity code Commented Nov 8, 2018 at 10:16
  • 1
    @Selaron Well I guess I completely forgot that. That is actually the answer. Could youu please write it as an answer so I can accept it? Thank you! Commented Nov 8, 2018 at 10:18

1 Answer 1

4

You defined a @OneToMany relation between Products and Ingredients which is in fact a @ManyToMany.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.