首页 > 解决方案 > 保存功能上的 EntityNotFoundException

问题描述

我正在尝试使用一对多关系保存实体。EntityNotFoundException尝试保存实体时抛出

我做了一些研究,但找不到任何解决方案

订单实体:

@Data
@Component
@Entity
@Slf4j
@Table (name = ORDER_TABLE_NAME)
public class OrderEntity {

//== Order Details
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    @Column(name = ORDER_ID_COLUMN)
    private String orderIdGuid;

    @Column (name = ORDER_BOOKED_DATETIME_COLUMN)
    private LocalDateTime orderBookedDate;

    @Column(name = ORDER_STATUS_COLUMN)
    private String orderStatus;

    @OneToMany( cascade={CascadeType.ALL}, orphanRemoval = true)
    @JoinColumn(name= ORDER_ID_COLUMN)
    @Autowired
    private List<ProductEntity> products;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = ORDER_ID_COLUMN)
    private List<Followup> followups;

产品实体:

@Data
@Component
@Entity
@Table (name=ORDER_PRODUCT_TABLE)
public class ProductEntity {

    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    @Column (name = PRODUCT_PRIMARY_ID)
    private String product_primary_id;

    //=== defined by constants
    @Column(name = PRODUCT_NAME)
    private String productName;

DAO类:

@Component
@Data
@Slf4j
public class DAOOrderRepository {

    @Autowired
    OrderRepository orderRepository;

    public void saveOrderEntity(OrderEntity orderEntity) {
        orderEntity.setOrderUpdatedDate(LocalDate.now());
        orderRepository.save(orderEntity);
    }

    public OrderEntity findOrderByOrderId(String orderId) {
        return orderRepository.findOrderEntityByOrderIdGuid(orderId);
    }

    public List<OrderEntity> findAllOrdersByEmployeeId(String employeeId) {
        return orderRepository.findBySalesRepEmployeeId(employeeId);
    }
}

存储库:

@Repository
public interface OrderRepository extends JpaRepository<OrderEntity,String> {

    List<OrderEntity> findBySalesRepEmployeeId(String employeeId);

    OrderEntity findOrderEntityByOrderIdGuid(String orderIdGuid);
}

例外:

org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to 
find 
com.app.apicore.Entities.OrderServiceEntity.ProductEntity.ProductEntity 
with id 03350d2a-15cf-46da-ba2c-58aeec79db9a; nested exception is 
javax.persistence.EntityNotFoundException: Unable to find 
com.app.apicore.Entities.OrderServiceEntity.ProductEntity.ProductEntity 
with id 03350d2a-15cf-46da-ba2c-58aeec79db9a
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:378)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:138)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy111.save(Unknown Source)
at com.app.apicore.Apis.DAOOrderRepository.saveOrderEntity(DAOOrderRepository.java:24)

每个订单都是一个新的订单记录。因此,当我第一次尝试保存订单记录时,它会正确保存订单数据。它第二次正确保存订单数据。但是第三次​​抛出异常。

标签: springjpa

解决方案


推荐阅读