首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从hibernate的外键表中获取主键表的结果

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

Stack Overflow用户
提问于 2012-12-25 13:10:09
回答 1查看 1.3K关注 0票数 0

我是第一次接触hibernate,并尝试使用条件。我坚持从2个表即主外键相关的表中获取结果。

我有拼车和API,现在基于用户搜索数据,我想填充拼车对象,其中包含SourceToDestinationDetails,但我没有得到它,也不知道如何使用标准SourceToDestinationDetails做它。

代码语言:javascript
运行
复制
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;
}

这是我写的,

代码语言:javascript
运行
复制
Criteria criteria1 = getSession().createCriteria(SourceToDestinationDetails.class);
criteria1.add(Restrictions.like("sourcePlace", "%" + from + "%"));
criteria1.add(Restrictions.like("destinationPlace", "%" + to + "%"));
List<SourceToDestinationDetails> listOfExactMatchCarpooler = criteria1.list();  

通过上面的标准接口,我只得到了SourceToDestinationDetails的DTO记录,但现在我也需要拼车记录,我不知道如何在SourceToDestinationDetails表中获得匹配Carpooler_id的拼车记录。

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

代码语言:javascript
运行
复制
String from = "Bellandur";
    String to = "Silk Board";

则结果应为List<Carpooler>对象,其中包含所有匹配的SourceToDestinationDetails列表。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-25 13:21:34

你可以通过Annotations做到这一点。您可以在SourceToDestinationDetails类中使用@OneToMany注释,如下所示:

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
  <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>

在这种情况下,您的模型类将是

代码语言:javascript
运行
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14027880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档