Skip to Content

Unable to persist relationship between two entities

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Posted on Jan 11, 2014 at 08:48 PM

    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>'

    });

    },

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.