本文介绍了JPA一对多过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在筑巢几个实体。但是在检索时我们只想获得那些活跃的实体。

  @Entity 
公共类系统{
@Id
@Column(name =ID)
private Integer id;

@OneToMany(mappedBy =system)
private Set< Systemproperty> systempropertys;
}

@Entity
公共类Systemproperty {
@Id
@Column(name =ID)
private Integer id;

@Id
@Column(name =ACTIVE)
private整数活跃;
}

请求 Systemproperties 我只想获得有效的属性 active = 1 )。



hibernate注释 以及使用。然而,两者都不适合我。即使我们正在使用hibernate,我也在考虑用Eclipselink替换它,因为我们目前必须使用预先加载,我们可能会遇到性能问题。子查询实际上效果不好,因为我们正在嵌套几个级别。



Eclipselink似乎有一个注释可以工作,但它似乎遵循不同的概念,然后hibernate @FilterDef 注释并在切换时会产生额外的开销。



@JoinColumn 不会似乎允许进一步过滤。是否有标准的JPA方法来解决这个问题?

解决方案

AFAIK没有基于JPA的便携方式来做到这一点。一个干净但有点低效的解决方案是在Java端执行所有操作并创建一个getter getActiveSystemproperties(),它手动迭代映射的 systempropertys 并返回一组不可变的活动属性。


We are nesting several entities. However upon retrieving we only want to get those entities which are active.

@Entity
public class System {
  @Id
  @Column(name = "ID")
  private Integer id;

  @OneToMany(mappedBy = "system")
  private Set<Systemproperty> systempropertys;
}

@Entity
public class Systemproperty {
  @Id
  @Column(name = "ID")
  private Integer id;

  @Id
  @Column(name = "ACTIVE")
  private Integer active;
}

When requesting the Systemproperties I only want to get the properties that are active (active = 1).

Searching around I found some hibernate annotations and the possibility to use subqueries. However both don't really work for me. Even though we are currently using hibernate I'm considering to replace it with Eclipselink, because we currently have to use eager loading and we are likely to run into performance problems with that. The subqueries don't really work well, because we are nesting several levels.

Eclipselink seems to have a @Customizer annotation that could work, however it seems to follow a different concept then the hibernate @FilterDef annotation and would cause additional overhead when switching.

The @JoinColumn doesn't seem to allow further filtering. Is there a standard JPA way to solve this problem?

解决方案

AFAIK there is no portable JPA-based way to do this. A clean, however a little bit inefficient, solution would be to do everything on Java-side and create a getter getActiveSystemproperties() that manually iterates over mapped systempropertys and returns an immutable set of active properties.

这篇关于JPA一对多过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 13:34