[appengine-java] Re: Not able to save child object in one-to-many relationship
Hi Ravi, sorry to not have an "easy" workaround for you but the following rules have proven to be working quite well for us when using GWT + GAEJ + JDO: 1. No Class hierarchies for JDO Objects going through GWT RPC 2. No JDO modeled 1:n relationships other List (same table) 3. Implicit 1:n relationships setting parent key in child objects using queries on keys for lookup In your case this means use a field like this in your Appointment object: @Persistent @Extension(vendorName="datanucleus", key="gae.parent-pk", value="true") private String parentKey; Set this field with the employees key, then you can change both entities in the same transaction (see entity groups). Use a key lookup with a filter on the parent key field if you need a quick count on how many appointments an employee has: http://code.google.com/intl/de-DE/appengine/docs/java/datastore/queriesandindexes.html#Queries_on_Keys or a regular query with the same filter to retrieve the actual appointments. Hope this helps, Fred On 27 Aug., 23:09, hampole wrote: > Thanks for the info. I looked at the issue and many other people > having problem of saving child object in a one-to-many relation. I am > new to this and I don't understand completely. Is there any work- > around to save a child object. If anyone can show me an example, I > appreciate. This is really frustrating as this is basic functionality > in any relational data. In my case, Employee object already exists and > I want to create a new appointment and associate with the employee > object. If there is a different way of doing this, please help. This > has taken quite a lot of time and if this is not available in GWT, I > don't think I can use Google apps for the development of our > application. I appreciate any help. > > Thanks, > Ravi > > On Aug 27, 1:51 am, Frederik Pfisterer wrote: > > > Your problem seems to be related to this open gwt > > issue:http://code.google.com/p/google-web-toolkit/issues/detail?id=4976 > > > Please star the issue to raise its attention. The workaround is not > > using 1:n relationships with GWT + GAE + JDO. > > > Cheers, > > Fred > > > On 26 Aug., 23:12, hampole wrote: > > > > I am trying to develop GAE application. I am using the latest GWT > > > SDK(2.0.4) AND App Engine SDK(1.3.6) on Eclipse3.4. I am having > > > problem insavinga childobject"Appointment" and the parentobject > > > is "Employee". Each Employee has many appointments and and each > > > appointment has one employee associated with. I have pasted the Client > > > code and the server code here. Please help and see what I am missing > > > here. I tried many different ways without success. When I retrieve the > > > appointments, the employee is always null. I appreciate any help or > > > some example that I can try. Thanks. > > > > //Client code > > > public class AppointmentCreateWidget extends Composite { > > > > private static AppointmentCreateWidgetUiBinder uiBinder = GWT > > > .create(AppointmentCreateWidgetUiBinder.class); > > > > interface AppointmentCreateWidgetUiBinder extends > > > UiBinder { > > > } > > > > @UiField Button btnSaveAppointment; > > > @UiField TextBox tbxSubject; > > > @UiField ListBox lbxRoom; > > > @UiField ListBox lbxHost; > > > @UiField DateBox dbAppointStartDate; > > > @UiField DateBox dbAppointEndDate; > > > @UiField ListBox lbxStartTimeHr; > > > @UiField ListBox lbxEndTimeHr; > > > @UiField ListBox lbxStartTimeMi; > > > @UiField ListBox lbxEndTimeMi; > > > > private final AppointmentServiceAsync appointService = > > > (AppointmentServiceAsync) GWT.create(AppointmentService.class); > > > private final List visitors = new ArrayList(); > > > > public AppointmentCreateWidget() { > > > initWidget(uiBinder.createAndBindUi(this)) > > > } > > > > @UiHandler("btnSaveAppointment") > > > void onClick(ClickEvent e) { > > > addAppointment(); > > > } > > > > private void addAppointment() { > > > String subject = tbxSubject.getText().toUpperCase().trim(); > > > Date startdate = dbAppointStartDate.getValue(); > > > Date enddate = dbAppointEndDate.getValue(); > > > String starttime = > > > lbxStartTimeHr.getValue(lbxStartTimeHr.getSelectedIndex()).toString() > > > + > > > > > > lbxStartTimeMi.getValue(lbxStartTimeMi.getSelectedIndex()); > > > String endtime = > > > lbxEndTimeHr.getValue(lbxEndTimeHr.getSelectedIndex()).toString() + > > > lbxEndTimeMi.getValue(lbxEndTimeMi.getSelectedIndex()); > > > String room = > > > lbxRoom.getValue(lbxRoom.getSelectedIndex()).toString(); > > > String key = lbxHost.getValue(lbxHost.getSelectedIndex()); > > > DateTimeFormat dateFormat = > > > DateTimeFormat.getShortDateFormat(
[appengine-java] Re: Not able to save child object in one-to-many relationship
Thanks for the info. I looked at the issue and many other people having problem of saving child object in a one-to-many relation. I am new to this and I don't understand completely. Is there any work- around to save a child object. If anyone can show me an example, I appreciate. This is really frustrating as this is basic functionality in any relational data. In my case, Employee object already exists and I want to create a new appointment and associate with the employee object. If there is a different way of doing this, please help. This has taken quite a lot of time and if this is not available in GWT, I don't think I can use Google apps for the development of our application. I appreciate any help. Thanks, Ravi On Aug 27, 1:51 am, Frederik Pfisterer wrote: > Your problem seems to be related to this open gwt > issue:http://code.google.com/p/google-web-toolkit/issues/detail?id=4976 > > Please star the issue to raise its attention. The workaround is not > using 1:n relationships with GWT + GAE + JDO. > > Cheers, > Fred > > On 26 Aug., 23:12, hampole wrote: > > > I am trying to develop GAE application. I am using the latest GWT > > SDK(2.0.4) AND App Engine SDK(1.3.6) on Eclipse3.4. I am having > > problem insavinga childobject"Appointment" and the parentobject > > is "Employee". Each Employee has many appointments and and each > > appointment has one employee associated with. I have pasted the Client > > code and the server code here. Please help and see what I am missing > > here. I tried many different ways without success. When I retrieve the > > appointments, the employee is always null. I appreciate any help or > > some example that I can try. Thanks. > > > //Client code > > public class AppointmentCreateWidget extends Composite { > > > private static AppointmentCreateWidgetUiBinder uiBinder = GWT > > .create(AppointmentCreateWidgetUiBinder.class); > > > interface AppointmentCreateWidgetUiBinder extends > > UiBinder { > > } > > > @UiField Button btnSaveAppointment; > > @UiField TextBox tbxSubject; > > @UiField ListBox lbxRoom; > > @UiField ListBox lbxHost; > > @UiField DateBox dbAppointStartDate; > > @UiField DateBox dbAppointEndDate; > > @UiField ListBox lbxStartTimeHr; > > @UiField ListBox lbxEndTimeHr; > > @UiField ListBox lbxStartTimeMi; > > @UiField ListBox lbxEndTimeMi; > > > private final AppointmentServiceAsync appointService = > > (AppointmentServiceAsync) GWT.create(AppointmentService.class); > > private final List visitors = new ArrayList(); > > > public AppointmentCreateWidget() { > > initWidget(uiBinder.createAndBindUi(this)) > > } > > > @UiHandler("btnSaveAppointment") > > void onClick(ClickEvent e) { > > addAppointment(); > > } > > > private void addAppointment() { > > String subject = tbxSubject.getText().toUpperCase().trim(); > > Date startdate = dbAppointStartDate.getValue(); > > Date enddate = dbAppointEndDate.getValue(); > > String starttime = > > lbxStartTimeHr.getValue(lbxStartTimeHr.getSelectedIndex()).toString() > > + > > lbxStartTimeMi.getValue(lbxStartTimeMi.getSelectedIndex()); > > String endtime = > > lbxEndTimeHr.getValue(lbxEndTimeHr.getSelectedIndex()).toString() + > > lbxEndTimeMi.getValue(lbxEndTimeMi.getSelectedIndex()); > > String room = > > lbxRoom.getValue(lbxRoom.getSelectedIndex()).toString(); > > String key = lbxHost.getValue(lbxHost.getSelectedIndex()); > > DateTimeFormat dateFormat = DateTimeFormat.getShortDateFormat(); > > > try { > > createAppointment(subject, startdate, enddate, > > starttime, > > endtime, key, visitors); > > } > > catch(Exception ex) { > > Window.alert("Save Appointment failed: " + ex.toString()); > > } > > } > > > private void createAppointment(String subject, Date startdate, Date > > enddate, String starttime, String endtime, String empID, List > > visitor) { > > appointService.addAppointment(subject,startdate, enddate, > > starttime, endtime, empID, visitor, new AsyncCallback() { > > public void onFailure(Throwable error) { > > Window.alert("Unable to create Appointment." + > > error.getMessage()); > > } > > public void onSuccess(Void ignore) { > > } > > }); > > } > > > } > > > //ServiceImpl > > public class AppointmentServiceImpl extends RemoteServiceServlet > > implements AppointmentService { > > /** > > * > > */ > > private static final long serialVersionUID = 1L; > > > public void addAppointment(String subject, Date appointStartDate, > >
[appengine-java] Re: Not able to save child object in one-to-many relationship
Your problem seems to be related to this open gwt issue: http://code.google.com/p/google-web-toolkit/issues/detail?id=4976 Please star the issue to raise its attention. The workaround is not using 1:n relationships with GWT + GAE + JDO. Cheers, Fred On 26 Aug., 23:12, hampole wrote: > I am trying to develop GAE application. I am using the latest GWT > SDK(2.0.4) AND App Engine SDK(1.3.6) on Eclipse3.4. I am having > problem in saving a child object "Appointment" and the parent object > is "Employee". Each Employee has many appointments and and each > appointment has one employee associated with. I have pasted the Client > code and the server code here. Please help and see what I am missing > here. I tried many different ways without success. When I retrieve the > appointments, the employee is always null. I appreciate any help or > some example that I can try. Thanks. > > //Client code > public class AppointmentCreateWidget extends Composite { > > private static AppointmentCreateWidgetUiBinder uiBinder = GWT > .create(AppointmentCreateWidgetUiBinder.class); > > interface AppointmentCreateWidgetUiBinder extends > UiBinder { > } > > @UiField Button btnSaveAppointment; > @UiField TextBox tbxSubject; > @UiField ListBox lbxRoom; > @UiField ListBox lbxHost; > @UiField DateBox dbAppointStartDate; > @UiField DateBox dbAppointEndDate; > @UiField ListBox lbxStartTimeHr; > @UiField ListBox lbxEndTimeHr; > @UiField ListBox lbxStartTimeMi; > @UiField ListBox lbxEndTimeMi; > > private final AppointmentServiceAsync appointService = > (AppointmentServiceAsync) GWT.create(AppointmentService.class); > private final List visitors = new ArrayList(); > > public AppointmentCreateWidget() { > initWidget(uiBinder.createAndBindUi(this)) > } > > @UiHandler("btnSaveAppointment") > void onClick(ClickEvent e) { > addAppointment(); > } > > private void addAppointment() { > String subject = tbxSubject.getText().toUpperCase().trim(); > Date startdate = dbAppointStartDate.getValue(); > Date enddate = dbAppointEndDate.getValue(); > String starttime = > lbxStartTimeHr.getValue(lbxStartTimeHr.getSelectedIndex()).toString() > + > lbxStartTimeMi.getValue(lbxStartTimeMi.getSelectedIndex()); > String endtime = > lbxEndTimeHr.getValue(lbxEndTimeHr.getSelectedIndex()).toString() + > lbxEndTimeMi.getValue(lbxEndTimeMi.getSelectedIndex()); > String room = > lbxRoom.getValue(lbxRoom.getSelectedIndex()).toString(); > String key = lbxHost.getValue(lbxHost.getSelectedIndex()); > DateTimeFormat dateFormat = DateTimeFormat.getShortDateFormat(); > > try { > createAppointment(subject, startdate, enddate, starttime, > endtime, key, visitors); > } > catch(Exception ex) { > Window.alert("Save Appointment failed: " + ex.toString()); > } > } > > private void createAppointment(String subject, Date startdate, Date > enddate, String starttime, String endtime, String empID, List > visitor) { > appointService.addAppointment(subject,startdate, enddate, > starttime, endtime, empID, visitor, new AsyncCallback() { > public void onFailure(Throwable error) { > Window.alert("Unable to create Appointment." + > error.getMessage()); > } > public void onSuccess(Void ignore) { > } > }); > } > > } > > //ServiceImpl > public class AppointmentServiceImpl extends RemoteServiceServlet > implements AppointmentService { > /** > * > */ > private static final long serialVersionUID = 1L; > > public void addAppointment(String subject, Date appointStartDate, > Date appointEndDate, String startTime, String endTime, String empID, > List visitors) throws NotLoggedInException { > PersistenceManager pm = getPersistenceManager(); > //get employee > Employee emp = pm.getObjectById(Employee.class, empID); > > Appointment newAppoint = new Appointment(subject, > appointStartDate, appointEndDate, startTime, endTime); > newAppoint.setVisitors(visitors); > newAppoint.setEmployee(emp); > emp.getAppointments().add(newAppoint); > > try { > pm.makePersistent(emp); > } finally { > pm.close(); > } > } > > private PersistenceManager getPersistenceManager() { > return PMF.get().getPersistenceManager(); > } > > } > > //Employee Bean > @PersistenceCapable(identityType = IdentityType.APPLICATION, > detachable = "true") > public class Employee implements IsSerializable, IValidatable { > > @PrimaryKey > @Persistent(valueStrategy = IdGenerator