detached entity passed to persist – springboot

huangapple go评论70阅读模式
英文:

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 = &quot;sales_order&quot;)
@EntityListeners(AuditingEntityListener.class)
public class SalesOrder extends Auditable&lt;String&gt;  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 = &quot;customer_id&quot;, nullable = false)
	private Customer customer;
	

@Entity
@Table(name = &quot;customer&quot;)
@EntityListeners(AuditingEntityListener.class)
public class Customer extends Auditable&lt;String&gt;  implements Serializable {
	

	private static final long serialVersionUID = 1L;

	@Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
	private Long id;
	
	@OneToOne(mappedBy=&quot;customer&quot;,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) .

huangapple
  • 本文由 发表于 2023年1月8日 22:49:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定