【问题标题】:CRUD operation on OData service produced by Apache Olingo with MySQL, JPA and Tomcat web server使用 MySQL、JPA 和 Tomcat Web 服务器对 Apache Olingo 生成的 OData 服务进行 CRUD 操作
【发布时间】:2017-01-04 06:46:15
【问题描述】:

我关注了一些example,其中我们可以使用带有 MySQL、JPA 和 Tomcat Web 服务器的 Apache Olingo 生成 OData 服务。本示例完全基于 MySQL 数据库中的数据显示。

如何根据上述链接中的示例执行创建、更新和删除等操作

Service.java

import org.apache.olingo.odata2.jpa.processor.ref.factory.JPAEntityManagerFactory;
import org.apache.olingo.odata2.processor.api.jpa.ODataJPAContext;
import org.apache.olingo.odata2.processor.api.jpa.ODataJPAServiceFactory;
import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException;

public class TileCollectionListServiceFactory extends ODataJPAServiceFactory {

    private static final String PERSISTENCE_UNIT_NAME = "ABC";

    @Override
    public ODataJPAContext initializeODataJPAContext() throws ODataJPARuntimeException {
        ODataJPAContext oDatJPAContext = this.getODataJPAContext();
        try {           
            oDatJPAContext.setEntityManagerFactory(JPAEntityManagerFactory.getEntityManagerFactory(PERSISTENCE_UNIT_NAME));
//          oDataJPAContext.setPersistenceUnitName(PUNIT_NAME);

//          EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
//          oDatJPAContext.setEntityManagerFactory(emf);
            oDatJPAContext.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);

            return oDatJPAContext;          
        } catch (Exception e) {         
            throw new RuntimeException(e);          
        }       
    }   
}

Model.java

import java.io.Serializable;
import javax.persistence.*;

@Entity
@NamedQuery(name="Tilecollection.findAll", query="SELECT t FROM Tilecollection t")
public class Tilecollection implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int idtilecollection;

    private String icon;

    private String isAdmin;

    private String title;

    private String type;

    public Tilecollection() {
    }

    public int getIdtilecollection() {
        return this.idtilecollection;
    }

    public void setIdtilecollection(int idtilecollection) {
        this.idtilecollection = idtilecollection;
    }

    public String getIcon() {
        return this.icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getIsAdmin() {
        return this.isAdmin;
    }

    public void setIsAdmin(String isAdmin) {
        this.isAdmin = isAdmin;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getType() {
        return this.type;
    }

    public void setType(String type) {
        this.type = type;
    }

}

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="ABC" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.wuerth.model.Tilecollection</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3307/XXXXXXXXXXXXXXXX"/>
            <property name="javax.persistence.jdbc.user" value="XXXXX"/>
            <property name="javax.persistence.jdbc.password" value="XXXXX"/>
        </properties>
    </persistence-unit>
</persistence>

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ABC</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>ODataServlet</servlet-name>
    <servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
    <init-param>
      <param-name>javax.ws.rs.Application</param-name>
      <param-value>org.apache.olingo.odata2.core.rest.app.ODataApplication</param-value>
    </init-param>
    <init-param>
      <param-name>org.apache.olingo.odata2.service.factory</param-name>
      <param-value>com.wuerth.main.TileCollectionListServiceFactory</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>ODataServlet</servlet-name>
    <url-pattern>/odata/v1/ABC_SRV/*</url-pattern>
  </servlet-mapping>

  <!-- CORS To allow access OData service by JavaScript in other domain  -->
  <filter>
      <filter-name>CorsFilter</filter-name>
      <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>CorsFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Index.html

<!DOCTYPE html>
 <html>
    <head>
        <meta charset="utf8">
        <title>OData</title>
    </head>## Heading ##
    <body>
        <h1>OData</h1><br><br>
        <a href="/ABC/odata/v1/ABC_SRV/">Service Document</a>
    </body>
</html>

请用一些好的例子和概念指导我。

提前谢谢你。

【问题讨论】:

    标签: mysql tomcat jpa odata olingo


    【解决方案1】:

    你可以参考SCN blog关于mysql和Olingo Odata2的例子。 我发现这个博客对开发我的应用程序很有帮助。

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 2013-08-28
      • 2016-03-18
      • 2015-06-06
      • 2017-04-19
      • 1970-01-01
      • 2010-11-10
      • 2012-02-28
      • 2011-08-26
      相关资源
      最近更新 更多