Hibernate:@Filter对单向@ManyToOne到子实体不起作用

时间:2017-07-24 18:00:52

标签: java hibernate jpa

我试图使用Hibernate的@Filter注释来过滤"孩子"父母的实体"实体。但是,我只使用单向@ManyToOne映射(第二个实体中没有@OneToMany),并且我没有按主键(id列)映射,而是另一个" assetClass"列。

我总是得到一个例外,即发现了多个子实体:

org.hibernate.HibernateException: More than one row with the given identifier was found

实体

我的主要实体我通过我的DAO加载包含单向映射以及过滤器定义,以及映射过滤器:

    @Entity
    @Table(name = "ASSETS")
    @FilterDef(name="activeControlsOnly", 
    parameters=@ParamDef(name="currentDate", type="date"))
    public class Asset {

    @Id
    @Column(name = "ASSET_ID")
    private Long id;

    @Column(name = "ASSET_CLASS")
    private String assetClass;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "ASSET_CLASS", referencedColumnName = "ASSET_CLASS", insertable = false, updatable = false)
    @Filter(name="activeControlsOnly", condition="validFrom <= :currentDate and validTo > :currentDate")
    private Control control;

}

&#34;孩子&#34;作为映射目标的实体:

@Entity
@Table(name = "CONTROLS")
public class Control {

    @Id
    @Column(name = "CONTROL_ID")
    private Long id;

    @Column(name = "ASSET_CLASS")
    private String assetClass;

    @Column(name = "VALID_TO")
    private Date validTo;

    @Column(name = "VALID_FROM")
    private Date validFrom;
}

我的DAO:

public class AssetDao {

    @PersistenceContext
    private transient EntityManager entityManager;

    public List<AssetEntity> loadAssets(Date theDate, ...) {

        entityManager.unwrap(Session.class).enableFilter("activeControlsOnly").setParameter("currentDate", theDate);

        TypedQuery<Asset> query = entityManager.createQuery("from Asset where ...", Asset.class);

        return query.getResultList();
    }
}

我已经尝试过在Control实体上另外设置Filter和FilterDef(甚至代替),但没有运气。这甚至可能吗?我该怎么办?

感谢您的帮助!

0 个答案:

没有答案