java - Hibernate entity virtual column error? -


i have defined virtual column distanceinkm in entity distance calculation , giving response user along distanceinkm virtual column not column in table,and it's working (case 1).

now using same entity fetching values table getting com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception: unknown column 'restaurant0_.distanceinkm' in 'field list'.(case 2)

i came know usage of @transiant annotation virtual columns used calculation.but if using virtual column not serialized/added user response in (case 1).i need implement both api ie , case 1 & case 2 using 1 entity

 @entity     @table(name = "restaurants")     public class restaurants implements serializable {          private static final long serialversionuid = 1l;          @id         @generatedvalue(strategy = generationtype.identity)         @column(name = "restaurant_id")         private int restaurantid;          @column(name = "restaurant_name")         private string restaurantname;          @column(name = "category_id")         private integer categoryid;          @column(name = "image_url")         private string imageurl;          private float longitude;          private float latitude;          @column(name = "contact_name")         private string contactname;          @column(name = "primary_phone")         private string primaryphone;          @column(name = "secondary_phone")         private string secondaryphone;          private string fax;          private string address1;          private string address2;          /*  virtual column   */         @column(insertable = false, updatable = false)         private string distanceinkm;  } 

help appreciated.

my query distance comparison,

select restaurant_id,restaurant_name, category_id,image_url,longitude,latitude, contact_name,primary_phone,secondary_phone, fax,address1,address2, ((acos(sin(:lat * pi() / 180) * sin(latitude * pi() / 180) + cos(:lat * pi() / 180) * cos(latitude * pi() / 180) * cos((:lon-longitude) * pi() / 180)) * 180 / pi()) * 60 * 1.1515)  `distanceinkm`  `restaurants` having  `distanceinkm`<=1  order `distanceinkm` asc 

my method

public list<restaurants> getrestaurantsbydistance(filterrestaurantrequest filterrestaurantrequest) throws sqlexception, classnotfoundexception, ioexception {         session session = sessionfactory.getcurrentsession();         float latitude = float.parsefloat(filterrestaurantrequest.getlatitude());         float longitude = float.parsefloat(filterrestaurantrequest.getlongitude());         string sql = "select restaurant_id,restaurant_name,category_id,image_url,longitude,latitude,contact_name,primary_phone,secondary_phone,fax,address1,address2,((acos(sin(:lat * pi() / 180) * sin(latitude * pi() / 180) + cos(:lat * pi() / 180) * cos(latitude * pi() / 180) * cos((:lon-longitude) * pi() / 180)) * 180 / pi()) * 60 * 1.1515) `distanceinkm` `restaurants` having `distanceinkm`<=1 order `distanceinkm` asc";         query query = session.createsqlquery(sql).addentity(restaurants.class);         query.setparameter("lat", latitude);         query.setparameter("lon", longitude);         list<restaurants> restaurantses = query.list();         return restaurantses;     } 

i think if going make calculations, can express virtual column sql formula or column value in database. if understood @formula annotation want. here tutorial http://docs.jboss.org/hibernate/core/3.6/reference/en-us/html_single/#d0e6481 , example http://gokhan.ozar.net/hibernate-derived-properties-formula-annotation/


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -