Malik Yawar
Malik Yawar

Reputation: 21

Spring boot JPA no returning existing result using findById

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.

Upvotes: 2

Views: 1124

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 81998

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.

Upvotes: 2

Related Questions