简体   繁体   中英

Spring Data- JPA multiple nested Object

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM