-1

I want to create a simple project management application in Java at the SAP HANA Cloud Platform. Users can add persons to projects and define the working hours of a person who works on a project.

I created successfully a small application logic. However it's not clear to me how I should define the persistent data objects. And how to store and read the data in the right way.

I created the classes "Project" and "Person" to add data (see below).

How can I create a relationship between them and store the hours of work a person spent on a project?

On an ERM, I know the solution: A 1:n relation between Person and Project and an extra table between them with "projectID","personID" and "workingHours".

So far the app writes data with the function "addNewPerson":

addNewPerson : function( sFirstName, sLastName, oTable ) {  
          var _this = this;  
          _this.odataServiceUrl = personsListOdataServiceUrl;  
          jQuery.ajax({  
             url : _this.odataServiceUrl + "/Person?$format=json",  
             type : 'POST',  
             contentType : 'application/json',  
             data : JSON.stringify({  
                                     firstName : sFirstName,  
                                     lastName : sLastName  
                                  }),  
             success : function(data) {  
               _this.getView().getModel().refresh();  
               oTable.unbindRows().bindRows("/Person");  
      },  
             error : function(jqXHR, textStatus, errorThrown) {  
           sap.ui.commons.MessageBox.alert("Failed to add person: " + textStatus+ "\n" + errorThrown);  
             }  
      }); },  

To read the data, I simply bind the rows to the object.

So, how can I create the data objects the right way? And how can I read and write the data to the objects?

Person.java:

package com.sap.netweaver.cloud.sample;
import javax.persistence.*;
@Entity
@Table(name = "T_PERSON")
@NamedQuery(name = "AllPersons", query = "select p from Person p")

public class Person {
    @Id
    @GeneratedValue
    private long id;
    @Basic
    private String firstName;
    @Basic
    private String lastName;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public void setFirstName(String param) {
        this.firstName = param;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setLastName(String param) {
        this.lastName = param;
    }

    public String getLastName() {
        return lastName;
    }
}

Project.java:

package com.sap.netweaver.cloud.sample;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name = "T_PROJECT")
@NamedQuery(name = "AllProjects", query = "select p from Project p")
public class Project {

    @Id
    @GeneratedValue
    private long id;
    @Basic
    private String projectName;
    @Basic
    private String projectDesc;
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public void setProjectName(String param) {
        this.projectName = param;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectDesc(String param) {
        this.projectDesc = param;
    }

    public String getProjectDesc() {
        return projectDesc;
    }
}
1

1 Answer 1

0

First to your question regarding the connection between the person and the project. Since one project can have multiple persons I guess, you need a

@OneToMany(mappedBy = "projectEntity", cascade = CascadeType.PERSIST)
private List<Persons> persons= new ArrayList<Persons>();

on the project side and on the person side

@JoinColumn(name = "projectEntity")// JPA creates join column for you with the project id - see wiki oneToMany JPA example
private Project project;

For testing I strongly recomment the following steps:

  1. Start from the JPA example in the SAP HCP sdk: neo-java-web-sdk-2.36.4\samples\persistence-with-jpa
  2. Once you understood the example (create,persit,find), you add the 1:N relation by adding a second entity with the coding described above. 3).
  3. If this is working I would recommend going from the default memory database set in your local Tomcat or Java EE server to a real local DB. Apache Derby worked fine for me there. You find various youtube videos where the installation of derby is explained.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.