在Bluemix上更改Boilerplates Java DB Web Starter示例代码中的表

时间:2015-09-30 20:20:29

标签: java ibm-cloud websphere-liberty javadb

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

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;
    }

}

当我的数据库中不存在TODO时,我也无法找到创建表TODO的位置。

谢谢

1 个答案:

答案 0 :(得分:3)

表创建在样板文件的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"/>