2

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.

3
  • 1
    If it isn't returning anything, it isn't there or you aren't using the database you think you are using. Commented Feb 12, 2020 at 14:04
  • I verified all this the data is there even database I am usijng is correct Commented Feb 12, 2020 at 14:08
  • 1
    I doubt it, else it would be returning something. The result isn't lying. So your applicaiton is either looking at a different database then you are looking at, or the data you see isn't persisted in the database (i.e. a non-committed transaction). Nonentheless, the result isn't lying. Commented Feb 12, 2020 at 14:10

1 Answer 1

2

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.