org.hibernate.PersistentObjectException多对一关系

时间:2018-04-16 13:04:54

标签: java spring hibernate spring-boot jpa

运行应用程序时,我从MockData收到org.hibernate.PersistentObjectException错误。是什么造成的?我的cascadetypes错了吗?

  

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

@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");

        categoryRepository.save(IT);
        categoryRepository.save(beauty);
        categoryRepository.save(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);

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

1 个答案:

答案 0 :(得分:0)

将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);
    }

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