Hibernate @Filter未应用于关联

时间:2014-03-06 03:22:33

标签: java hibernate hibernate-mapping

我无法弄清楚为什么我的过滤器没有应用于关联。

我可以看到它正在注册

06 Mar 2014 02:55:33,039 [INFO]  (main) org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean: Building new Hibernate SessionFactory
06 Mar 2014 02:55:33,039 [DEBUG]  (main) org.hibernate.cfg.Configuration: Preparing to build session factory with filters : {addressType=org.hibernate.engine.FilterDefinition@1015ee3d}
06 Mar 2014 02:55:40,172 [DEBUG]  (main) org.hibernate.impl.SessionFactoryImpl: Session factory constructed with filter configurations : {addressType=org.hibernate.engine.FilterDefinition@1015ee3d}

以下是我的课程

@Entity
@Table(name = "person")
@FilterDef(name = "addressType", parameters = @ParamDef(name = "addressType", type = "string"))
public class Person {

    @Id
    @Column(name = "EMPLID", nullable = false)
    private String employeeId;

    @OneToMany
    @JoinColumn(name = "EMPLID")
    @Filter(name = "addressType", condition = ":addressType = ADDRESS_TYPE")
    private Set<PersonAddressHistory> personAddressHistories;

    public String getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(final String employeeId) {
        this.employeeId = employeeId;
    }


    public Set<PersonAddressHistory> getPersonAddressHistories() {
        return personAddressHistories;
    }

    public void setPersonAddressHistories(final Set<PersonAddressHistory> personAddressHistories) {
        this.personAddressHistories = personAddressHistories;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this).add("employeeId", getEmployeeId()).toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof Person)) {
            return false;
        }
        Person other = (Person) obj;

        return Objects.equal(getEmployeeId(), other.getEmployeeId());
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(getEmployeeId());
    }

}


@Entity
@Table(name = "address_history")
public class PersonAddressHistory implements Serializable {

    @Column(name = "EMPLID", nullable = false)
    private String employeeId;

    @Id
    @Column(name = "ADDRESS_TYPE", nullable = false)
    private String addressType;

    @Id
    @Column(name = "EFFDT", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar effectiveDate;

    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "addressLine1", column = @Column(name = "ADDRESS", nullable = false)),
            @AttributeOverride(name = "city", column = @Column(name = "CITY", nullable = false)),
            @AttributeOverride(name = "state", column = @Column(name = "STATE", nullable = false)),
            @AttributeOverride(name = "country", column = @Column(name = "COUNTRY", nullable = false)),
            @AttributeOverride(name = "postalCode", column = @Column(name = "POSTAL", nullable = false)) })
    private Address address;

    public String getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(final String employeeId) {
        this.employeeId = employeeId;
    }

    public String getAddressType() {
        return addressType;
    }

    public void setAddressType(final String addressType) {
        this.addressType = addressType;
    }

    public Calendar getEffectiveDate() {
        return effectiveDate;
    }

    public void setEffectiveDate(final Calendar effectiveDate) {
        this.effectiveDate = effectiveDate;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(final Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this).add("employeeId", getEmployeeId()).add("addressType", getAddressType())
                .add("address", getAddress())
                .toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof PersonAddressHistory)) {
            return false;
        }
        PersonAddressHistory other = (PersonAddressHistory) obj;

        return Objects.equal(getEmployeeId(), other.getEmployeeId())
                && Objects.equal(getAddressType(), other.getAddressType())
                && Objects.equal(getEffectiveDate(), other.getEffectiveDate());
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(getEmployeeId(), getAddressType(), getEffectiveDate());
    }

}

And in my DAO

public class PersonDao extends AbstractBaseDao {

    public Person getPersonById(final String id) {
        getCurrentSession().enableFilter("addressType").setParameter("addressType", "HOME");
        return (Person) getCurrentSession().get(Person.class, id);
    }

}

我正在使用旧架构。 另外,如果我要对java.util.Calendar实例进行过滤,我在hibernate @FilterDef注释中指定什么作为参数类型 - “java.util.Calendar”或只是“日历”。

非常感谢。

1 个答案:

答案 0 :(得分:0)

尝试更改FilterDef的注释,将其移动到PersonAddressHistory类:

@Entity 
@Table(name = "person") 
@FilterDef(name = "addressType", parameters = @ParamDef(name = "addressType", type = "java.lang.String")) 
public class PersonAddressHistory implements Serializable {