Reputation: 859
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
Upvotes: 2
Views: 1863
Reputation: 7937
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
Upvotes: 0
Reputation: 18430
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.
Upvotes: 4