1

The Class and Hbm and DAO Impl files are given below. when calling the address.getDistance() methos i am getting null value

How to get the calculated distance from sql to hibernate class

Address.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.lb.bb.model.Address" table="address" catalog="bb">
        <id name="addressId" type="java.lang.Integer">
            <column name="address_id" />
            <generator class="identity" />
        </id>
        <property name="addressLine" type="string" column="address_line" length="200" not-null="true"/>
        <property name="city" type="string" column="city" length="200" not-null="true"/>
        <property name="cityUrl" type="string" column="city_url" length="200" not-null="true"/>
        <property name="state" type="string" column="state" length="200" not-null="true"/>
        <property name="country" type="string" column="country" length="200" not-null="true"/>
        <property name="zipcode" type="string" column="zipcode" length="100" not-null="true"/>
        <property name="phoneno" type="string" column="phoneno" length="100" not-null="true"/>
        <property name="latitude" type="double" column="latitude" not-null="true"/>
        <property name="longitude" type="double" column="longitude" not-null="true" />
    </class>
</hibernate-mapping>

Address Class

public class Address implements java.io.Serializable{

private static final long serialVersionUID = -2587677535225820165L;

    private Integer addressId;
    private String addressLine;
    private String city;
    private String cityUrl;
    private String state;
    private String country;
    private String zipcode;
    private String phoneno;
    private Double latitude;
    private Double longitude;
    private Double distance;

    public Address(){

    }
}

AddressDAOIMpl method to calculate the distance

@SuppressWarnings("unchecked")
public List<Address> listAddressByLatitudeAndLongitude(Double latitude, Double longitude) {

    Query query = getSession().createSQLQuery("SELECT *, ((ACOS(SIN(:latitude * PI() / 180) * SIN(latitude * PI() / 180) + COS(:latitude * PI() / 180)" +
            " * COS(latitude * PI() / 180) * COS((:longitude - longitude) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance FROM address a " +
            "HAVING distance<=1000 ORDER BY distance ASC").addEntity(Address.class)
            .setParameter("latitude", latitude).setParameter("longitude", longitude);
    return (List<Address>) query.list();
}
  • I don't quite understand the question. The `distance` field of your POJO isn't mapped to Hibernate in any way (neither as a field nor as a [derived property](http://stackoverflow.com/questions/15813118/hibernate-derived-property-with-xml-mapping). What are you trying to do? – Boris the Spider Mar 19 '14 at 11:52

0 Answers0