0

I have written restriction as follows,

 DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
 Date frmDate= df.parse("2014-01-01");
 Date toDate=df.parse("2014-09-16");

 Criteria criteria = session.createCriteria(HistoryLatitudeBean.class);
          criteria.add(Restrictions.eq("vehicleno",12));
          criteria.add(Restrictions.ge("rdate", frmDate)); 
          criteria.add(Restrictions.lt("rdate", toDate));
          criteria.add(Restrictions.between("rdate", frmDate, toDate));

          List<HistoryLatitudeBean> groupList=criteria.list();// <---groupList contains same objects

          for(HistoryLatitudeBean hb : groupList){

              System.out.println(hb.getLat());
          }

My bean is like this,

@Entity
@Table(name="hlatlng")
public class HistoryLatitudeBean {

@Id
@Column(name="vehicleno")
private int vehicleno;
@Column(name="lat")
private String lat;
@Column(name="lng")
private String lng;
@Column(name="status")
private String status;
@Column(name="rdate")
private Date rdate;
@Column(name="rtime")
private Date rtime;

//getters and setters
}

I am trying for following query, select * from hlatlng where vehicleno=12 and rdate BETWEEN '2014-01-01' and '2014-06-05'

In DB it gives 11 rows which has diff values. When I execute it through hibernate criteria, it gives 11 objects of same value. I checked through debug in eclipse all objets in groupList same id, how can I resolve it. Please help me.

Raghu
  • 1,324
  • 7
  • 24
  • 47
  • why are you using gt, lt and between. For clear query you can use hql and pass the variable. http://stackoverflow.com/questions/12699064/how-to-compare-dates-using-between-clause-in-hibernate – Swaraj Sep 09 '14 at 10:29
  • I tried like that , that is also giving same output. – Raghu Sep 09 '14 at 10:37
  • look at my another question http://stackoverflow.com/questions/25738350/how-to-write-a-hql-query-for-between-clause-for-date-range/25739659#25739659 – Raghu Sep 09 '14 at 10:38
  • 3
    Your `vehicleno` is an `id`. How do you expect to get more than one result from your query querying by id? – Aleksandr M Sep 09 '14 at 10:47
  • oh yes..,actually in that table there is no primary key. if I remove `@Id` will it leads to any problem? – Raghu Sep 09 '14 at 10:49
  • @AleksandrM If I remove `@Id` annotation it gives me error as `org.hibernate.AnnotationException: No identifier specified for entity: com.abc.its.beans.HistoryLatitudeBean` How to resolve this – Raghu Sep 09 '14 at 10:52
  • you can't create any object in hibernate without Id, it will throw Identifier not found for particular class. – Swaraj Sep 09 '14 at 11:03
  • @Swaraj can I make it foreign key without primary key.? – Raghu Sep 09 '14 at 11:04
  • No bro, you can't only solution of your problem is making surrogate key as explained in http://stackoverflow.com/questions/767277/hibernate-and-no-pk – Swaraj Sep 09 '14 at 11:11

1 Answers1

2

The issue seems to be resolved with Aleksandr M comment.

You are missing a field annotated with @Id. Each @Entity needs an @Id - this is the primary key in the database.

Hibernate requires an identifier for each entity.

also, need to modify you query for omitting vehicleno since it is the primary key and will return the same result everytime.

@Id
@Column(name="vehicleno")
private int vehicleno;
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • yes I have these three lines in my bean, but in my table I dont have a primary key, so I tried without `@Id`. but it throws error. – Raghu Sep 09 '14 at 11:03
  • @Raghu see my answer, clearly explained there, either make new field as `Id` or if you are using existing `vehicleno` as `Id`, the you will get one record only. – Ankur Singhal Sep 09 '14 at 11:04
  • Yes .. , Thanks bro , I have created one more field for id . then it worked. – Raghu Sep 09 '14 at 11:13
  • Thank you @ankur-singhal. Your answer helped me too! – Khanh Tran Aug 31 '16 at 18:45