Java Spring数据-JPA多嵌套对象
我的班级结构如下:Java Spring数据-JPA多嵌套对象,java,spring,spring-boot,spring-data-jpa,spring-data,Java,Spring,Spring Boot,Spring Data Jpa,Spring Data,我的班级结构如下: 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
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
}
现在我有了一个如下的Respository接口
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);
}
请帮助我如何构造这种场景的方法字段Z在X中不存在,它在Y类中。因此,应该使用对象的完整路径作为Z的id
注:请考虑使用可理解的班级名称。我猜它们只是一个例子。字段Z在X中不存在,它在Y类中。因此,应该使用对象的完整路径作为Z的id
注:请考虑使用可理解的班级名称。我想它们只是个例子。您可以使用import org.springframework.data.mongodb.repository.Query;注释
@Query("{$and : [{'y.id': ?0}, {'y.z.id': ?1 }]}")
public X findByIdAndYIdAndZId(Long yId, Long zId);
您将对有更多了解,您可以使用import org.springframework.data.mongodb.repository.Query;注释
@Query("{$and : [{'y.id': ?0}, {'y.z.id': ?1 }]}")
public X findByIdAndYIdAndZId(Long yId, Long zId);
你们会对有更多的了解,我知道我可以用Query,但我想用method。我知道我可以用Query,但我想用method。
@Query("{$and : [{'y.id': ?0}, {'y.z.id': ?1 }]}")
public X findByIdAndYIdAndZId(Long yId, Long zId);