2

I have a class structure something like below:

public class X {

@Id
private Long id;

//One to one mapping
private Y y;

....Some more attibutes

}

public class Y {

@Id
private Long id;

//ManyToOne mapping
private Z z;

....Some more attibutes

}

public class Z {

@Id
private Long id;

....Some more attibutes

}

Now I have a Respository interface like below

public interface XRepository extends JPARepository<X, Long> {
    // This is not working,    
    public X findByIdAndYIdAndZId(Long xId, Long yId, Long zId);
    
    //This also doesn't work obviously for same reason as above one
    public X findYIdAndZId(Long yId, Long zId);

}

I am getting this exception

org.springframework.data.mapping.PropertyReferenceException: No property zId found for type X!

Please help me on how to construct the method for such scenario

2 Answers 2

4

The field Z does not exist in X, it is in Y class. So, you should use the full path of the object for Z's id.

public X findByIdAndYIdAndYZId(Long xId, Long yId, Long zId);

Note: Please consider to use the understandable name for the class. I guess they are for example only.

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

Comments

0

You can use import org.springframework.data.mongodb.repository.Query; annotation.

 @Query("{$and : [{'y.id': ?0}, {'y.z.id': ?1 }]}")
 public X findByIdAndYIdAndZId(Long yId, Long zId);

You will get more idea about Query

1 Comment

I know I could do it using Query but I wanted to use method.

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.