Spring Boot JPA没有使用findById返回现有结果 [英] Spring boot JPA no returning existing result using findById

查看:378
本文介绍了Spring Boot JPA没有使用findById返回现有结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Oracle数据库和一些JPA查询创建了一个非常小巧的Spring Boot应用程序.

I have created a pretty small and simple Spring Boot app using the Oracle database and some JPA queries.

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

This is the code snippet which is not returning data, which is actually exists in database.

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

此处findById返回空结果集.

here findById is returning empty result set.

这是我的存储库

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);
    }
}

我测试了,尝试了不同的方法,但是没有用.

I tested, tried different ways but of no use.

推荐答案

有几种方案可能会给人留下这样的印象:

There are a couple of scenarios how one might get this impression:

  1. 您正在查看错误的数据库.
  2. 当您尝试加载数据时,数据尚不存在,但在您检查数据时已存在. 众所周知,JPA缓存可以相当高效地创建此类方案.
  3. 数据看起来与您想象的有些不同.这可能是由于看不见或容易错过诸如空格甚至控制字符之类的内容引起的.
  4. 您在创建数据的事务内或通过允许脏读的会话检查数据库,并且尚未提交创建数据的插入.
  1. You are looking at the wrong database.
  2. The data isn't there yet when you try to load it, but is when you check. JPAs caches are known to create such scenarios rather efficiently.
  3. The data looks a little different than you think. This could be caused by invisible or easy to miss content like spaces or even control characters.
  4. You check the database within the transaction that created the data or with a session that allows dirty reads and the insert that created the data wasn't committed yet.

这篇关于Spring Boot JPA没有使用findById返回现有结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆