Made contacts

This commit is contained in:
Norbi Peti 2016-07-19 13:41:31 +02:00
parent 345e8e27b5
commit c07a0d6207
4 changed files with 43 additions and 41 deletions

View file

@ -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...");

View file

@ -36,6 +36,14 @@ public class DataProvider implements AutoCloseable {
return users;
}
public List<User> getUser(Long id) { //TODO
EntityManager em = emf.createEntityManager();
TypedQuery<User> query = em.createQuery("SELECT u FROM User u", User.class);
List<User> 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)

View file

@ -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() {
}
}

View file

@ -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<Long> contacts;
public List<Long> getContacts() {
if (contacts == null)
contacts = new ArrayList<>();
return contacts;
}
public void setContacts(List<Long> 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) {