From 26b01b500065634eb3133dc354a0ba71b13bff56 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 7 Aug 2013 23:53:53 +0200 Subject: wip o Start of JPA implementation. --- .../io/trygvis/container/myapp/AddressBook.java | 236 -------------------- .../trygvis/container/myapp/AddressBookDirect.java | 237 ++++++++++++++++++++ .../io/trygvis/container/myapp/AddressBookJpa.java | 242 +++++++++++++++++++++ .../container/myapp/DriverManagerDataSource.java | 60 +++++ .../io/trygvis/container/myapp/package-info.java | 2 +- 5 files changed, 540 insertions(+), 237 deletions(-) delete mode 100644 myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java create mode 100644 myapp/src/main/java/io/trygvis/container/myapp/AddressBookDirect.java create mode 100644 myapp/src/main/java/io/trygvis/container/myapp/AddressBookJpa.java create mode 100644 myapp/src/main/java/io/trygvis/container/myapp/DriverManagerDataSource.java (limited to 'myapp/src/main') diff --git a/myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java b/myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java deleted file mode 100644 index d5c93fe..0000000 --- a/myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java +++ /dev/null @@ -1,236 +0,0 @@ -package io.trygvis.container.myapp; - -import javax.persistence.TypedQuery; -import java.io.BufferedReader; -import java.io.EOFException; -import java.io.InputStreamReader; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.List; - -import static io.trygvis.container.myapp.Contact.Gender.FEMALE; -import static io.trygvis.container.myapp.Contact.Gender.MALE; - -public class AddressBook { - - private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); - - private Connection c; - - public static void main(String[] args) throws Exception { - try { - new AddressBook().work(); - } catch (EOFException ignore) { - } - } - - private void work() throws Exception { - boolean done = false; - while (!done) { - c = DriverManager.getConnection("jdbc:h2:mem:address-book;DB_CLOSE_DELAY=-1"); - try { - c.setAutoCommit(false); - done = main(); - c.commit(); - System.out.println("OK"); - } finally { - c.close(); - } - } - } - - private boolean main() throws Exception { - System.out.println("Menu:"); - System.out.println("c Create"); - System.out.println("d Drop"); - System.out.println("m Company menu"); - System.out.println("n Contact menu"); - System.out.println("q Quit"); - String cmd = cmd(); - switch (cmd) { - case "c": - create(); - break; - case "d": - drop(); - break; - case "m": - company(); - break; - case "n": - contact(); - break; - case "q": - return true; - default: - System.out.println("Unknown command: " + cmd); - } - return false; - } - - private String cmd() throws Exception { - String cmd = null; - do { - String read = line(); - if (read.length() != 0) { - cmd = read.trim(); - } - } while (cmd == null); - return cmd; - } - - private String line() throws Exception { - String line = reader.readLine(); - if (line == null) { - throw new EOFException(); - } - return line.trim(); - } - - public void create() throws SQLException { - Statement statement = c.createStatement(); - statement.executeUpdate(CompanyDao.createTableSql); - statement.executeUpdate(ContactDao.createTableSql); - for (String sql : Sequences.createSequences) { - statement.executeUpdate(sql); - } - } - - public void drop() throws SQLException { - Statement statement = c.createStatement(); - for (String sql : Sequences.dropSequences) { - statement.executeUpdate(sql); - } - statement.executeUpdate(ContactDao.dropTableSql); - statement.executeUpdate(CompanyDao.dropTableSql); - } - - // ----------------------------------------------------------------------- - // Company - // ----------------------------------------------------------------------- - - private void company() throws Exception { - while (true) { - System.out.println("Company menu:"); - System.out.println("c Create"); - System.out.println("d Drop"); - System.out.println("d List"); - System.out.println("q Back"); - String cmd = cmd(); - switch (cmd) { - case "c": - addCompany(); - break; - case "d": - deleteCompany(); - break; - case "l": - listCompanies(); - break; - case "q": - return; - default: - System.out.println("Unknown command: " + cmd); - } - } - } - - public void addCompany() throws Exception { - System.out.print("Name: "); - String name = line(); - - Company company = new Company(name); - CompanyDao.insertInto(c, company); - } - - public void deleteCompany() { - } - - public void listCompanies() { - TypedQuery p = CompanyDao.query(c); - - List resultList = p.getResultList(); - for (Company company : resultList) { - System.out.println("====================="); - System.out.println("Id: " + company.getId()); - System.out.println("Name: " + company.name); - } - System.out.println(); - } - - // ----------------------------------------------------------------------- - // Contact - // ----------------------------------------------------------------------- - - private void contact() throws Exception { - while (true) { - System.out.println("Contact menu:"); - System.out.println("c Create"); - System.out.println("d Drop"); - System.out.println("q Back"); - String cmd = cmd(); - switch (cmd) { - case "c": - addContact(); - break; - case "d": - deleteContact(); - break; - case "l": - listContacts(); - break; - case "q": - return; - default: - System.out.println("Unknown command: " + cmd); - } - } - } - - public void addContact() throws Exception { - System.out.print("Name: "); - String name = line(); - Contact.Gender g = null; - while (g == null) { - System.out.print("Gender (m/f): "); - try { - String line = line(); - switch (line) { - case "m": - g = MALE; - break; - case "f": - g = FEMALE; - break; - default: - g = Contact.Gender.valueOf(line); - break; - } - } catch (IllegalArgumentException ignore) { - } - } - - Company company = null; - Contact o = new Contact(name, g, company); - ContactDao.insertInto(c, o); - } - - public void deleteContact() { - - } - - public void listContacts() { - TypedQuery p = ContactDao.query(c); - - List resultList = p.getResultList(); - for (Contact contact : resultList) { - System.out.println("====================="); - System.out.println("Id: " + contact.getId()); - System.out.println("Name: " + contact.name); - System.out.println("Gender: " + contact.gender); - } - System.out.println(); - } -} diff --git a/myapp/src/main/java/io/trygvis/container/myapp/AddressBookDirect.java b/myapp/src/main/java/io/trygvis/container/myapp/AddressBookDirect.java new file mode 100644 index 0000000..df0f283 --- /dev/null +++ b/myapp/src/main/java/io/trygvis/container/myapp/AddressBookDirect.java @@ -0,0 +1,237 @@ +package io.trygvis.container.myapp; + +import javax.persistence.TypedQuery; +import java.io.BufferedReader; +import java.io.EOFException; +import java.io.InputStreamReader; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.List; + +import static io.trygvis.container.myapp.CompanyDao.Utils.insertCompany; +import static io.trygvis.container.myapp.Contact.Gender.FEMALE; +import static io.trygvis.container.myapp.Contact.Gender.MALE; + +public class AddressBookDirect { + + private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + + private Connection c; + + public static void main(String[] args) throws Exception { + try { + new AddressBookDirect().work(); + } catch (EOFException ignore) { + } + } + + private void work() throws Exception { + boolean done = false; + while (!done) { + c = DriverManager.getConnection("jdbc:h2:mem:address-book;DB_CLOSE_DELAY=-1"); + try { + c.setAutoCommit(false); + done = main(); + c.commit(); + System.out.println("OK"); + } finally { + c.close(); + } + } + } + + private boolean main() throws Exception { + System.out.println("Menu:"); + System.out.println("c Create"); + System.out.println("d Drop"); + System.out.println("m Company menu"); + System.out.println("n Contact menu"); + System.out.println("q Quit"); + String cmd = cmd(); + switch (cmd) { + case "c": + create(); + break; + case "d": + drop(); + break; + case "m": + company(); + break; + case "n": + contact(); + break; + case "q": + return true; + default: + System.out.println("Unknown command: " + cmd); + } + return false; + } + + private String cmd() throws Exception { + String cmd = null; + do { + String read = line(); + if (read.length() != 0) { + cmd = read.trim(); + } + } while (cmd == null); + return cmd; + } + + private String line() throws Exception { + String line = reader.readLine(); + if (line == null) { + throw new EOFException(); + } + return line.trim(); + } + + public void create() throws SQLException { + Statement statement = c.createStatement(); + statement.executeUpdate(CompanyDao.createTableSql); + statement.executeUpdate(ContactDao.createTableSql); + for (String sql : Sequences.createSequences) { + statement.executeUpdate(sql); + } + } + + public void drop() throws SQLException { + Statement statement = c.createStatement(); + for (String sql : Sequences.dropSequences) { + statement.executeUpdate(sql); + } + statement.executeUpdate(ContactDao.dropTableSql); + statement.executeUpdate(CompanyDao.dropTableSql); + } + + // ----------------------------------------------------------------------- + // Company + // ----------------------------------------------------------------------- + + private void company() throws Exception { + while (true) { + System.out.println("Company menu:"); + System.out.println("c Create"); + System.out.println("d Drop"); + System.out.println("d List"); + System.out.println("q Back"); + String cmd = cmd(); + switch (cmd) { + case "c": + addCompany(); + break; + case "d": + deleteCompany(); + break; + case "l": + listCompanies(); + break; + case "q": + return; + default: + System.out.println("Unknown command: " + cmd); + } + } + } + + public void addCompany() throws Exception { + System.out.print("Name: "); + String name = line(); + + Company company = new Company(name); + insertCompany(c, company); + } + + public void deleteCompany() { + } + + public void listCompanies() { + TypedQuery p = CompanyDao.Utils.queryCompany(c); + + List resultList = p.getResultList(); + for (Company company : resultList) { + System.out.println("====================="); + System.out.println("Id: " + company.getId()); + System.out.println("Name: " + company.name); + } + System.out.println(); + } + + // ----------------------------------------------------------------------- + // Contact + // ----------------------------------------------------------------------- + + private void contact() throws Exception { + while (true) { + System.out.println("Contact menu:"); + System.out.println("c Create"); + System.out.println("d Drop"); + System.out.println("q Back"); + String cmd = cmd(); + switch (cmd) { + case "c": + addContact(); + break; + case "d": + deleteContact(); + break; + case "l": + listContacts(); + break; + case "q": + return; + default: + System.out.println("Unknown command: " + cmd); + } + } + } + + public void addContact() throws Exception { + System.out.print("Name: "); + String name = line(); + Contact.Gender g = null; + while (g == null) { + System.out.print("Gender (m/f): "); + try { + String line = line(); + switch (line) { + case "m": + g = MALE; + break; + case "f": + g = FEMALE; + break; + default: + g = Contact.Gender.valueOf(line); + break; + } + } catch (IllegalArgumentException ignore) { + } + } + + Company company = null; + Contact o = new Contact(name, g, company); + ContactDao.Utils.insertContact(c, o); + } + + public void deleteContact() { + + } + + public void listContacts() { + TypedQuery p = ContactDao.Utils.queryContact(c); + + List resultList = p.getResultList(); + for (Contact contact : resultList) { + System.out.println("====================="); + System.out.println("Id: " + contact.getId()); + System.out.println("Name: " + contact.name); + System.out.println("Gender: " + contact.gender); + } + System.out.println(); + } +} diff --git a/myapp/src/main/java/io/trygvis/container/myapp/AddressBookJpa.java b/myapp/src/main/java/io/trygvis/container/myapp/AddressBookJpa.java new file mode 100644 index 0000000..3981805 --- /dev/null +++ b/myapp/src/main/java/io/trygvis/container/myapp/AddressBookJpa.java @@ -0,0 +1,242 @@ +package io.trygvis.container.myapp; + +import io.trygvis.persistence.sql.SqlEntityMeta; +import io.trygvis.persistence.sql.SqlUnit; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.TypedQuery; +import javax.sql.DataSource; +import java.io.BufferedReader; +import java.io.EOFException; +import java.io.InputStreamReader; +import java.sql.SQLException; +import java.util.List; + +import static io.trygvis.container.myapp.Contact.Gender.FEMALE; +import static io.trygvis.container.myapp.Contact.Gender.MALE; + +public class AddressBookJpa { + + private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + + private EntityManager entityManager; + + public static void main(String[] args) throws Exception { + try { + new AddressBookJpa().work(); + } catch (EOFException ignore) { + } + } + + private void work() throws Exception { + DataSource ds = new DriverManagerDataSource("jdbc:h2:mem:address-book;DB_CLOSE_DELAY=-1"); + EntityManagerFactory emf = new MyAppEntityManagerFactory(ds); + boolean done = false; + while (!done) { + try { + entityManager = emf.createEntityManager(); + EntityTransaction transaction = entityManager.getTransaction(); + done = main(); + System.out.println("OK"); + transaction.commit(); + } finally { + entityManager.close(); + } + } + } + + private boolean main() throws Exception { + System.out.println("Menu:"); + System.out.println("c Create"); + System.out.println("d Drop"); + System.out.println("m Company menu"); + System.out.println("n Contact menu"); + System.out.println("q Quit"); + String cmd = cmd(); + switch (cmd) { + case "c": + create(); + break; + case "d": + drop(); + break; + case "m": + company(); + break; + case "n": + contact(); + break; + case "q": + return true; + default: + System.out.println("Unknown command: " + cmd); + } + return false; + } + + private String cmd() throws Exception { + String cmd = null; + do { + String read = line(); + if (read.length() != 0) { + cmd = read.trim(); + } + } while (cmd == null); + return cmd; + } + + private String line() throws Exception { + String line = reader.readLine(); + if (line == null) { + throw new EOFException(); + } + return line.trim(); + } + + public void create() throws SQLException { + for (SqlEntityMeta entityMeta : entityManager.unwrap(SqlUnit.class).getEntities()) { + entityManager.createNativeQuery(entityMeta.createTableSql).executeUpdate(); + } + for (String sql : Sequences.createSequences) { + entityManager.createNativeQuery(sql).executeUpdate(); + } + } + + public void drop() throws SQLException { + for (String sql : Sequences.dropSequences) { + entityManager.createNativeQuery(sql); + } + for (SqlEntityMeta entityMeta : entityManager.unwrap(SqlUnit.class).getEntities()) { + entityManager.createNativeQuery(entityMeta.dropTableSql).executeUpdate(); + } + } + + // ----------------------------------------------------------------------- + // Company + // ----------------------------------------------------------------------- + + private void company() throws Exception { + while (true) { + System.out.println("Company menu:"); + System.out.println("c Create"); + System.out.println("d Drop"); + System.out.println("d List"); + System.out.println("q Back"); + String cmd = cmd(); + switch (cmd) { + case "c": + addCompany(); + break; + case "d": + deleteCompany(); + break; + case "l": + listCompanies(); + break; + case "q": + return; + default: + System.out.println("Unknown command: " + cmd); + } + } + } + + public void addCompany() throws Exception { + System.out.print("Name: "); + String name = line(); + + Company company = new Company(name); + entityManager.persist(company); + } + + public void deleteCompany() { + } + + public void listCompanies() { + TypedQuery p = entityManager.createQuery("", Company.class); + + List resultList = p.getResultList(); + for (Company company : resultList) { + System.out.println("====================="); + System.out.println("Id: " + company.getId()); + System.out.println("Name: " + company.name); + } + System.out.println(); + } + + // ----------------------------------------------------------------------- + // Contact + // ----------------------------------------------------------------------- + + private void contact() throws Exception { + while (true) { + System.out.println("Contact menu:"); + System.out.println("c Create"); + System.out.println("d Drop"); + System.out.println("q Back"); + String cmd = cmd(); + switch (cmd) { + case "c": + addContact(); + break; + case "d": + deleteContact(); + break; + case "l": + listContacts(); + break; + case "q": + return; + default: + System.out.println("Unknown command: " + cmd); + } + } + } + + public void addContact() throws Exception { + System.out.print("Name: "); + String name = line(); + Contact.Gender g = null; + while (g == null) { + System.out.print("Gender (m/f): "); + try { + String line = line(); + switch (line) { + case "m": + g = MALE; + break; + case "f": + g = FEMALE; + break; + default: + g = Contact.Gender.valueOf(line); + break; + } + } catch (IllegalArgumentException ignore) { + } + } + + Company company = null; + Contact o = new Contact(name, g, company); + entityManager.persist(company); + } + + public void deleteContact() { + + } + + public void listContacts() { + TypedQuery p = entityManager.createQuery("", Contact.class); + + List resultList = p.getResultList(); + for (Contact contact : resultList) { + System.out.println("====================="); + System.out.println("Id: " + contact.getId()); + System.out.println("Name: " + contact.name); + System.out.println("Gender: " + contact.gender); + } + System.out.println(); + } +} diff --git a/myapp/src/main/java/io/trygvis/container/myapp/DriverManagerDataSource.java b/myapp/src/main/java/io/trygvis/container/myapp/DriverManagerDataSource.java new file mode 100644 index 0000000..3165f17 --- /dev/null +++ b/myapp/src/main/java/io/trygvis/container/myapp/DriverManagerDataSource.java @@ -0,0 +1,60 @@ +package io.trygvis.container.myapp; + +import javax.sql.DataSource; +import java.io.PrintWriter; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.util.logging.Logger; + +class DriverManagerDataSource implements DataSource { + private final String url; + + DriverManagerDataSource(String url) { + this.url = url; + } + + @Override + public Connection getConnection() throws SQLException { + return DriverManager.getConnection(url); + } + + @Override + public Connection getConnection(String username, String password) throws SQLException { + return DriverManager.getConnection(url, username, password); + } + + @Override + public PrintWriter getLogWriter() throws SQLException { + return null; + } + + @Override + public void setLogWriter(PrintWriter out) throws SQLException { + } + + @Override + public void setLoginTimeout(int seconds) throws SQLException { + } + + @Override + public int getLoginTimeout() throws SQLException { + return 0; + } + + @Override + public Logger getParentLogger() throws SQLFeatureNotSupportedException { + return null; + } + + @Override + public T unwrap(Class iface) throws SQLException { + return null; + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + return false; + } +} diff --git a/myapp/src/main/java/io/trygvis/container/myapp/package-info.java b/myapp/src/main/java/io/trygvis/container/myapp/package-info.java index c212ca1..fdb7353 100644 --- a/myapp/src/main/java/io/trygvis/container/myapp/package-info.java +++ b/myapp/src/main/java/io/trygvis/container/myapp/package-info.java @@ -1,3 +1,3 @@ -@SqlEntitySet package io.trygvis.container.myapp; +@SqlEntitySet(name = "MyApp") package io.trygvis.container.myapp; import io.trygvis.persistence.SqlEntitySet; -- cgit v1.2.3