Jayesh
Jayesh

Reputation: 6111

get results of primary key table from foreign key table in hibernate

I am new to hibernate and trying to use Criteria. I stuck in getting the results from 2 table ie table where primary-foreign key are in realtion.

I have Carpooler and SourceToDestinationDetails DTO, Now based on user search data I want to fill Carpooler Object, which contain SourceToDestinationDetails, but I am not getting it and don't know how to do it using 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 This is what I wrote,

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

through above Criteria API, I am only getting SourceToDestinationDetails DTO record, but now I need Carpooler record as well, I dont know how to get Carpooler record of matching Carpooler_id in SourceToDestinationDetails table.

I mean if user gives,

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

then result should be List<Carpooler> object, which contain all matching SourceToDestinationDetails list inside.

Upvotes: 0

Views: 1369

Answers (1)

Jayamohan
Jayamohan

Reputation: 12924

You can do that by Annotations. You can use @OneToMany annotation in your SourceToDestinationDetails class as below,

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

If you want to acheive the same thing using HIBERNATE XML. Declare the XML as below

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

In that case your model class will be

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

I usually prefer Annotations than ugly XML's

Upvotes: 1

Related Questions