Database management

This commit is contained in:
Norbi Peti 2016-07-19 12:45:42 +02:00
parent b948843675
commit 345e8e27b5
5 changed files with 139 additions and 4 deletions

View file

@ -12,7 +12,8 @@ import org.reflections.util.ConfigurationBuilder;
import com.sun.net.httpserver.HttpServer;
import io.github.norbipeti.chat.server.db.DataProvider;
import io.github.norbipeti.chat.server.db.*;
import io.github.norbipeti.chat.server.db.domain.*;
import io.github.norbipeti.chat.server.page.*;
public class Main {
@ -25,7 +26,11 @@ public class Main {
// https://docs.oracle.com/javase/8/docs/api/
System.out.println("Loading database...");
try (DataProvider provider = new DataProvider()) {
User user = new User();
provider.addUser(user);
System.out.println(provider.getUsers());
provider.removeUser(user);
System.out.println(provider.getUsers());
}
System.out.println("Starting webserver...");
HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), 8080), 10);

View file

@ -1,13 +1,48 @@
package io.github.norbipeti.chat.server.db;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import io.github.norbipeti.chat.server.db.domain.*;
public class DataProvider implements AutoCloseable {
private EntityManagerFactory emf;
public DataProvider() {
Persistence.createEntityManagerFactory("ChatServerPU");
emf = Persistence.createEntityManagerFactory("ChatServerPU");
}
public void addUser(User user) {
save(user);
}
private void save(Object object) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(object);
em.getTransaction().commit();
em.close();
}
public List<User> getUsers() {
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();
User managedUser = em.find(User.class, user.getId());
em.remove(managedUser);
em.getTransaction().commit();
em.close();
}
@Override

View file

@ -0,0 +1,38 @@
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

@ -0,0 +1,56 @@
package io.github.norbipeti.chat.server.db.domain;
import javax.persistence.*;
@Entity
@Table(name = "\"User\"")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
private String name;
private String email;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", email=" + email + ", password=" + password + "]";
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public User() {
}
}

View file

@ -5,7 +5,8 @@
<persistence-unit name="ChatServerPU">
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:chatserver;create=true" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:chatserver;create=true" /><property
name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>