错误
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: com.xptraining.model.Representative
购物车控制器类
@POST
@Path(XpConstants.PLACE_ORDER)
@Consumes("application/json")
@Produces("application/json")
public Status placeOrder1(Carts carts) {
Representative r2 = carts.getRepresentative();
List<Sku> sList = carts.getListSku();
CartsOrderDto cartOrderDto = new CartsOrderDto();
Sku s = new Sku();
try {
cartOrderDto.setCartId(carts.getCartId());
cartOrderDto.setTotalBill(carts.getPrize());
cartOrderDto.setProductList(carts.getListSku());
cartOrderDto.setRepresentative(carts.getRepresentative());
Carts c = new Carts();
c.setCartId(carts.getCartId());
cartServices.deleteCart(carts.getCartId());
for (Sku sku : sList) {
s.setPrize(sku.getPrize());
s.setProduct(sku.getProduct());
s.setQuanity(sku.getQuanity());
s.setSize(sku.getSize());
s.setSkuId(sku.getSkuId());
s.setSkuName(sku.getSkuName());
s.setUnitName(sku.getUnitName());
skuServices.addSku(s);
}
Orders orders = new Orders();
orders.setCurrentDate(Utilities.getTodaysDate());
orders.setStatus("Active");
orders.setRepresentative(r2);
orders.setListSku(sList);
representativeServices.addRepresentative(r2);
orderServices.saveOrders(orders);
logger.info("Record saved in order and deleted from cart sucessfully");
return new Status(1, XpConstants.RECORD_SAVED);
} catch (Exception e) {
logger.error(XpConstants.ERROR_OCCURED, e);
}
return new Status(1, XpConstants.PLACED_WRONG_ID);
}
实体类:
Cart.java
@Entity
@Table(name = "carts")
public class Carts {
@Id
@Column(name = CARTS_ID)
private long cartId;
@Column(name = PRIZE)
private int prize;
@ElementCollection(targetClass=Product.class)
@OneToMany(cascade = CascadeType.ALL , fetch=FetchType.EAGER )
@JoinTable(
name = "CARTS_SKU",
joinColumns = { @JoinColumn(name = CARTS_ID) },
inverseJoinColumns = { @JoinColumn(name = SKU_ID) }
)
private List<Sku> listSku;
@OneToOne(cascade=CascadeType.ALL ,fetch=FetchType.EAGER)
@JoinColumn(name=REPRESENTATIVE_ID)
private Representative representative;
}
Orders.java
@Entity
@Table(name = Orders.TABLE_NAME)
public class Orders {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = Order.ORDER_ID)
private long ordersId;
@Column(name = CURRENT_DATE)
private Date currentDate;
@Column(name = STATUS)
private String status;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = REPRESENTATIVE_ID)
private Representative representative;
@LazyCollection(LazyCollectionOption.FALSE)
@ElementCollection(targetClass = Product.class)
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "ORDERS_SKU",
joinColumns = { @JoinColumn(name = ORDER_ID) },
inverseJoinColumns = { @JoinColumn(name = SKU_ID) })
private List<Sku> listSku;
}
Representative.java
@Entity
@Table(name = Representative.TABLE_NAME)
public class Representative implements java.io.Serializable {
@Id
@GeneratedValue (strategy=GenerationType.AUTO)
@Column(name = REPRESENTATIVE_ID)
private long representativeId;
@Column(name = FIRST_NAME)
private String representativeFirstName;
@Column(name = LAST_NAME)
private String representativeLastName;
@Column(name = EMAIL)
private String representativeEmail;
@Column(name = PASSWORD)
private String representativePassword;
@Column(name = EMPID)
private int representativeEmpId;
@JsonIgnore
@OneToOne(mappedBy="representative", cascade=CascadeType.PERSIST , fetch=FetchType.EAGER)
private Orders orders;
@JsonIgnore
@OneToOne(mappedBy="representative", cascade=CascadeType.ALL , fetch=FetchType.EAGER)
private Carts carts;
}
sku.java
@Entity
@Table(name = Sku.TABLE_NAME)
public class Sku implements java.io.Serializable {
@Id
@Column(name = SKU_ID)
private long skuId;
@Column(name = SKU_NAME)
private String skuName;
@Column(name = UNIT_NAME)
private String unitName;
@Column(name = SIZE)
private String size;
@Column(name = PRIZE)
private int prize;
@Column(name = QUANTITY)
private int quanity;
@ManyToOne(fetch = FetchType.EAGER , cascade=CascadeType.ALL)
@JoinColumn(name = PRODUCT_ID , nullable = false)
private Product product;
}
Product.java
@Entity
@Table(name = Product.TABLE_NAME)
public class Product implements java.io.Serializable {
@Id
@GeneratedValue (strategy=GenerationType.AUTO)
@Column(name = PRODUCT_ID)
private long productId;
@Column(name = PRODUCT_NAME )
private String productName;
@Column(name = SPECIALITY_ID)
private int specialtyId;
@LazyCollection(LazyCollectionOption.FALSE)
@ElementCollection(targetClass=Product.class)
@OneToMany(mappedBy="product" , cascade=CascadeType.ALL)
private List<Sku> listSkuOrders = new ArrayList<Sku>();
}
还有两个额外的表由@JoinTable注释创建 我是1.carts_sku 2.orders_sku
购物车的服务impl方法:
/**
* Delete Cart
*
* @param id
* cart id
* @throws Exception
*/
@Override
@Transactional
public void deleteCart(long id) throws Exception {
cartsRepository.delete(id);
}
订单的服务impl方法:
@Override
@Transactional
public Orders saveOrders(Orders orders) {
return ordersRepository.save(orders);
}
当我按购物车ID删除购物车记录时,它会被删除但是它无法保存到显示以下错误的订单
org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.xptraining.model.Representative; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.xptraining.model.Representative
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: com.xptraining.model.Representative
答案 0 :(得分:1)
这里的问题是你试图坚持一个分离的对象,在你的汽车课上你有
-c
由于级联类型是全部,所有操作都将级联,包括删除。
@OneToOne(cascade=CascadeType.ALL ,fetch=FetchType.EAGER)
@JoinColumn(name=REPRESENTATIVE_ID)
private Representative representative;
删除购物车时,代表将被分离(因为您仍然持有对它的引用)
当您将其添加到订单并保存问题时。
要解决此问题,您需要重新附加对象或更改级联类型以保持
答案 1 :(得分:1)
我认为carts
中的placeOrder1()
已分离,因为它是一个JSON请求。因此carts.representative
也是分离的。您可能需要通过carts.representative
或session.merge()
(可能通过有权访问entityManager.merge()
的服务类)将session
附加到会话中。请参阅https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/Session.html用于重新附加的其他选项。
在尝试将carts.representative
Order
作为{{1}}
答案 2 :(得分:1)
将CascadeType.ALL
更改为CascadeType.MERGE
CascadeType.ALL
尝试删除引用并使用新ID重新插入,这会导致错误,但CascadeType.MERGE
尝试使用相同的引用并保留引用,然后更新新数据任何