2

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. enter image description here

3 Answers 3

2

1- Use @Embedded and @Embeddable annotation accordingly on your entity object then your method will fetch nested object.

OR

2- @Query annotation is used for writing custom query please refer this link custom query reference

Sign up to request clarification or add additional context in comments.

Comments

2

You may have to write a query like this in your ServiceRepository.

public interface ServiceRepository extends JpaRepository<Service, Long> {

    @Query("SELECT s from Service s where s.autoService.serviceName  = :autoServiceName and s.category = :categoryName")
    Set<Round> getRoundsBySessionQuestionId(@Param("autoServiceName") String autoServiceName, @Param("categoryName") String categoryName);

}

Hope this helps. Happy coding !

2 Comments

Unfortunately I am getting empty list right now. I am using now autoServiceId not name. But for some reason I am getting empty list of data from database.
Were you able to solve the issue. The above query would literally solve it. If that worked please mark it as the correct answer since it helps someone who search the same in future. Else let me know the current issue. To me if you send English characters for category it should work.
2

Since you have a serviceName attribute in both AutoService and Service entities, ServiceRepository.findByServiceNameAndCategory is equivalent to the following SQL query:

SELECT
  *
FROM
  Service
WHERE
  serviceName = ?
AND category  = ?

As seen, this query does not hit the AutoService entity at all, which is why the results are not as expected.


The correct repository method is:

public interface ServiceRepository extends JpaRepository<Service, Long> {
  List<Service> findByCategoryAndAutoServiceServiceName(String category, String autoServiceName);
}

This method will search the nested AutoService object by its serviceName, as expected.

A sample project is available on Github to show this in action.

2 Comments

Thank you for good answer, I am using custom query right now, and I am getting empty list from data base. Can you check my edit please ?
@Andrew, you don't need the custom query, that is what my answer is about. You could simply have findByCategoryAndAutoServiceId or something like that. If that is not working for you, please post the actual code and some sample data that we can test with.

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.