I am unable to to get a list of nested objects using a JpaRepository. I'll try to explain what I want using the following code:
AutoService entity:
@Entity
public class AutoService {
@Id
private long id;
@Column(name = "serviceName", nullable = false)
private String serviceName;
}
Service entity:
@Entity
public class Service {
@Id
private long serviceId;
@Column(name = "serviceName", nullable = false)
private String serviceName;
@Column(name = "category", nullable = false)
private String category;
@ManyToOne
@JoinColumn(name = "autoServiceId", nullable = false)
private AutoService autoService;
}
ServiceRepository interface:
public interface ServiceRepository extends JpaRepository<Service, Long> {
List<Service> findByServiceNameAndCategory(String autoServiceName, String categoryName);
}
Business logic:
@org.springframework.stereotype.Service
public class ServiceServiceImpl implements ServiceService {
@Autowired
private ServiceRepository serviceRepository;
@Override
public List<Service> findByAutoServiceAndCategory(String autoServiceName, String serviceCategory) {
return serviceRepository.findByServiceNameAndCategory(autoServiceName, serviceCategory);
}
}
As I am expecting, the code above is unable to provide the desired list of Services matching the provided category and AutoService names.
Can someone provide advice on how should I use my repository to get list of nester services by: autoServiceName and serviceCategory please?
EDIT:
Right now I am using the custom query.
I am using autoServiceId instead of service name right now.
But for some reason I am getting empty list of objects.
Here is my JPA Repo.
public interface ServiceRepository extends JpaRepository<Service, Long> {
@Query("SELECT s from Service s where s.autoService.id = :autoServiceId and s.category = :categoryName")
List<Service> findByServiceNameAndCategory(@Param("autoServiceId") Long autoServiceId, @Param("categoryName") String categoryName);
}
Any suggestions please ?
I think i know the answer. Problem in my category, sended to the server. I wrote it on Russian language. And encoding broken value of category on server side.
