猿问

我厌倦了用 JpaRepository 做点什么但是找不到行 findById ,

我试图通过 idfindById方法获取行,但它没有成功,

无法在 DB 中找到行。当我findAll之前打电话时findById工作正常。我使用 PostgreSQL。


public void updateOffer(OrderOfferDTO orderOfferDTO) {


  //        List<OrderOfferEntity> all = orderOfferRepository.findAll();

        OrderOfferEntity orderOfferEntity = orderOfferRepository.findById(orderOfferDTO.getId()).orElse(null);


        assert(orderOfferEntity != null);


        if (!isCanOfferUpdate(orderOfferEntity)) return;


        orderOfferEntity.setNumberOffer(orderOfferEntity.getNumberOffer() + 1);

        orderOfferEntity.setOfferDelivery(orderOfferDTO.getOfferDelivery());

        orderOfferEntity.setOfferPickup(orderOfferDTO.getOfferPickup());


        orderOfferRepository.save(orderOfferEntity);

    }

资料库


@Repository

public interface OrderOfferRepository extends JpaRepository<OrderOfferEntity, Long> {

    OrderOfferEntity findByUserEntityAndOrderEntity(UserEntity userEntity, OrderEntity orderEntity);


    List<OrderOfferEntity> findByUserEntityNotAndOrderEntity(UserEntity supplierEntity, OrderEntity orderEntity);

    List<OrderOfferEntity> findByOrderEntity(OrderEntity orderEntity);

    List<OrderOfferEntity> findByUserEntity(UserEntity userEntity);

}


实体


@Entity

@Table(name = "AUCTION_SUPPLIER")

@Data

public class OrderOfferEntity {


    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private Long id;


    @Column(name = "NUMBER_OFFER")

    private Integer numberOffer;


    @Column(name = "OFFER_PICKUP")

    private Integer offerPickup;


    @Column(name = "OFFER_DELIVERY")

    private Integer offerDelivery;


    @Column(name = "IS_ACCEPTED")

    private Boolean isAccepted;


    @ManyToOne(optional = false)

    @JoinColumn(name = "DELIVERY_PROGRESS_ID")

    private DeliveryProgressEntity deliveryProgressEntity;


    @ManyToOne

    @JoinColumn(name = "SUPPLIER_ID", nullable = false)

    private UserEntity userEntity;


    @ManyToOne

    @JoinColumn(name = "CUSTOMER_ORDER_ID", nullable = false)

    private OrderEntity orderEntity;


    @ManyToOne

    @JoinColumn(name = "FACTORY_ID", nullable = false)

}


动漫人物
浏览 159回答 1
1回答

喵喵时光机

您所有的 ManyToOne 关系都是非可选的或非空的。所以 Hibernate 生成 INNER JOINS。您必须确保您的数据遵循此规则。如果您定义非可选或非空关系,您还应该对数据库表中的外键设置 NOT NULL 约束。
随时随地看视频慕课网APP

相关分类

Java
我要回答