JPA + EclipseLink + 云平台 = 运行在云端的数据库应用

JPA(Java Persistence API)的实现 Provider 有 Hibernate,OpenJPA 和 EclipseLink 等等。
本文介绍如何通过 JPA + Eclipse 连接 SAP 云平台上的 HANA 数据库实例。
1. 登陆 SAP 云平台 Cockpit,创建一个新的 HANA DB 实例:

设置数据库 ID 和 System user 的密码。

创建成功后,HANA DB 实例状态为 CREATING:


稍等片刻后,状态变为 STARTED,可用。

2. 接下来就可以开发 Java 应用通过 JPA 操作这个云端的数据库实例了。

SAP 云平台 neo 环境的 SDK 下自带了一个 JPA 的 hello world 应用,位于文件夹 samples 下面,名称为 persistence-with-jpa:

直接 import 到 Eclipse 里,targeted Runtimes 选择为 Java Web Tomcat 8:

再将该应用从 Eclipse 部署到 SAP 云平台上:


部署成功后,在 SAP 云平台 Cockpit 里得到该应用的访问 url:

3. 将部署完成的 Java 应用与第一步创建的 HANA DB 实例进行绑定:


绑定成功,这样该 Java 应用就能使用云端的 HANA DB 实例了。

在 PC 浏览器里测试。这个 hello world 的 JPA 应用只设计了一张表,包含 First Name 和 Last Name 两列。



在手机端的访问:

SAP 云平台以微服务的方式提供了 Document 的 CRUD(增删改查)操作。该微服务基于标准的 CMIS 协议(Content Management Interoperability Service)。


同标准的 CMIS 相比,SAP 云平台的 Document Service 增添了一些功能的支持:

通过一个 Hello World 应用来了解如何在 Java 程序里消费 SAP 云平台的 Document Service。
通过这个链接下载例子程序。
点击该超链接下载 Java Web Tomcat 8 SDK。

例子程序位于该 SDK 的 samples 文件夹下。

将该应用部署到 SAP 云平台之后,访问该应用看到如下提示信息:

该提示信息是由应用的 DocumentStoreServlet 的 goGet 方法输出:

一旦点击 Connect 超链接之后:

调用另一个 Servlet DocumentStoreConnectServlet 的 doGet 方法,执行三个逻辑:

Step 1 的 Ecm 即 Enterprise Content Management,指 SAP 云平台上的 Document Service。
该 Document Service 的实例句柄通过 JNDI 获得:

在 web.xml 里进行配置:

第一次点击 Connect 时,会执行下图 catch 分支的代码,用硬编码的密匙创建一个新的 repository。第二次执行即进入第 85 行逻辑,因此此时 repository 已经存在。

第一次点击 Connect 的输出:

第二次的输出:

在 SAP 云平台的 Cockpit 里能够看到成功创建的 repository:

如果需要删除该 repository,需提供创建时硬编码的密匙 abcdef0123456789。

版权声明: 本文为 InfoQ 作者【Jerry Wang】的原创文章。
原文链接:【http://xie.infoq.cn/article/8eab7f698ca93680523a0d15f】。文章转载请联系作者。
评论