Created some classes and a generic get method
This commit is contained in:
parent
ef70448ca6
commit
f9cba67116
3 changed files with 85 additions and 20 deletions
|
@ -29,19 +29,23 @@ public class DataProvider implements AutoCloseable {
|
|||
}
|
||||
|
||||
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;
|
||||
return get(User.class);
|
||||
}
|
||||
|
||||
public User getUser(Long id) {
|
||||
public List<Message> getMessages() {
|
||||
return get(Message.class);
|
||||
}
|
||||
|
||||
public List<Conversation> getConversations() {
|
||||
return get(Conversation.class);
|
||||
}
|
||||
|
||||
private <T> List<T> get(Class<T> cl) {
|
||||
EntityManager em = emf.createEntityManager();
|
||||
TypedQuery<User> query = em.createQuery("SELECT u FROM User u WHERE u.id = " + id, User.class);
|
||||
User user = query.getResultList().get(0);
|
||||
TypedQuery<T> query = em.createQuery("SELECT x FROM " + cl.getSimpleName() + " x", cl);
|
||||
List<T> results = query.getResultList();
|
||||
em.close();
|
||||
return user;
|
||||
return results;
|
||||
}
|
||||
|
||||
public void removeUser(User user) {
|
||||
|
@ -53,17 +57,6 @@ 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)
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package io.github.norbipeti.chat.server.db.domain;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class Conversation {
|
||||
private List<Message> messsages;
|
||||
private List<User> users;
|
||||
|
||||
public List<Message> getMesssages() {
|
||||
return messsages;
|
||||
}
|
||||
|
||||
public void setMesssages(List<Message> messsages) {
|
||||
this.messsages = messsages;
|
||||
}
|
||||
|
||||
public List<User> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
public void setUsers(List<User> users) {
|
||||
this.users = users;
|
||||
}
|
||||
}
|
45
src/io/github/norbipeti/chat/server/db/domain/Message.java
Normal file
45
src/io/github/norbipeti/chat/server/db/domain/Message.java
Normal file
|
@ -0,0 +1,45 @@
|
|||
package io.github.norbipeti.chat.server.db.domain;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class Message {
|
||||
private User sender;
|
||||
private Date time;
|
||||
private String message;
|
||||
private Conversation conversation;
|
||||
|
||||
public User getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender(User sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
public Date getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(Date time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public Conversation getConversation() {
|
||||
return conversation;
|
||||
}
|
||||
|
||||
public void setConversation(Conversation conversation) {
|
||||
this.conversation = conversation;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue