2014-03-07 56 views
0

嗨我有一個場景,我需要從數據庫加載記錄時加載第三級域對象。 這裏是我的塞納里奧的例子: 有三個域類用戶,物業,請休眠EAGER提取

@Entity 
@Table(name="user") 
public class USER{ 
@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "id") 
    private Long id; 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name="PROP_ID", insertable=false, updatable=false) 
    private Property property; 
    ............ 
    other fields and getter and setter methdos 
    ............. 
    } 

    @Entity 
    @Table(name="properties") 
    public class Property{ 
     @Id 
     @GeneratedValue(strategy=GenerationType.AUTO) 
     private Long id; 

     @OneToOne(mappedBy="property", fetch=FetchType.Lazy) 
     @JoinColumn(name="CONTACT_ID") 
     private Contact contact; 
     ............ 
     other fields and getter and setter methdos 
     ............. 
    } 

@Entity 
@Table(name = "contact") 
public class Contact{ 
@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "id") 
private Long id; 
@Column(name = "ADDRESS1",length = 255) 
private String address1; 
@Column(name = "ADDRESS2",length = 255) 
private String address2; 
    ............ 
     other fields and getter and setter methdos 
     ............. 
    } 

有我的領域類,我想獲得聯繫方式,當我獲取用戶記錄。 我能夠通過使用JOIN獲取模式獲取屬性記錄,但我沒有得到如何獲得第三級記錄。

這裏是我的查詢以供參考:

 DetachedCriteria criteria = DetachedCriteria.forClass(USER.class); 
     criteria.setFetchMode("property", FetchMode.JOIN); 
     List<USER> users = getHibernateTemplate().findByCriteria(criteria); 

在這裏,我能得到的財產對象我這樣做聯接查詢。 但我希望Contact對象和Property對象一起。

請不要將我的EAFER提取定義用於完整的域本身。 不知何故,我必須在標準本身。

,這意味着我不能定義屬性表是這樣的:

@Entity 
    @Table(name="properties") 
    public class Property{ 
     @Id 
     @GeneratedValue(strategy=GenerationType.AUTO) 
     private Long id; 

     @OneToOne(mappedBy="property", fetch=FetchType.EAGER) 
     @JoinColumn(name="CONTACT_ID") 
     private Contact contact; 
     ............ 
     other fields and getter and setter methdos 
     ............. 
    } 

回答

0

您是否嘗試過增加另一fetchmode爲property.contact

DetachedCriteria criteria = DetachedCriteria.forClass(USER.class); 
criteria.setFetchMode("property", FetchMode.JOIN) 
criteria.setFetchMode("property.contact", FetchMode.JOIN); 
List<USER> users = getHibernateTemplate().findByCriteria(criteria);