From c07a0d6207166b8ea3746cec91036b53cd01b0c0 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 19 Jul 2016 13:41:31 +0200 Subject: [PATCH] Made contacts --- src/io/github/norbipeti/chat/server/Main.java | 9 ++++- .../chat/server/db/DataProvider.java | 19 ++++++++++ .../chat/server/db/domain/Contact.java | 38 ------------------- .../norbipeti/chat/server/db/domain/User.java | 18 ++++++++- 4 files changed, 43 insertions(+), 41 deletions(-) delete mode 100644 src/io/github/norbipeti/chat/server/db/domain/Contact.java diff --git a/src/io/github/norbipeti/chat/server/Main.java b/src/io/github/norbipeti/chat/server/Main.java index 711da38..8c4a111 100644 --- a/src/io/github/norbipeti/chat/server/Main.java +++ b/src/io/github/norbipeti/chat/server/Main.java @@ -5,6 +5,9 @@ import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.Set; +import javax.persistence.EntityManager; + +import org.hibernate.Hibernate; import org.reflections.Reflections; import org.reflections.scanners.SubTypesScanner; import org.reflections.util.ClasspathHelper; @@ -28,8 +31,10 @@ public class Main { try (DataProvider provider = new DataProvider()) { User user = new User(); provider.addUser(user); - System.out.println(provider.getUsers()); - provider.removeUser(user); + User user2 = new User(); + user2.setName("Teszt"); + user2.getContacts().add(user.getId()); + provider.addUser(user2); System.out.println(provider.getUsers()); } System.out.println("Starting webserver..."); diff --git a/src/io/github/norbipeti/chat/server/db/DataProvider.java b/src/io/github/norbipeti/chat/server/db/DataProvider.java index bbaffdf..4abbb38 100644 --- a/src/io/github/norbipeti/chat/server/db/DataProvider.java +++ b/src/io/github/norbipeti/chat/server/db/DataProvider.java @@ -36,6 +36,14 @@ public class DataProvider implements AutoCloseable { return users; } + public List getUser(Long id) { //TODO + EntityManager em = emf.createEntityManager(); + TypedQuery query = em.createQuery("SELECT u FROM User u", User.class); + List users = query.getResultList(); + em.close(); + return users; + } + public void removeUser(User user) { EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); @@ -45,6 +53,17 @@ public class DataProvider implements AutoCloseable { em.close(); } + public EntityManager startTransaction() { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + return em; + } + + public void endTransaction(EntityManager em) { + em.getTransaction().commit(); + em.close(); + } + @Override public void close() { if (emf != null) diff --git a/src/io/github/norbipeti/chat/server/db/domain/Contact.java b/src/io/github/norbipeti/chat/server/db/domain/Contact.java deleted file mode 100644 index ce754af..0000000 --- a/src/io/github/norbipeti/chat/server/db/domain/Contact.java +++ /dev/null @@ -1,38 +0,0 @@ -package io.github.norbipeti.chat.server.db.domain; - -import javax.persistence.*; - -@Entity -public class Contact { - @ManyToOne - private User user1; - @ManyToOne - private User user2; - - public User getUser1() { - return user1; - } - - public void setUser1(User user1) { - this.user1 = user1; - } - - public User getUser2() { - return user2; - } - - public void setUser2(User user2) { - this.user2 = user2; - } - - @Id - private Long getId() { - if (user1 == null) - return null; - return user1.getId(); - } - - public Contact() { - - } -} diff --git a/src/io/github/norbipeti/chat/server/db/domain/User.java b/src/io/github/norbipeti/chat/server/db/domain/User.java index 81d9205..1848284 100644 --- a/src/io/github/norbipeti/chat/server/db/domain/User.java +++ b/src/io/github/norbipeti/chat/server/db/domain/User.java @@ -1,5 +1,8 @@ package io.github.norbipeti.chat.server.db.domain; +import java.util.ArrayList; +import java.util.List; + import javax.persistence.*; @Entity @@ -12,6 +15,18 @@ public class User { private String name; private String email; private String password; + @ElementCollection(fetch = FetchType.EAGER) + private List contacts; + + public List getContacts() { + if (contacts == null) + contacts = new ArrayList<>(); + return contacts; + } + + public void setContacts(List contacts) { + this.contacts = contacts; + } public String getName() { return name; @@ -27,7 +42,8 @@ public class User { @Override public String toString() { - return "User [id=" + id + ", name=" + name + ", email=" + email + ", password=" + password + "]"; + return "User [id=" + id + ", name=" + name + ", email=" + email + ", password=" + password + ", contacts=" + + contacts + "]"; } public void setEmail(String email) {