Reputation: 512
Using spring-data-jpa
.
@Entity
@Table(name="employees")
Class Employee{
.
.
.
.
}
public interface EmployeeRepository extends CrudRepository<Employee,Long> {
@Query(value = "SELECT * from employees where org_id=:orgId ",nativeQuery = true)
public List<Employee> findByOrgId(@Param("orgId") String orgId);
}
When am calling the repository function it behaves pretty strange.
In Employees table there are two records for org_id =18.
Scenario 1: When am passing 18 to this function am able to get a result which is of type List. Iterating it am able to get the expected result
Scenario 2 When am passing an org id which is not present in the DB the it returns me null.
Am handling it using checking for null.
Is checking for null is the right way? Should the result should return a empty list instead of null?
Upvotes: 4
Views: 7526
Reputation: 15908
List<Object>
is the return value of the method in the defined interface, the method should never return null
.Upvotes: 1