如何在SAP HANA Cloud中配置和使用持久性数据对象?

时间:2022-03-23 16:33:44

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.

我想在SAP HANA云平台上用Java创建一个简单的项目管理应用程序。用户可以将人员添加到项目中并定义从事项目工作的人员的工作时间。

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).

我创建了“Project”和“Person”类来添加数据(见下文)。

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".

在ERM上,我知道解决方案:Person和Project之间的1:n关系以及它们之间带有“projectID”,“personID”和“workingHours”的额外表。

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

到目前为止,应用程序使用“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;
}

}

2 个解决方案

#1


0  

For me it seems that you want to expose your database as a OData service. If this is the case I can point you to the java library at "http://olingo.incubator.apache.org/".

对我来说,似乎您希望将数据库公开为OData服务。如果是这种情况,我可以指向“http://olingo.incubator.apache.org/”的java库。

In the documentation (http://olingo.incubator.apache.org/documentation.html) there is a section about JPA.

在文档(http://olingo.incubator.apache.org/documentation.html)中有一节关于JPA。

Perhaps you will find a solution there.

也许你会在那里找到解决方案。

Kind regards

#2


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. 从SAP HCP sdk中的JPA示例开始:neo-java-web-sdk-2.36.4 \ samples \ persistence-with-jpa

  3. 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).
  4. 一旦理解了示例(create,persit,find),就可以通过添加带有上述编码的第二个实体来添加1:N关系。 3)。

  5. 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.
  6. 如果这样做,我建议从本地Tomcat或Java EE服务器中的默认内存数据库集到真正的本地数据库。 Apache Derby在那里对我很好。您可以找到各种YouTube视频,其中介绍了德比的安装。

#1


0  

For me it seems that you want to expose your database as a OData service. If this is the case I can point you to the java library at "http://olingo.incubator.apache.org/".

对我来说,似乎您希望将数据库公开为OData服务。如果是这种情况,我可以指向“http://olingo.incubator.apache.org/”的java库。

In the documentation (http://olingo.incubator.apache.org/documentation.html) there is a section about JPA.

在文档(http://olingo.incubator.apache.org/documentation.html)中有一节关于JPA。

Perhaps you will find a solution there.

也许你会在那里找到解决方案。

Kind regards

#2


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. 从SAP HCP sdk中的JPA示例开始:neo-java-web-sdk-2.36.4 \ samples \ persistence-with-jpa

  3. 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).
  4. 一旦理解了示例(create,persit,find),就可以通过添加带有上述编码的第二个实体来添加1:N关系。 3)。

  5. 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.
  6. 如果这样做,我建议从本地Tomcat或Java EE服务器中的默认内存数据库集到真正的本地数据库。 Apache Derby在那里对我很好。您可以找到各种YouTube视频,其中介绍了德比的安装。