cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to persist relationship between two entities

RuedigerMueller
Advisor
Advisor
0 Kudos

I am building a small app using JPA, Olingo and SAPUI5 on the HANA Cloud Platform to familiarize myself with the HANA Cloud Platform. The app has two entities: a "lesson" and a "vocable". A lesson can have multiple vocables. The problem that I face is that I am somehow not able to persist the relationship between the two.

Using my coding I am able to create vocables and I am able to create lessons. I even get a success message when I first create a lesson and then create a vocable by posting to /vocab-web/vocab.svc/Lessons(1L)/VocableDetails. Both the lesson as well as the vocable are persisted at the database but the relationship between the two is lost. I tried various things all without success.

I am desperately looking for any hints and ideas on how to fix this.

I attached the two JPA classes (renamed to .txt) and the generated $metadata file.

This is the code that I use to create the vocable that is linked to a lesson that I previously created:

addNewVocable : function(sLearned, sKnown, oTable) {

  var vocables = {};

  var resource = window.location.protocol + "//"

    + window.location.hostname + (window.location.port ? ":" + window.location.port : "")

    + "/vocab-web/vocab.svc/Lessons(""1L" + ")";

  vocables.Learned = sLearned;

  vocables.Known = sKnown;

  vocables.Level = 1;

  vocables.DueDate = new Date().toISOString().replace("Z", "0000");

  this.getView().getModel().create("/Lessons(1L)/VocableDetails", vocables, null,

  this.successMsg, this.errorMsg);

  },

If you want to have a look at the complete source code you can have a look at RuedigerMueller/vocab · GitHub

Any help is highly appreciated.

Thanks, Rüdiger

Accepted Solutions (0)

Answers (1)

Answers (1)

RuedigerMueller
Advisor
Advisor
0 Kudos

I was able to find a work-around (or is it the solution?). I first create the vocable and then - with an AJAX call - establish the link between the vocable and the lesson (see hard coded solution below).

One thing I don't understand: I have to use PUT for the AJAX call. If I use POST if get a 405 Method not allowed. According to the OData documentation POST creates the link and PUT updates the link. If POST does not work and only PUT ist working: does that mean the link does somehow get created but just not the correct one? On the DB the link column is empty... Questions over questions.

addNewVocable : function(sLearned, sKnown, oTable) {

  var vocables = {};

  vocables.Learned = sLearned;

  vocables.Known = sKnown;

  vocables.Level = 1;

  vocables.DueDate = new Date().toISOString().replace("Z", "0000");

  this.getView().getModel().create("/Vocables", vocables, null,

  this.successMsg, this.errorMsg);

//Establish link with the lesson

  jQuery.ajax({

       url : 'http://localhost:8080/vocab-web/vocab.svc/Vocables(2L)/$links/LessonDetails',

       type : 'PUT',

       contentType : 'application/xml',

       data : '<uri xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices">http://localhost:8080/vocab-web/vocab.svc/Lessons(1L)</uri>'

  });

},