2013-12-15 91 views
0

我一直堅持使用@OneToMany映射獲取結果。這裏是模型類。如何在休眠中添加限制條件到OneToMany映射

Assessment.java

@Entity 
    @FilterDef(name = "tenantFilter", parameters = @ParamDef(name = "tenantIdParam", type = "string")) 
    @Filters(@Filter(name = "tenantFilter", condition = "tenant_id = :tenantIdParam")) 
    @Table(name = "assessment") 
    public class Assessment extends Revenue implements Comparable<Assessment> { 

    //other attriutes 

    @ManyToOne(fetch = FetchType.LAZY) 
     @Cascade(value = CascadeType.ALL) 
     @JoinColumn(name = "property_id") 
     private Property propertyAssessment; 
    @Column(name = "tenant_id", nullable = false) 
     private String tenantId; 

//getters and setters 
    } 

Property.java

@Entity 
@FilterDef(name = "tenantFilter", parameters = @ParamDef(name = "tenantIdParam", type = "string")) 
@Filters(@Filter(name = "tenantFilter", condition = "tenant_id = :tenantIdParam")) 
@Table(name = "property") 
public class Property implements java.io.Serializable { 

//other attributes 

@ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "mas_gnd_id") 
    private GramaNiladhariDivision gramaNiladhariDivision; 
    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "mas_so_id", nullable = true) 
    private SubOffice subOffice; 
    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "mas_ward_id", nullable = true) 
    private Ward ward; 
    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "mas_road_id", nullable = true) 
    private Road road; 
    @Column(name = "side_of_property") 
    private Character sideOfProperty; 
    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "mas_pt_id", nullable = true) 
    private PropertyType propertyType; 
    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "property_description") 
    private PropertyDescription propertyDescription; 
@OneToMany(fetch = FetchType.LAZY, mappedBy = "propertyAssessment") 
    @Cascade(value = CascadeType.ALL) 
    private List<Assessment> assessments = new ArrayList<Assessment>(0); 
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "property") 
    @Cascade(value = CascadeType.ALL) 
    @LazyCollection(LazyCollectionOption.FALSE) 
    private List<PropertyOwner> propertyOwners = new ArrayList<PropertyOwner>(0); 

//getters and setters 
} 

PropertyOwner.java

@Entity 
@FilterDef(name = "tenantFilter", parameters = @ParamDef(name = "tenantIdParam", type = "string")) 
@Filters(@Filter(name = "tenantFilter", condition = "tenant_id = :tenantIdParam")) 
@Table(name = "property_owner") 
public class PropertyOwner implements java.io.Serializable { 

//other attributes 

@OneToOne(fetch = FetchType.EAGER) 
    @Cascade(value = CascadeType.ALL) 
    @JoinColumn(name = "person_id") 
    private Person person; 
    @Column(name = "is_main") 
    private Boolean main; 
    @ManyToOne(fetch = FetchType.LAZY) 
    @Cascade(value = CascadeType.ALL) 
    @JoinColumn(name = "property_id") 
    private Property property = new Property(); 
    @Column(name = "tenant_id", nullable = false, updatable = false) 
    private String tenantId; 
    @Column(name = "status", columnDefinition = "varchar(255) default 'active'") 
    private String status; 

//getters and setters 

} 

所以我的問題是我想去的地方的地位是無效的加載PropertyOwners。我試圖用不同的方式編寫Criteria,但我無法得到確切的結果。即使我將限制添加到狀態,它始終會將所有PropertyOwner返回給我,無論其狀態如何。這是我嘗試的標準之一。

public Assessment getAssessmentById(Long assessmentId, Object tenantId) throws HibernateException { 
     Session session = getSession(tenantId); 
     Assessment result; 
     result = (Assessment) session 
       .createCriteria(Assessment.class) 
       .setFetchMode("propertyAssessment", FetchMode.JOIN) 
       .createAlias("propertyAssessment.propertyOwners", "propertyOwners", JoinType.INNER_JOIN, 
         Restrictions.ne("propertyOwners.status", ScandiumKeyBox.INACTIVE)) 
       .setFetchMode("propertyAssessment.subOffice", FetchMode.JOIN) 
       .setFetchMode("propertyAssessment.ward", FetchMode.JOIN) 
       .setFetchMode("propertyAssessment.gramaNiladhariDivision", FetchMode.JOIN) 
       .setFetchMode("propertyAssessment.propertyDescription", FetchMode.JOIN) 
       .setFetchMode("propertyAssessment.propertyType", FetchMode.JOIN) 
       .setFetchMode("propertyAssessment.road", FetchMode.JOIN).setFetchMode("registerNumber", FetchMode.JOIN) 
       .setFetchMode("registerPageNumbers", FetchMode.JOIN).setFetchMode("elgActivity", FetchMode.JOIN) 
       .add(Restrictions.eq("id", assessmentId)).uniqueResult(); 
     return result; 
    } 

請爲我提供一個適當的解決方案來解決這個問題。謝謝。

回答

1

最後,我找到了一種方法,可以從PropertyOwners的狀態中獲得確切的結果。棘手的部分是當檢索結果hibernate加載所有匹配的實體時,由於JoinType.INNER_JOIN而不考慮createAlias中的Restrictions。所以我把JoinType.LEFT_OUTER_JOIN設置爲createAlias並刪除createAlias中的Restrictions。我按照慣例設置了Restrictions。現在它給出了與狀態匹配的確切結果。

這裏是修改的方法。

public Assessment getAssessmentById(Long assessmentId, Object tenantId) throws HibernateException { 
     Session session = getSession(tenantId); 
     Assessment result; 
     result = (Assessment) session 
      .createCriteria(Assessment.class) 
      .setFetchMode("propertyAssessment", FetchMode.JOIN) 
      .createAlias("propertyAssessment.propertyOwners", "propertyOwners", JoinType.LEFT_OUTER_JOIN) 
      .setFetchMode("propertyAssessment.subOffice", FetchMode.JOIN) 
      .setFetchMode("propertyAssessment.ward", FetchMode.JOIN) 
      .setFetchMode("propertyAssessment.gramaNiladhariDivision", FetchMode.JOIN) 
      .setFetchMode("propertyAssessment.propertyDescription", FetchMode.JOIN) 
      .setFetchMode("propertyAssessment.propertyType", FetchMode.JOIN) 
      .setFetchMode("propertyAssessment.road", FetchMode.JOIN).setFetchMode("registerNumber", FetchMode.JOIN) 
      .setFetchMode("registerPageNumbers", FetchMode.JOIN).setFetchMode("elgActivity", FetchMode.JOIN) 
      .add(Restrictions.eq("id", assessmentId)) 
      .add(Restrictions.ne("propertyOwners.status", ScandiumKeyBox.INACTIVE)) 
      .uniqueResult(); 
     return result; 
    } 

謝謝。

+0

我有同樣的問題,你的解決方案修復它。我不清楚爲什麼hibernate會以這種方式工作,爲什麼INNERJOIN(我認爲它是默認值)不起作用。但感謝你的帖子,我有它的工作。 –