java - How to set true or false if there is no record in table using hibernate -
i using spring rest hibernate , fetching particular record database table passing id method. method working if there no record in table want false in variable , if record exist want true in variable in json object.
here entity class subscribe.java
@entity @table(name="subscribe") @jsonignoreproperties({"hibernatelazyinitializer", "handler"}) public class subscribe implements serializable { private static final long serialversionuid = 1l; @id @generatedvalue @column(name="id") private long id; @column(name="subscribed_id") private string subid; @column(name="subscriber_id") private long subrid; public long getsubrid() { return subrid; } public void setsubrid(long subrid) { this.subrid = subrid; } public string getsubid() { return subid; } public void setsubid(string subid) { this.subid = subid; } public long getid() { return id; } public void setid(long id) { this.id = id; } }
here dao class
@suppresswarnings({ "unchecked", "rawtypes" }) public list<subscribe> getsubscribebyid(long id) throws exception { session = sessionfactory.opensession(); criteria cr = session.createcriteria(subscribe.class); cr.add(restrictions.eq("subrid", id)); list results = cr.list(); tx = session.gettransaction(); session.begintransaction(); tx.commit(); return results; }
and here controller
@requestmapping(value = "/subscribe/{id}", method = requestmethod.get) public @responsebody list<subscribe> getsubscriber(@pathvariable("id") long id) { list<subscribe> sub = null; try { sub = profileservice.getsubscribebyid(id); } catch (exception e) { e.printstacktrace(); } return sub; }
please suggest me how can can this
given way code structured ( pass database object directly rest client ) there no clean way can this.
i think more restful approach return http code indicates requested record not found. http 404 appropriate code use.
so, in pseudo-code,
@requestmapping(value = "/subscribe/{id}", method = requestmethod.get) public @responsebody list<subscribe> getsubscriber(@pathvariable("id") long id) { list<subscribe> sub = null; try { sub = profileservice.getsubscribebyid(id); } catch (exception e) { e.printstacktrace(); } if ( sub.isempty() ){ return http 404 result code } else { return sub; } }
your client side code need altered respond http result code rather boolean value, otherwise remain unchanged.
Comments
Post a Comment