Hibernate: @Filter on unidirectional @ManyToOne to child entity not working

Viewed 404

I'm trying to use Hibernate's @Filter annotation to filter the "child" entities of a "parent" entity. However I'm only using a unidirectional @ManyToOne mapping (with no @OneToMany in the second entity) and I'm not mapping by the primary key (id column), but another "assetClass" column.

I'm always getting the exception that more than one child entities were found:

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

Entities

My main entity I'm loading via my DAO that contains the unidirectional mapping and also the filter definition, as well as the filter on the mapping:

    @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;

}

The "child" entity that's the target of the mapping:

@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;
}

And my 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();
    }
}

I've already experimented with setting the Filter and FilterDef additionally (and even instead of) at the Control entity, but no luck. Is this even possible? What can I do?

Thank you for your help!

0 Answers
Related