有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpringBootJPA表层次结构

我在数据库中只有一个名为Student的表。以下是我的代码中针对它的jpa实体:

@Entity
@Table(name = "student")
public class Student{

@Id
private Long id;

@Column
private String firstName;

@Column
@Embedded
@JsonUnwrapped
private School school;
}

public class School{

private Integer grade;//eg. 2nd, 3rd

private String type; //eg. primary, secondary
}

到目前为止,代码只用于获取所有学生及其数据或获取特定学生。因此,DB模式就是这样。但是现在,我们有了一个新的功能,我们需要根据特定的年级进行搜索,并获取该特定年级的所有学生。或者为特定学校类型招收所有学生,例如小学的所有学生。因此,需求完全相反,因此我们需要将以下模式的返回发送到前端:

class SchoolResponseDTO{

private String schoolType;

private List<Integer> grades;
}

class Grade{

private Integer id;

private List<Integer> studentId;
}

更详细地说,从现在开始,我们需要找到所有的学校类型,然后,该学校类型中的所有年级,然后,该学校类型和年级中的所有学生

到目前为止,我们一直在使用Spring JpaRepository来满足我们的需求。我觉得这个新需求需要定制查询,我不认为使用JPARepository可以直接处理这个问题。我只是想知道你是怎么想的。在没有自定义sql查询的情况下可以做到这一点吗


共 (1) 个答案

  1. # 1 楼答案

    您可以使用Spring数据并使用类似以下查询属性表达式的内容。从文档:

    Property expressions can refer only to a direct property of the managed entity, as shown in the preceding example. At query creation time, you already make sure that the parsed property is a property of the managed domain class. However, you can also define constraints by traversing nested properties. Consider the following method signature:

    List<Person> findByAddressZipCode(ZipCode zipCode);
    

    Assume a Person has an Address with a ZipCode. In that case, the method creates the property traversal x.address.zipCode. The resolution algorithm starts by interpreting the entire part (AddressZipCode) as the property and checks the domain class for a property with that name (uncapitalized). If the algorithm succeeds, it uses that property. If not, the algorithm splits up the source at the camel case parts from the right side into a head and a tail and tries to find the corresponding property — in our example, AddressZip and Code. If the algorithm finds a property with that head, it takes the tail and continues building the tree down from there, splitting the tail up in the way just described. If the first split does not match, the algorithm moves the split point to the left (Address, ZipCode) and continues.

    Although this should work for most cases, it is possible for the algorithm to select the wrong property. Suppose the Person class has an addressZip property as well. The algorithm would match in the first split round already, choose the wrong property, and fail (as the type of addressZip probably has no code property).

    To resolve this ambiguity you can use _ inside your method name to manually define traversal points. So our method name would be as follows:

    List<Person> findByAddress_ZipCode(ZipCode zipCode);
    

    Because we treat the underscore character as a reserved character, we strongly advise following standard Java naming conventions (that is, not using underscores in property names but using camel case instead).

    检查链接 https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions

    希望能有帮助