Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use value in embedded object in JPA derived query

I have an object that is maintained by hibernate and is queried on using JPA. The object I am working with has an embedded object that is nested within using the @Embedded annotation.

I need to be able to write a derived query so that I can select the objects by a value in that embedded object.

public abstract class ParentObject {

    @Id
    private Long id;

    @Embedded
    private ChildObject cO;

    // Is there an annotation that can go here to allow JPA to use this as a queryable value?
    public Long getChildObjectId() {
        return cO.getChildObjectId();
    }

}


public class ChildObject {

    @Id
    private Long childObjectId;

}


public interface ParentObjectRepository extends JpaRepository<ParentObject, Long> {

    // This doesn't work and throws an error saying it can't find a value by the name of child object id
    List<ParentObject> findByChildObjectId(List<Long> ids);

}

I have tried different annotations to try and allow JPA to use the method as a value in a derived query but couldn’t find anything that fitted the bill.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I feel as though there will be an annotation that can be used to allow JPA to know where to look for child object id.

>Solution :

You must add the attirbute name (c0) as well:

List<ParentObject> findByCOChildObjectId(List<Long> ids);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading