【问题标题】:get results of primary key table from foreign key table in hibernate从hibernate的外键表中获取主键表的结果
【发布时间】:2012-12-25 05:10:08
【问题描述】:

我不熟悉休眠并尝试使用标准。 我坚持从 2 个表中获取结果,即主外键存在的表。

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

这是我写的,

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记录,但现在我也需要拼车记录,我不知道如何在SourceToDestinationDetails表中获取匹配Carpooler_id的拼车记录。

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

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

那么结果应该是List&lt;Carpooler&gt;对象,里面包含了所有匹配的SourceToDestinationDetails列表。

【问题讨论】:

    标签: java hibernate hibernate-mapping hibernate-criteria


    【解决方案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>();
    }
    

    比起丑陋的 XML,我通常更喜欢注解

    【讨论】:

    • 我已经在使用 hbm.xml 文件进行映射,但我的问题是我是否需要添加私有 Set carpoolers;在 SourceToDestinationDetails 中,因为我的拼车者已经拥有 List 属性?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多