从 Boilerplates Java 更改表格 bluemix 上的 DB Web Starter 示例代码

Change tables from Boilerplates Java DB Web Starter sample code on bluemix

我目前正在研究 bluemix 上的 Boilerplates Java DB Web Starter 应用程序中的示例代码,我在更改 table 时遇到了一些麻烦。 我的数据库已准备就绪并在那里运行。我试图将代码更改为从待办事项 table 获取,而不是从我的国家 table 获取。这是我如何在代码上更改它的图片:

http://i.imgur.com/MglCM6T.png

package example.jpa;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "Country")
public class TODO {

    @Id //primary key
    @Column(name = "country_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    int id;

    @Basic
    @Column(name = "country_name")
    String name;

    public String getName() {
        System.out.println(name);
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        System.out.println(id);
        return id;
    }

    public void setId(int pk) {
        id = pk;
    }

    @Override
    public String toString() {
        return String.format("{\"id\": \"%d\", \"name\": \"%s\"}", id, name);
    }
}

http://i.imgur.com/tirtUaY.

package example.jpa;

import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.transaction.UserTransaction;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/Country")
public class TODOListResource {

    private UserTransaction utx;
    private EntityManager em;

    public TODOListResource() {
        utx = getUserTransaction();
        em = getEm();
    }

    @POST
    public Response create(@FormParam("name") String name) {
        TODO todo = new TODO();
        todo.setName(name);
        try {
            utx.begin();
            em.persist(todo);
            utx.commit();
            return Response.ok(todo.toString()).build();
        } catch (Exception e) {
            e.printStackTrace();            
            return Response.status(javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR).build();
        } finally {
            try {
                if (utx.getStatus() == javax.transaction.Status.STATUS_ACTIVE) {
                    utx.rollback();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @DELETE
    public Response delete(@QueryParam("country_id") int id) {
        try {
            utx.begin();
            TODO todo = em.find(TODO.class, id);
            if (todo != null) {
                em.remove(todo);
                utx.commit();
                return Response.ok().build();
            } else {
                return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Response.status(javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR).build();
        } finally {
            try {
                if (utx.getStatus() == javax.transaction.Status.STATUS_ACTIVE) {
                    utx.rollback();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @PUT
    public Response update(@FormParam("country_id") int id,
            @FormParam("name") String name) {
        try {
            utx.begin();
            TODO todo = em.find(TODO.class, id);
            if (todo != null) {
                todo.setName(name);// TODO check if null
                em.merge(todo);
                utx.commit();
                return Response.ok().build();
            } else {
                return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Response.status(javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR).build();
        } finally {
            try {
                if (utx.getStatus() == javax.transaction.Status.STATUS_ACTIVE) {
                    utx.rollback();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response get(@QueryParam("country_id") int id) {
        if (id == 0) {
            List<TODO> list = em.createQuery("SELECT country_id, country_name FROM country", TODO.class).getResultList();
            if (list.size() == 0) {
                list = em.createQuery("SELECT country_id, country_name FROM country", TODO.class).getResultList();
            }
            //TODO use JSON util like Gson to render objects and use REST Response Writer
            String json = "{\"id\":\"all\", \"body\":" + list.toString() + "}";
            return Response.ok(json).build();
        }
        TODO todo = null;
        try {
            utx.begin();
            todo = em.find(TODO.class, id);
            utx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            return Response.status(javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR).build();
        } finally {
            try {
                if (utx.getStatus() == javax.transaction.Status.STATUS_ACTIVE) {
                    utx.rollback();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (todo != null)
            return Response.ok(todo.toString()).build();
        else
            return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND).build();
    }

    /*private void createSampleData() {
        create("sample entry #1");
        create("sample entry #2");
        create("sample entry #3");
    }*/

    private UserTransaction getUserTransaction() {
        InitialContext ic;
        try {
            ic = new InitialContext();
            return (UserTransaction) ic.lookup("java:comp/UserTransaction");
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return null;
    }


    private EntityManager getEm() {
        InitialContext ic;
        try {
            ic = new InitialContext();
            return (EntityManager) ic.lookup("java:comp/env/openjpa-todo/entitymanager");
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return null;
    }

}

当我的数据库中不存在它时,我也找不到它在哪里创建 table TODO。

谢谢

Table 创建在样板文件的 persistence.xml 文件中配置。 JPA 可以为您创建它,因此该应用程序并未明确执行此操作。根据您使用的 Liberty JPA 功能(因为它们使用不同的 JPA 实现),需要以下属性之一:

<!-- allow table definitions/creation on-the-fly jpa-2.0 feature -->
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
<!-- allow table definitions/creation on-the-fly jpa-2.1 feature -->
<property name="eclipselink.ddl-generation" value="create-tables"/>