Change tables from Boilerplates Java DB Web Starter sample code on bluemix -
i'm studying sample code boilerplates java db web starter app on bluemix , i'm having troubles changing table. db ready , functioning there. tried change code instead of getting todolist table, country table. here picture of how changed on code:
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); } } 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(); } { 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(); } { 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(); } { 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 country", todo.class).getresultlist(); if (list.size() == 0) { list = em.createquery("select country_id, country_name country", todo.class).getresultlist(); } //todo use json util gson render objects , 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(); } { 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; } } i couldn't find it's creating table todo when doesn't exist in db.
thank you
table creation configured in boilerplate's persistence.xml file. jpa can create you, app isn't explicitly doing it. depending on liberty jpa feature use (because use different jpa implementations) 1 of these properties needed:
<!-- 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"/>
Comments
Post a Comment