java - Spring boot JPA不使用findById返回现有结果

标签 java hibernate spring-boot jpa spring-data-jpa

我使用 Oracle 数据库和一些 JPA 查询创建了一个非常小且简单的 Spring Boot 应用程序。

这是不返回数据的代码片段,该数据实际上存在于数据库中。

letterRecipientNonOas = letterRecipientNonOasRepository
                            .findById(Long.valueOf(letterRecipientDTO.getNonOas().getId()))
                            .orElseThrow(() -> new EntityNotFoundException(LetterRecipientNonOas.class,
                                    Constant.MESSAGE_ENTITY_NOT_FOUND));

此处 findById 返回空结果集。

这是我的存储库

package com.care.document.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import com.care.document.model.LetterRecipientNonOas;

/**
 * The Interface LetterRecipientNonOasRepository.
 */
@Repository
public interface LetterRecipientNonOasRepository extends PagingAndSortingRepository<LetterRecipientNonOas, Long> {

    Optional<LetterRecipientNonOas> findByLetterId(Long id);

    Optional<LetterRecipientNonOas> findByTitleIgnoreCase(String title);

    List<LetterRecipientNonOas> findByTitleContainingIgnoreCase(String title);

    List<LetterRecipientNonOas> findAllByTitleIgnoreCaseAndIdNot(String title, Long recipientId);

    List<LetterRecipientNonOas> findAllByIdAndLetterId(long id, long letterId);

}

这是我的模型类:

package com.care.document.model;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;

import org.springframework.lang.Nullable;

import com.care.admin.model.BaseEntity;
import com.care.admin.util.CommonUtil;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.FieldDefaults;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Entity
@Table(name = "letter_recipient_non_oas")
public class LetterRecipientNonOas extends BaseEntity implements Serializable {

    @Id
    Long id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "letter_id")
    Letter letter;

    Integer recipientType; // Action/Info

    //byte recipientSubType; // Internal/External/NonOAS

    byte recipientCategory; //Internal/External
    int orderNo;
    String title;

    @Nullable
    String remarks;

    String address;

    @PrePersist
    private void prePersist() {
        this.id = CommonUtil.generateID(this.atRegion);
    }
}

我测试过,尝试过不同的方法,但没有用。

最佳答案

有几种情况可能会让人产生这种印象:

  1. 您正在查看错误的数据库。
  2. 当您尝试加载数据时,数据尚未存在,但当您检查时数据已​​存在。 众所周知,JPA 缓存可以非常有效地创建此类场景。
  3. 数据看起来与您想象的有些不同。这可能是由不可见或容易错过的内容(例如空格甚至控制字符)引起的。
  4. 您在创建数据的事务中或使用允许脏读的 session 检查数据库,并且创建数据的插入尚未提交。

关于java - Spring boot JPA不使用findById返回现有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60189890/

相关文章:

java - UserRecoverableAuthIOException 无意使用 Drive SDK for Android

java - 行大小太大 (> 8126) hibernate

java - Wildfly + Hibernate+ Log4J2 -> 不记录 hibernate 内容

java - 如何以与实现无关的方式在 JPA 中创建 Clob

java - Spring RestTemplate : Exponential Backoff retry policy

java - 在 Spring boot 应用程序项目中使用 Wildfly

java - 为什么我的休息时间不起作用

java - 贪吃蛇游戏——无法驾驭蛇

java - 在 hibernate 中创建序列 ID

java - Spark Streaming作业如何在Kafka主题上发送数据并将其保存在Elastic中