从hibernate中的外键表中获取主键表的结果

时间:2012-12-25 05:10:09

标签: java hibernate hql hibernate-mapping hibernate-criteria

我是hibernate的新手,并尝试使用Criteria。 我坚持从2表中得到结果,即主要外键正在实现的表。

我有Carpooler和SourceToDestinationDetails DTO,现在基于用户搜索数据,我想填充Carpooler对象,其中包含SourceToDestinationDetails,但我没有得到它,也不知道如何使用Criteria 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();  

通过上面的Criteria API,我只获得了SourceToDestinationDetails DTO记录,但是现在我也需要Carpooler记录,我不知道如何在SourceToDestinationDetails表中获得Carpooler匹配Carpooler_id的记录。

我的意思是如果用户给出,

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

然后结果应该是List<Carpooler>对象,其中包含所有匹配的SourceToDestinationDetails列表。

1 个答案:

答案 0 :(得分:1)

您可以Annotations执行此操作。您可以在 SourceToDestinationDetails 类中使用@OneToMany注释,如下所示,

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

如果你想使用 HIBERNATE XML 来实现同样的目标。声明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>();
}

我通常更喜欢Annotations而不是丑陋的XML

相关问题