summaryrefslogtreecommitdiff
path: root/myapp/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'myapp/src/main')
-rw-r--r--myapp/src/main/java/io/trygvis/container/myapp/AbstractEntity.java20
-rw-r--r--myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java19
-rw-r--r--myapp/src/main/java/io/trygvis/container/myapp/Contact.java (renamed from myapp/src/main/java/io/trygvis/container/myapp/Person.java)14
3 files changed, 37 insertions, 16 deletions
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<Person> p = PersonDao.query(c);
+ TypedQuery<Contact> p = ContactDao.query(c);
- List<Person> resultList = p.getResultList();
+ List<Contact> 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/Person.java b/myapp/src/main/java/io/trygvis/container/myapp/Contact.java
index be9026a..87e58c5 100644
--- a/myapp/src/main/java/io/trygvis/container/myapp/Person.java
+++ b/myapp/src/main/java/io/trygvis/container/myapp/Contact.java
@@ -1,23 +1,23 @@
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 class Contact extends AbstractEntity {
public String name;
- public Person(String name) {
+// public Contact mother;
+
+ public Contact(String name) {
+ super(null);
this.name = name;
}
- public Person(Long id, String name) {
- this.id = id;
+ public Contact(Long id, String name) {
+ super(id);
this.name = name;
}
}