简体   繁体   English

Spring Data JPA(休眠)与动态条件的一对多关系

[英]Spring Data JPA (Hibernate) One to Many Relationship with Dynamic Condition

I have a one to many relationship between two of my entites, Airport and AirportTranslation : 我的两个实体AirportAirportTranslation之间存在一对多的关系:

public class Airport {
    @Id
    @Column(name = "id")
    private Long id;

    @OneToMany(mappedBy="airport", fetch = FetchType.LAZY)
    private List<AirportTranslation> translations;
}

And: 和:

public class AirportTranslation implements Serializable {
    @Id
    @Column(name = "id", updatable = false)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "airport_id")
    private Airport airport;

    @Column(name = "lang")
    private String lang;

    @Column(name = "name")
    private String name;    
}

Now I want to get all translations of an airport based on the current language of the system, using the normal syntax: airport.getTranslations() . 现在,我想使用常规语法: airport.getTranslations()根据系统的当前语言获取机场的所有翻译。

Due to the fact that the current language is dynamic, I can not use hibernate @Where . 由于当前语言是动态的,因此我无法使用hibernate @Where

I think using Hibernate @Filter could be the best option, but I can't find any clean, working sample of that for spring boot applications. 我认为使用Hibernate @Filter可能是最好的选择,但是我找不到用于Spring Boot应用程序的任何干净有效的示例。

There are few options. 没有什么选择。

Java Filtering Java过滤

Easy one, but slow and something I would consider design smell,to filter all data in getter. 简单,但很慢,我会考虑设计气味,以过滤getter中的所有数据。

We'd much rather prefer to filter data in database for performance reasons. 出于性能方面的考虑,我们宁愿选择筛选数据库中的数据。

Query Filtering 查询过滤

Other option is to manually add filters to your repositories, yet you would have to ALWAYS remember to put filters in your queries, every time you add new one. 另一种选择是将过滤器手动添加到存储库,但是每次添加新过滤器时,您都必须始终记住将过滤器放入查询中。

Which creates some maintenance issues. 这会带来一些维护问题。

Hibernate Filtering 休眠过滤

To get Hibernate Filtering and Spring Data Jpa is a little bit tricky. 要获取Hibernate过滤和Spring Data Jpa,有些棘手。

Since Spring Repositories are the abstraction to not interact with EntityManagers/ Session objects, yet we have to set filtering on our session for query we want to make, which is pretty much the same as making manual filters. 由于Spring Repositories是不与EntityManagers / Session对象进行交互的抽象,因此我们必须在会话上设置过滤条件以进行查询,这与制作手动过滤器几乎相同。

For more information on this solution, please refer to LINK 有关此解决方案的更多信息,请参阅LINK。

Spring Specifications 弹簧规格

The cleanest and most fitting for filtering with some business/system logic, I'd consider using Spring Specifications. 我考虑使用Spring规范,这是最干净,最适合使用某些业务/系统逻辑进行过滤的方法。 There is a chapter in Spring documentation about that with some great examples, so I won't be copy-pasting that. Spring文档中有一章介绍了一些出色的示例,因此我不会将其粘贴粘贴。

Please refer to https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#specifications 请参考https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#specifications

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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