From 381c368b8907640bdd7cd81db2294724e082a119 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 4 Aug 2013 17:17:39 +0200 Subject: wip --- myapp/pom.xml | 5 ----- .../io/trygvis/container/myapp/AbstractEntity.java | 20 +++++++++++++++++++ .../io/trygvis/container/myapp/AddressBook.java | 19 +++++++++--------- .../java/io/trygvis/container/myapp/Contact.java | 23 ++++++++++++++++++++++ .../java/io/trygvis/container/myapp/Person.java | 23 ---------------------- 5 files changed, 53 insertions(+), 37 deletions(-) create mode 100644 myapp/src/main/java/io/trygvis/container/myapp/AbstractEntity.java create mode 100644 myapp/src/main/java/io/trygvis/container/myapp/Contact.java delete mode 100644 myapp/src/main/java/io/trygvis/container/myapp/Person.java (limited to 'myapp') diff --git a/myapp/pom.xml b/myapp/pom.xml index 8851022..8084e3e 100644 --- a/myapp/pom.xml +++ b/myapp/pom.xml @@ -31,11 +31,6 @@ maven-compiler-plugin - io.trygvis.container.compiler.MyProcessor diff --git a/myapp/src/main/java/io/trygvis/container/myapp/AbstractEntity.java b/myapp/src/main/java/io/trygvis/container/myapp/AbstractEntity.java new file mode 100644 index 0000000..40896e0 --- /dev/null +++ b/myapp/src/main/java/io/trygvis/container/myapp/AbstractEntity.java @@ -0,0 +1,20 @@ +package io.trygvis.container.myapp; + +import javax.persistence.Id; + +public abstract class AbstractEntity { + private Long id; + + protected AbstractEntity(Long id) { + this.id = id; + } + + @Id + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } +} diff --git a/myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java b/myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java index 0120b6a..0101004 100644 --- a/myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java +++ b/myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java @@ -86,7 +86,7 @@ public class AddressBook { @Override public void run(Connection c) throws Exception { Statement statement = c.createStatement(); - statement.executeUpdate(PersonDao.createTableSql); + statement.executeUpdate(ContactDao.createTableSql); for (String sql : Sequences.sequences) { statement.executeUpdate(sql); } @@ -97,7 +97,7 @@ public class AddressBook { @Override public void run(Connection c) throws Exception { Statement statement = c.createStatement(); - statement.executeUpdate(PersonDao.dropTableSql); + statement.executeUpdate(ContactDao.dropTableSql); } } @@ -106,21 +106,22 @@ public class AddressBook { public void run(Connection c) throws Exception { System.out.print("Name: "); String name = line(); - Person o = new Person(name); - PersonDao.insertInto(c, o); + Contact o = new Contact(name); + ContactDao.insertInto(c, o); } } public static class ListCommand implements Command { @Override public void run(Connection c) throws Exception { - TypedQuery p = PersonDao.query(c); + TypedQuery p = ContactDao.query(c); - List resultList = p.getResultList(); + List resultList = p.getResultList(); for (int i = 0; i < resultList.size(); i++) { - Person person = resultList.get(i); - System.out.println("#" + i); - System.out.println("Name: " + person.name); + Contact contact = resultList.get(i); + System.out.println("Item #" + i + ":"); + System.out.println("Id: " + contact.getId()); + System.out.println("Name: " + contact.name); } } } diff --git a/myapp/src/main/java/io/trygvis/container/myapp/Contact.java b/myapp/src/main/java/io/trygvis/container/myapp/Contact.java new file mode 100644 index 0000000..87e58c5 --- /dev/null +++ b/myapp/src/main/java/io/trygvis/container/myapp/Contact.java @@ -0,0 +1,23 @@ +package io.trygvis.container.myapp; + +import javax.persistence.Entity; +import javax.persistence.SequenceGenerator; + +@Entity +@SequenceGenerator(name = "id_seq") +public class Contact extends AbstractEntity { + + public String name; + +// public Contact mother; + + public Contact(String name) { + super(null); + this.name = name; + } + + public Contact(Long id, String name) { + super(id); + this.name = name; + } +} diff --git a/myapp/src/main/java/io/trygvis/container/myapp/Person.java b/myapp/src/main/java/io/trygvis/container/myapp/Person.java deleted file mode 100644 index be9026a..0000000 --- a/myapp/src/main/java/io/trygvis/container/myapp/Person.java +++ /dev/null @@ -1,23 +0,0 @@ -package io.trygvis.container.myapp; - -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.SequenceGenerator; - -@Entity -@SequenceGenerator(name = "id_seq") -public class Person { - @Id - public Long id; - - public String name; - - public Person(String name) { - this.name = name; - } - - public Person(Long id, String name) { - this.id = id; - this.name = name; - } -} -- cgit v1.2.3