英文:
detached entity passed to persist - springboot
问题
销售订单保存对于现有客户不起作用,但对于新客户起作用
@Entity
@Table(name = "sales_order")
@EntityListeners(AuditingEntityListener.class)
public class SalesOrder extends Auditable<String> implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "customer_id", nullable = false)
private Customer customer;
@Entity
@Table(name = "customer")
@EntityListeners(AuditingEntityListener.class)
public class Customer extends Auditable<String> implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(mappedBy = "customer", cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
private SalesOrder salesOrder;
SalesOrder salesOrder = new SalesOrder();
Branch branch = BranchContextHolder.getCurrentBranch();
String nextSequence = null;
if (salesOrderDto.getId() == null) {
salesOrder = new SalesOrder(salesOrderDto);
salesOrder.setBranch(branch);
if (customerRepository.existsByPhone(salesOrderDto.getCustomer().getPhone())) {
Customer customer = customerRepository.findCustomerByPhone(salesOrderDto.getCustomer().getPhone());
customer.setSalesOrder(salesOrder);
salesOrder.setCustomer(customer);
}
nextSequence = sequenceService.getNextSequence(branch.getId(), module.getId());
salesOrder.setUid(nextSequence);
}
// 保存销售订单
salesOrder = salesOrderRepository.save(salesOrder);
在保存时出现错误:
detached entity passed to persist: com.jee.cms.app.customer.model.Customer; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.jee.cms.app.customer.model.Customer
我漏掉了什么吗?
英文:
Sales Order save not working for existing Customer but working for new Customer
@Entity
@Table(name = "sales_order")
@EntityListeners(AuditingEntityListener.class)
public class SalesOrder extends Auditable<String> implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
@OneToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
@JoinColumn(name = "customer_id", nullable = false)
private Customer customer;
@Entity
@Table(name = "customer")
@EntityListeners(AuditingEntityListener.class)
public class Customer extends Auditable<String> implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
@OneToOne(mappedBy="customer",cascade={CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST,CascadeType.REFRESH})
private SalesOrder salesOrder;
SalesOrder salesOrder = new SalesOrder();
Branch branch = BranchContextHolder.getCurrentBranch();
String nextSequense = null;
if(salesOrderDto.getId()==null) {
salesOrder = new SalesOrder(salesOrderDto);
salesOrder.setBranch(branch);
if(customerRepository.existsByPhone(salesOrderDto.getCustomer().getPhone())) {
Customer customer = customerRepository.findCustomerByPhone(salesOrderDto.getCustomer().getPhone());
customer.setSalesOrder(salesOrder);
salesOrder.setCustomer(customer);
}
nextSequense = sequenceService.getNextSequense(branch.getId(),module.getId());
salesOrder.setUid(nextSequense);
}
//save sales order
salesOrder = salesOrderRepository.save(salesOrder);
On save getting error:
detached entity passed to persist: com.jee.cms.app.customer.model.Customer; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.jee.cms.app.customer.model.Customer
Anything I am missing?
答案1
得分: 1
尝试使用@Transactional
注解您的方法。然后,Customer
在方法内部将不会变为游离状态。
英文:
Try annotating your method with @Transactional
. Then then Customer
won't get detached within the method.
答案2
得分: 0
这种类型的错误是因为具有相同的id(@id列值)的多个条目被找到而引发的。
英文:
This type of error arise when more then one entry found with same id (@id column value) .
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论