2012-12-25 56 views
0

我是hibernate的新手,試圖使用Criteria。 我堅持從2表中得到結果,即主外鍵在實際中的表。在休眠狀態下從外鍵表中獲取主鍵表的結果

我有Carpooler和SourceToDestinationDetails DTO,現在根據我要填寫Carpooler對象,其中包含SourceToDestinationDetails用戶的搜索數據,但我沒有得到它,不知道如何使用標準的API來做到這一點。

public class Carpooler implements Serializable{ 

    private long carpoolerId; 
    private String drivingLicenceNumber=null; 
    private String userType=null;![enter image description here][1] 
    private User user=null; 
    private List<VehicleDetails> listOfVehicleDetails=null; 
    private List<SourceToDestinationDetails> listOfSourceToDestinationDetails=null; 
    private Date carpoolerCreationDate; 
} 

public class SourceToDestinationDetails implements Serializable{ 

    private static final long serialVersionUID = -7158985673279885525L; 

    private long sourceToDestinationId; 
    private String sourcePlace=null; 
    private String destinationPlace=null; 
    private String inBetweenPlaces=null; 

    private String sourceLeavingTime=null; 
} 

enter image description here enter image description here 這是我寫的,

Criteria criteria1 = getSession().createCriteria(SourceToDestinationDetails.class); 
criteria1.add(Restrictions.like("sourcePlace", "%" + from + "%")); 
criteria1.add(Restrictions.like("destinationPlace", "%" + to + "%")); 
List<SourceToDestinationDetails> listOfExactMatchCarpooler = criteria1.list(); 

通過上述標準的API,我只得到SourceToDestinationDetails DTO紀錄,但現在我需要Carpooler記錄,以及,我不知道如何在SourceToDestinationDetails表中獲取匹配Carpooler_id的Carpooler記錄。

我的意思是,如果用戶給,

String from = "Bellandur"; 
    String to = "Silk Board"; 

那麼結果應該是List<Carpooler>對象,它裏面含有所有匹配SourceToDestinationDetails名單。

回答

1

您可以通過Annotations來做到這一點。如果你想使用acheive HIBERNATE XML同樣的事情,你可以使用@OneToMany標註在SourceToDestinationDetails類,如下,

public class SourceToDestinationDetails implements Serializable{ 

    private static final long serialVersionUID = -7158985673279885525L; 
    @Column 
    private long sourceToDestinationId; 
    @Column 
    private String sourcePlace=null; 
    @Column 
    private String destinationPlace=null; 
    @Column 
    private String inBetweenPlaces=null; 
    @Column 
    private String sourceLeavingTime=null; 

    @OneToMany(mappedBy = "carpooler_id", cascade = CascadeType.ALL) 
    private Set<Carpooler> carpoolers; 
} 

。聲明XML如下

<set name="carpoolers" table="source_destination" 
      inverse="true" lazy="true" fetch="select"> 
     <key> 
      <column name="carpooler_id" not-null="true" /> 
     </key> 
     <one-to-many class="com.test.Carpooler" /> 
    </set> 

在這種情況下,你的模型類將是

public class SourceToDestinationDetails implements Serializable{ 

    private static final long serialVersionUID = -7158985673279885525L; 

    private long sourceToDestinationId; 
    private String sourcePlace=null; 
    private String destinationPlace=null; 
    private String inBetweenPlaces=null; 

    private String sourceLeavingTime=null; 
    private Set<StockDailyRecord> carpoolers = 
       new HashSet<StockDailyRecord>(); 
} 

我通常喜歡批註比醜陋的XML的

+0

我已經使用映射的hbm.xml文件,但我問題是我需要添加私人設置 carpoolers;在SourceToDestinationDetails中,因爲我的Carpooler已經具有List 屬性? – Jayesh

+0

查看更新的回覆.... – Jayamohan