Home org.hibernate.PersistentObjectException Many-to-one relationship
Reply: 0

org.hibernate.PersistentObjectException Many-to-one relationship

user2879
1#
user2879 Published in April 25, 2018, 6:14 pm

When running the application I get a org.hibernate.PersistentObjectException error from MockData. What is causing this? Are my cascadetypes wrong?

org.hibernate.PersistentObjectException: detached entity passed to persist

The products are created and added after the categories, so the category should exist?

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);
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.314057 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO