'Hibernate’s “Object References an Unsaved Transient Instance” Error
I recently startet Programming a Back-End in Spring Boot. When implementing the Entitys and their Relationships I soon faced Hibernate’s “Object References an Unsaved Transient Instance” Error. I searched for answers in this forum and tried to fix my Cascade Annotation with this Tutorial: https://www.baeldung.com/hibernate-unsaved-transient-instance-error (which is addressing the specific Error). But when I start my Project and fill in Hard Code Data with the init() method I always Face the Error again:
org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : *.structure.Cart.customer -> *.structure.Customer; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : *.structure.Cart.customer -> *.structure.Customer
@Entity
@Table(name="arti")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Article {
@Id
@GeneratedValue
@Column(name = "id")
private Long articleId;
private String name;
private String manufactor;
private float price;
@ManyToMany
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name = "cart_id")
private Set<Cart> articleCart= new HashSet<>();
@ManyToMany
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name = "order_id")
private Set<Cart> articleOrder= new HashSet<>();
}
@Entity
@Table(name="cust")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Customer {
@Id
@GeneratedValue
@Column(name = "id")
private Long customerId;
@Column(name = "name")
private String name;
@Column(name = "address")
private String address;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "cart_id", referencedColumnName = "id")
private Cart cart;
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Order> orders = new HashSet<>();
}
@Entity
@Table(name="cart")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Cart {
@Id
@GeneratedValue
@Column(name = "id")
private Long cartId;
@OneToOne(mappedBy = "cart")
private Customer customer;
@ManyToMany
@JoinColumn(name = "article_id")
private Set<Article> articles= new HashSet<>();
}
@Entity
@Table(name="orde")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Order {
@Id
@GeneratedValue
@Column(name = "id")
private Long orderId;
@ManyToOne
@Cascade(CascadeType.SAVE_UPDATE)
@JoinColumn(name = "customer_id")
private Customer customer;
@ManyToMany
@JoinColumn(name = "article_id")
private Set<Article> articles= new HashSet<>();
}
@SpringBootApplication
public class GqlApplication {
@Autowired
private ArticleRepository articleRepository;
@Autowired
private CartRepository cartRepository;
@Autowired
private CustomerRepository customerRepository;
@Autowired
private OrderRepository orderRepository;
public static void main(String[] args) {
SpringApplication.run(GqlApplication.class, args);
}
@PostConstruct
public void init() {
Article dress = Article.builder()
.name("red dress")
.manufactor("Prada")
.price(20)
.build();
Article hat = Article.builder()
.name("brown hat")
.manufactor("Gucci")
.price(280)
.build();
Set<Article> articles = new HashSet<>();
articles.add(dress);
articles.add(hat);
Customer tom = Customer.builder()
.name("tom")
.address("Am Baum 3")
.build();
Cart cart = Cart.builder()
.customer(tom)
.articles(articles)
.build();
Order order = Order.builder()
.customer(tom)
.articles(articles)
.build();
Set<Order> orders = new HashSet<>();
orders.add(order);
tom.setCart(cart);
tom.setOrders(orders);
articleRepository.save(dress);
articleRepository.save(hat);
cartRepository.save(cart);
customerRepository.save(tom);
orderRepository.save(order);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
