Home > Software engineering >  How to use value in embedded object in JPA derived query
How to use value in embedded object in JPA derived query

Time:11-23

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.

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.

CodePudding user response:

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

List<ParentObject> findByCOChildObjectId(List<Long> ids);
  • Related