[FIX] Hibernate.

This commit is contained in:
Zaxar163 2019-06-03 13:56:54 +03:00
parent 05e98bcad6
commit 2dde0bb5e1
6 changed files with 42 additions and 17 deletions

View file

@ -21,7 +21,7 @@ public class HibernateConfiguratorComponent extends Component {
public boolean parallelHibernateInit;
@Override
public void preInit(LaunchServer launchServer) {
LaunchServerDaoFactory.setUserDaoProvider(HibernateUserDAOImpl::new);
LaunchServerDaoFactory.setUserDaoProvider(launchServer, HibernateUserDAOImpl::new);
Runnable init = () -> {
Configuration cfg = new Configuration()
.addAnnotatedClass(User.class)
@ -32,7 +32,7 @@ public void preInit(LaunchServer launchServer) {
.setProperty("hibernate.connection.pool_size", pool_size);
if(hibernateConfig != null)
cfg.configure(Paths.get(hibernateConfig).toFile());
HibernateManager.sessionFactory = cfg.buildSessionFactory();
HibernateManager.setSessionFactory(launchServer, cfg.buildSessionFactory());
};
if(parallelHibernateInit)
CommonHelper.newThread("Hibernate Thread", true, init);

View file

@ -1,18 +1,22 @@
package pro.gravit.launchserver.dao;
import java.util.function.Supplier;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.dao.impl.DefaultUserDAOImpl;
public class LaunchServerDaoFactory {
private static Supplier<UserDAO> getUserDao = DefaultUserDAOImpl::new;
private static final Function<LaunchServer, UserDAO> defDao = DefaultUserDAOImpl::new;
private static final Map<LaunchServer, Function<LaunchServer, UserDAO>> daos = new ConcurrentHashMap<>();
public static void setUserDaoProvider(Supplier<UserDAO> getDao) {
LaunchServerDaoFactory.getUserDao = getDao;
public static void setUserDaoProvider(LaunchServer srv, Function<LaunchServer, UserDAO> getDao) {
daos.put(srv, getDao);
}
public static UserDAO createUserDao()
public static UserDAO createUserDao(LaunchServer srv)
{
return getUserDao.get();
return daos.getOrDefault(srv, defDao).apply(srv);
}
}

View file

@ -10,7 +10,7 @@ public class UserService {
private final UserDAO usersDao;
public UserService(LaunchServer server) {
usersDao = LaunchServerDaoFactory.createUserDao();
usersDao = LaunchServerDaoFactory.createUserDao(server);
}
public User findUser(int id) {

View file

@ -4,10 +4,12 @@
import java.util.List;
import java.util.UUID;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.dao.User;
import pro.gravit.launchserver.dao.UserDAO;
public class DefaultUserDAOImpl implements UserDAO {
public DefaultUserDAOImpl(LaunchServer srv) { }
@Override
public User findById(int id) {
return null;

View file

@ -11,18 +11,24 @@
import org.hibernate.Session;
import org.hibernate.Transaction;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.dao.User;
import pro.gravit.launchserver.dao.UserDAO;
import pro.gravit.launchserver.hibernate.HibernateManager;
public class HibernateUserDAOImpl implements UserDAO {
private final LaunchServer srv;
public HibernateUserDAOImpl(LaunchServer srv) {
this.srv = srv;
}
public User findById(int id) {
return HibernateManager.sessionFactory.openSession().get(User.class, id);
return HibernateManager.getSessionFactory(srv).openSession().get(User.class, id);
}
public User findByUsername(String username) {
EntityManager em = HibernateManager.sessionFactory.createEntityManager();
EntityManager em = HibernateManager.getSessionFactory(srv).createEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
@ -34,7 +40,7 @@ public User findByUsername(String username) {
}
public User findByUUID(UUID uuid) {
EntityManager em = HibernateManager.sessionFactory.createEntityManager();
EntityManager em = HibernateManager.getSessionFactory(srv).createEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
@ -46,7 +52,7 @@ public User findByUUID(UUID uuid) {
}
public void save(User user) {
Session session = HibernateManager.sessionFactory.openSession();
Session session = HibernateManager.getSessionFactory(srv).openSession();
Transaction tx1 = session.beginTransaction();
session.save(user);
tx1.commit();
@ -54,7 +60,7 @@ public void save(User user) {
}
public void update(User user) {
Session session = HibernateManager.sessionFactory.openSession();
Session session = HibernateManager.getSessionFactory(srv).openSession();
Transaction tx1 = session.beginTransaction();
session.update(user);
tx1.commit();
@ -62,7 +68,7 @@ public void update(User user) {
}
public void delete(User user) {
Session session = HibernateManager.sessionFactory.openSession();
Session session = HibernateManager.getSessionFactory(srv).openSession();
Transaction tx1 = session.beginTransaction();
session.delete(user);
tx1.commit();
@ -71,6 +77,6 @@ public void delete(User user) {
@SuppressWarnings("unchecked")
public List<User> findAll() {
return (List<User>) HibernateManager.sessionFactory.openSession().createQuery("From User").list();
return (List<User>) HibernateManager.getSessionFactory(srv).openSession().createQuery("From User").list();
}
}

View file

@ -1,7 +1,20 @@
package pro.gravit.launchserver.hibernate;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.hibernate.SessionFactory;
import pro.gravit.launchserver.LaunchServer;
public class HibernateManager {
public static SessionFactory sessionFactory;
private static final Map<LaunchServer, SessionFactory> sessionMap = new ConcurrentHashMap<>();
public static SessionFactory getSessionFactory(LaunchServer srv) {
return sessionMap.get(srv);
};
public static SessionFactory setSessionFactory(LaunchServer srv, SessionFactory fact) {
return sessionMap.get(srv);
};
}