Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.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 org.hibernate.PersistentObjectException多对一关系_Java_Spring_Hibernate_Spring Boot_Jpa - Fatal编程技术网

Java org.hibernate.PersistentObjectException多对一关系

Java org.hibernate.PersistentObjectException多对一关系,java,spring,hibernate,spring-boot,jpa,Java,Spring,Hibernate,Spring Boot,Jpa,运行应用程序时,我从MockData中得到一个org.hibernate.PersistentObjectException错误。这是什么原因造成的?我的回答错了吗 org.hibernate.PersistentObjectException:传递给的分离实体 坚持 产品是在类别之后创建和添加的,那么类别应该存在吗 Product.java @Entity @Data @Table(name = "product") @JsonIgnoreProperties({"hibernateLazyI

运行应用程序时,我从MockData中得到一个org.hibernate.PersistentObjectException错误。这是什么原因造成的?我的回答错了吗

org.hibernate.PersistentObjectException:传递给的分离实体 坚持

产品是在类别之后创建和添加的,那么类别应该存在吗

Product.java

@Entity
@Data
@Table(name = "product")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "manufacturer")
    private String manufacturer;

    @Column(name = "price")
    private double price;

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "category_id")
    private Category category;

    public Product(String name, String manufacturer, double price, Category category) {
        this.name = name;
        this.manufacturer = manufacturer;
        this.price = price;
        this.category = category;
    }

    public Product() {
    }

}
Category.java

@Entity
@Data
@Table(name = "category")
public class Category {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Column(name = "name")
    private String name;

    @OneToMany(mappedBy = "category", cascade = CascadeType.ALL)
    private List<Product> products;

    public Category(String name) {
        this.name = name;
    }

    public Category() {
    }
}

将mockdata更改为以下选项解决了问题:

@Component
class MockData {

    private final ProductRepository productRepository;
    private final CategoryRepository categoryRepository;

    @Autowired
    public MockData(ProductRepository productRepository, CategoryRepository categoryRepository) {
        this.productRepository = productRepository;
        this.categoryRepository = categoryRepository;

        loadData();
    }

    private void loadData() {

        Category IT = new Category("IT");
        Category beauty = new Category("Beauty");
        Category food = new Category("Food");

        Product computer = new Product("Computer", "Dell", 5000, IT);
        Product computer2 = new Product("Computer2", "HP", 5000, IT);
        Product eyeliner = new Product("Eyeliner", "Chanel", 100, beauty);
        Product hamburger = new Product("Angus", "Burger King", 100, food);

        IT.setProducts(Arrays.asList(computer, computer2));
        beauty.setProducts(Collections.singletonList(eyeliner));
        food.setProducts(Collections.singletonList(hamburger));

        categoryRepository.save(IT);
        categoryRepository.save(beauty);
        categoryRepository.save(food);

        productRepository.save(computer);
        productRepository.save(computer2);
        productRepository.save(eyeliner);
        productRepository.save(hamburger);
    }

产品在保存之前会被创建并添加到类别中。

我已经在这里回答了一个类似的问题:我的猜测?尝试初始化您的
产品
收藏可能重复
@Component
class MockData {

    private final ProductRepository productRepository;
    private final CategoryRepository categoryRepository;

    @Autowired
    public MockData(ProductRepository productRepository, CategoryRepository categoryRepository) {
        this.productRepository = productRepository;
        this.categoryRepository = categoryRepository;

        loadData();
    }

    private void loadData() {

        Category IT = new Category("IT");
        Category beauty = new Category("Beauty");
        Category food = new Category("Food");

        Product computer = new Product("Computer", "Dell", 5000, IT);
        Product computer2 = new Product("Computer2", "HP", 5000, IT);
        Product eyeliner = new Product("Eyeliner", "Chanel", 100, beauty);
        Product hamburger = new Product("Angus", "Burger King", 100, food);

        IT.setProducts(Arrays.asList(computer, computer2));
        beauty.setProducts(Collections.singletonList(eyeliner));
        food.setProducts(Collections.singletonList(hamburger));

        categoryRepository.save(IT);
        categoryRepository.save(beauty);
        categoryRepository.save(food);

        productRepository.save(computer);
        productRepository.save(computer2);
        productRepository.save(eyeliner);
        productRepository.save(hamburger);
    }