Why Spring Boot findById returns null while findByIdAndStatus return record?

Viewed 299

Spring boot 2.2.2 ~ 2.2.4 ( the one i have tested so far)

I have an Abstract class

@MappedSuperclass
@Data
@EntityListeners(AuditingEntityListener.class)
@FilterDef(name = "tenantFilter", parameters = {@ParamDef(name = "tenantId", type = "int")})
@Filter(name = "tenantFilter", condition = "tenantId = :tenantId")
public abstract class BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    ....
}

A personnel class extending the abstract class

@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "personnel")
@AllArgsConstructor
@NoArgsConstructor
@Data
@EntityListeners(AuditingEntityListener.class)
public class Personnel extends BaseEntity {

    @NotNull
    @OneToOne
    @JoinColumn(name = "titleID")
    private Title title;

    // other fields and relationships of type ManyToOne, OneToMany
}

My Jpa Repository class

public interface PersonnelRepository extends JpaRepository<Personnel, Integer> {
    Optional<PersonnelData> findByFileNo(String fileNo);
    Optional<PersonnelData> findByIdAndDeletedOnIsNull(Integer id);
}

In my database, I have personnel records with some of the fields being null and the relationships.

In my controller when i do findById(1), it returns null but when I do findByIdAndDeletedOnIsNull(1), it returns the record.

I have tried @NotFound(action = NotFoundAction.IGNORE) on the relationships that are null with no success.

0 Answers
Related