Spring boot JPA no returning existing result using findById

心不动则不痛 提交于 2021-02-11 02:45:31

问题


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

here findById is returning empty result set.

this is my repository

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

}

and this is my model class:

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.


回答1:


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

  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.


来源:https://stackoverflow.com/questions/60189890/spring-boot-jpa-no-returning-existing-result-using-findbyid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!