Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用hibernate将对象保存在多个其他对象中_Java_Hibernate - Fatal编程技术网

Java 使用hibernate将对象保存在多个其他对象中

Java 使用hibernate将对象保存在多个其他对象中,java,hibernate,Java,Hibernate,我目前尝试创建一个简单的目录web视图,并使用Spring和Hibernate保存数据。我想要一个模板,它是产品,在这个模板中应该有多个成分,它们也是产品 这些是我的课程: Template.java @Entity public class Template extends Product implements Serializable { @OneToMany(fetch=FetchType.LAZY, cascade = {CascadeType.ALL, CascadeType

我目前尝试创建一个简单的目录web视图,并使用Spring和Hibernate保存数据。我想要一个
模板
,它是
产品
,在这个模板中应该有多个
成分
,它们也是
产品

这些是我的课程:

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...

}
@Entity
public class Ingredient extends Product implements Serializable {
    public Ingredient(String name, Money price) {
        super(name, price);
    }
}
Product.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...

}
@Entity
public class Ingredient extends Product implements Serializable {
    public Ingredient(String name, Money price) {
        super(name, price);
    }
}
此类来自salespoint框架。它处理名称和价格的保存,但也处理Hibernate的ID之类的事情

public void initialize()

该方法为测试目的提供虚拟数据

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
}
运行此代码时,我总是会遇到这样的错误:

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]
我这样解释这个错误:Hibernate试图创建一个表来链接模板和配料,但是配料键出现了不止一次

当我尝试在每个列表中只添加一种成分时(一个lsit只添加奶酪,一个添加培根,…),效果非常好


我不太知道如何搜索这个主题,我试着在谷歌上搜索,但我还没有找到任何东西。我需要做哪些更改才能使代码正常工作,因此一个成分对象可以出现在多个模板中?

您定义了
产品
成分
之间的
@OneToMany
关系,实际上是
@manytomy
您定义了
产品
成分
之间的
关系事实a
@ManyToMany

看起来更像是一个
@ManyToMany
关系,而不是
@OneToMany
请包含完整的实体code@Selaron我想我完全忘了。这实际上就是答案。你能把它写下来回答我吗?非常感谢。看起来更像是一个
@manytomy
关系,而不是
@OneToMany
请包含完整的实体code@Selaron我想我完全忘了。这实际上就是答案。你能把它写下来回答我吗?非常感谢。