通过休眠模式按ManyToOne关系进行过滤

时间:2019-05-08 09:19:38

标签: hibernate jpa spring-data-jpa many-to-one

我正在尝试为具有休眠状态的ManyToOne实境创建过滤器。但我不知道。
我可以使用@Filter注释过滤OneToMany关系。但是我不知道如何过滤另一个方向。

TestRun类将成为我的查询的条目,而我要执行的操作是使用特定的customerbranch获得最新的运行结果。尽管我不知道如何仅将最新运行的内容排除在这个问题之外。

除了过滤customerbranch之外,我还希望仅获得那些与特定componentName相关联的结果。问题是componentName不在结果本身中,而是在相关的testCase中。

简而言之:

我想为客户获得一个TestRun,并为TestResults分支,并匹配关联的TestCase实体的条件。

我尝试过的事情:

我试图在@Filter的{​​{1}}列中添加一个componentName注释,但这没有用。 我还尝试为存储库类编写一个自定义查询。但是我在弄乱似乎对结果没有任何影响的联接。我没有为我的TestCase对象过滤掉TestResults的列表,而是因为联接而得到了一长串TestRun的列表。

我不知道该如何处理。我考虑过创建两个查询,而不是单个查询。但是imo不会那么干净。我相信这不是一个非常特殊的情况,这意味着对于这种查询可能有一个简单的解决方案。

我的实体

TestRun

TestRuns

TestResult

@Entity
@Table(name = "test_run")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "testrun")
@FilterDef(
    name = "byCustomerAndBranch",
    parameters = {
        @ParamDef(
            name = "customer",
            type = "string"
        ),
        @ParamDef(
            name = "branch",
            type = "string"
        )
    }
)
@Filter(
    name = "byCustomerAndBranch",
    condition = "customer = :customer and branch = :branch"
)
public class TestRun implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @NotNull
    @Column(name = "customer", nullable = false)
    private String customer;

    @NotNull
    @Column(name = "branch", nullable = false)
    private String branch;

    @OneToMany(mappedBy = "testRun")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<TestResult> testResults = new HashSet<>();
}

TestCase

@Entity
@Table(name = "test_result")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class TestResult implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @ManyToOne(optional = false)
    @NotNull
    @JsonIgnoreProperties("testResults")
    private TestRun testRun;

    @ManyToOne(optional = false)
    @NotNull
    @JsonIgnoreProperties("testResults")
    private TestCase testCase;
}

0 个答案:

没有答案
相关问题