弹簧靴 2.5
@Entity
public class Orders {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@NotNull
@OneToMany(mappedBy = "orders", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
private Set<ProductEntry> productEntities = new HashSet<>();
@Entity
public class Cart {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@OneToMany(mappedBy = "cart", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
private Set<ProductEntry> productEntities = new HashSet<>();
@Entity
public class ProductEntry {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Exclude
private int id;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@ManyToOne(fetch = FetchType.LAZY)
private Orders orders;
@Exclude
@ManyToOne(fetch = FetchType.EAGER, optional = false)
private Cart cart;
我的控制器:
@PostMapping("/order/cart")
public Response createOrderByCart(@RequestBody Map<String, Object> payloadMap) {
int cartId = (int) payloadMap.get("cart_id");
Optional<Cart> findCart = cartRepository.findById(cartId);
if (findCart.isPresent()) {
final Orders order = new ObjectMapper().convertValue(payloadMap.get("order"), Orders.class);
Cart cart = findCart.get();
Set<ProductEntry> productEntities = cart.getProductEntities();
order.setProductEntities(productEntities);
double orderTotalAmount = cart.getTotalAmount();
ordersRepository.save(order);
return ResponseService.getSuccessResponse(GsonUtil.gson.toJson(order));
} else {
String errorMessage = "Not found cart with id " + cartId;
logger.warn(errorMessage);
return ResponseService.getErrorResponse(errorMessage);
}
}
但我在这一行得到错误:
ordersRepository.save(order);
这里错误
2020-04-13 23:19:42.225 INFO 9289 --- [nio-8092-exec-7] r.o.s.e.controller.OrderController : createOrderByCart: payloadMap: {cart_id=1, order={paymentCardNumber=1111-2222-3333-4444, shipping={price=5.0, currency=USD, contactName=Some name here, address=Some address here, country=Russia, city=Moscow}, promoCode=ABC}}
2020-04-13 23:19:42.236 INFO 9289 --- [nio-8092-exec-7] r.o.s.e.controller.OrderController : createOrderByCart: use_promo_code_discount
2020-04-13 23:19:42.248 ERROR 9289 --- [nio-8092-exec-7] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/api/v1] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Found shared references to a collection: com.myproject.eshop_orders.api.model.Orders.productEntities; nested exception is org.hibernate.HibernateException: Found shared references to a collection: com.myproject.eshop_orders.api.model.Orders.productEntities] with root cause
org.hibernate.HibernateException: Found shared references to a collection: com.myproject.eshop_orders.api.model.Orders.productEntities
at org.hibernate.engine.internal.Collections.processReachableCollection(Collections.java:188) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.event.internal.FlushVisitor.processCollection(FlushVisitor.java:50) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:104) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:65) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.event.internal.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:59) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:182) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:232) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:92) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:102) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1352) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:443) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3202) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2370) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:447) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:183) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$300(JdbcResourceLocalTransactionCoordinatorImpl.java:40) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:281) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:101) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:534) ~[spring-orm-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:743) ~[spring-tx-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:711) ~[spring-tx-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:631) ~[spring-tx-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:385) ~[spring-tx-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) ~[spring-tx-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.2.5.RELEASE.jar:5.2.5.RELEASE]