Attendance model:
@Entity
public class Attendance {
@Id
Long id;
@Index
Date date;
@Parent
@Index
@ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
Ref<Student> studentRef;
public Long getId() {
return id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getWebsafeKey() {
return Key.create(studentRef.getKey(), Attendance.class, id).getString();
}
}
Student model:
@Entity
public class Student {
@Id
Long id;
@Index
String name;
String student_id;
Long mobile_number;
String email;
@Index
@ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
List<Ref<Shift>> shiftRef = new ArrayList<Ref<Shift>>();
public Long getId() {
return id;
}
public String getStudent_id() {
return student_id;
}
public void setStudent_id(String student_id) {
this.student_id = student_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getMobile_number() {
return mobile_number;
}
public void setMobile_number(Long mobile_number) {
this.mobile_number = mobile_number;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getWebsafeKey() {
return Key.create(Student.class, id).getString();
}
}This how I am inserting an attendance entity into datastore:
Key<Student> studentKey = Key.create(student_web_safe_key);
Date date = new Date();
date.setTime(System.currentTimeMillis());
attendance.setDate(date);
attendance.studentRef = Ref.create(studentKey);
ofy().save().entity(attendance).now();Questions:But when I retrieve the same entity without specifying its parent then its gives me date as: "date ": "2016-06-15T12:31:18.845Z". please explain this?
- The above query store the date in format: 2016-06-15 (18:01:18.845) IST
2. I am able to store attendance corresponds to each student and also able to retrieve all the attendance of a student.but how can retrieve attendance of student on specified date or range of dates?I tried blow query:
Query<Attendance> query = ofy().load().type(Attendance.class).ancestor(studentKey).filter("date =",date);
but it gives me following exception:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 503 Service Unavailable
{
"code": 503,
"errors": [
{
"domain": "global",
"message": "com.google.appengine.api.datastore.DatastoreNeedIndexException: no matching index found. recommended index is:\n- kind: Attendance\n ancestor: yes\n properties:\n - name: date\n\nThe suggested index for this query is:\n <datastore-index kind=\"Attendance\" ancestor=\"true\" source=\"manual\">\n <property name=\"date\" direction=\"asc\"/>\n </datastore-index>\n\n",
"reason": "backendError"
}
],
"message": "com.google.appengine.api.datastore.DatastoreNeedIndexException: no matching index found. recommended index is:\n- kind: Attendance\n ancestor: yes\n properties:\n - name: date\n\nThe suggested index for this query is:\n <datastore-index kind=\"Attendance\" ancestor=\"true\" source=\"manual\">\n <property name=\"date\" direction=\"asc\"/>\n </datastore-index>\n\n"
}
.filter("date >=",dateAtMidnight).filter("date <", nextDateAtMidnight)
You will also require an index for this query, you can copy the specified index definition into your datastore-indexes.xml, but you'll need to understand this sooner or later, so read up.
In many circumstances it makes sense to denormalise data to make querying easier, so you may want to store a duplicate field which is a string representation of a date (excluding time and zone), and query for that instead (which allows you to get away with just @Index, and ignore timezones, times etc). This makes sense if this is a common query, or day based queries are very frequent, or typically the time/zone is irrelevant. Make sure to store the day in a format that lexicographically sorts, such as yyyy-MM-dd
--
You received this message because you are subscribed to the Google Groups "objectify-appengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objectify-appen...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Query<Attendance> query = ofy().load().type(Attendance.class).ancestor(studentKey).filter("string_date =",date);
Query<Attendance> query = ofy().load().type(Attendance.class).ancestor(studentKey).filter("string_date >=",date);
gives me same exception as I post earlier.
To unsubscribe from this group and stop receiving emails from it, send an email to objectify-appengine+unsub...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to objectify-appen...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "objectify-appengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objectify-appen...@googlegroups.com.
Thanks jeff, can you please tell me steps to use jodatime in objectify?
Thanks jeff, can you please tell me steps to use jodatime in objectify?how do I insert a joda date and retrieve from datastore.
--