mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-15 11:39:11 +03:00
[FEATURE] Удален SessionFactoryManager
This commit is contained in:
parent
8ef61b8673
commit
c44208fed3
3 changed files with 14 additions and 35 deletions
|
@ -9,30 +9,29 @@
|
|||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.Transaction;
|
||||
|
||||
import pro.gravit.launcher.OshiHWID;
|
||||
import pro.gravit.launchserver.LaunchServer;
|
||||
import pro.gravit.launchserver.dao.User;
|
||||
import pro.gravit.launchserver.dao.UserDAO;
|
||||
import pro.gravit.launchserver.dao.UserHWID;
|
||||
import pro.gravit.launchserver.hibernate.SessionFactoryManager;
|
||||
|
||||
public class HibernateUserDAOImpl implements UserDAO {
|
||||
private final SessionFactoryManager manager;
|
||||
private final SessionFactory factory;
|
||||
|
||||
public HibernateUserDAOImpl(LaunchServer srv) {
|
||||
manager = SessionFactoryManager.forLaunchServer(srv);
|
||||
public HibernateUserDAOImpl(SessionFactory factory) {
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
public User findById(int id) {
|
||||
try (Session s = manager.fact.openSession()) {
|
||||
try (Session s = factory.openSession()) {
|
||||
return s.get(User.class, id);
|
||||
}
|
||||
}
|
||||
|
||||
public User findByUsername(String username) {
|
||||
EntityManager em = manager.fact.createEntityManager();
|
||||
EntityManager em = factory.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
|
||||
|
@ -44,7 +43,7 @@ public User findByUsername(String username) {
|
|||
}
|
||||
|
||||
public User findByUUID(UUID uuid) {
|
||||
EntityManager em = manager.fact.createEntityManager();
|
||||
EntityManager em = factory.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
|
||||
|
@ -57,7 +56,7 @@ public User findByUUID(UUID uuid) {
|
|||
|
||||
@Override
|
||||
public List<UserHWID> findHWID(OshiHWID hwid) {
|
||||
EntityManager em = manager.fact.createEntityManager();
|
||||
EntityManager em = factory.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<UserHWID> personCriteria = cb.createQuery(UserHWID.class);
|
||||
|
@ -77,7 +76,7 @@ public List<UserHWID> findHWID(OshiHWID hwid) {
|
|||
}
|
||||
|
||||
public void save(User user) {
|
||||
try (Session session = manager.fact.openSession()) {
|
||||
try (Session session = factory.openSession()) {
|
||||
Transaction tx1 = session.beginTransaction();
|
||||
session.save(user);
|
||||
tx1.commit();
|
||||
|
@ -85,7 +84,7 @@ public void save(User user) {
|
|||
}
|
||||
|
||||
public void update(User user) {
|
||||
try (Session session = manager.fact.openSession()) {
|
||||
try (Session session = factory.openSession()) {
|
||||
Transaction tx1 = session.beginTransaction();
|
||||
session.update(user);
|
||||
tx1.commit();
|
||||
|
@ -93,7 +92,7 @@ public void update(User user) {
|
|||
}
|
||||
|
||||
public void delete(User user) {
|
||||
try (Session session = manager.fact.openSession()) {
|
||||
try (Session session = factory.openSession()) {
|
||||
Transaction tx1 = session.beginTransaction();
|
||||
session.delete(user);
|
||||
tx1.commit();
|
||||
|
@ -102,7 +101,7 @@ public void delete(User user) {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<User> findAll() {
|
||||
try (Session s = manager.fact.openSession()) {
|
||||
try (Session s = factory.openSession()) {
|
||||
return (List<User>) s.createQuery("From User").list();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
import pro.gravit.launchserver.dao.UserHWID;
|
||||
import pro.gravit.launchserver.dao.UserService;
|
||||
import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl;
|
||||
import pro.gravit.launchserver.hibernate.SessionFactoryManager;
|
||||
import pro.gravit.utils.helper.CommonHelper;
|
||||
|
||||
import java.nio.file.Paths;
|
||||
|
@ -22,8 +21,6 @@ public class HibernateDaoProvider extends DaoProvider {
|
|||
|
||||
@Override
|
||||
public void init(LaunchServer server) {
|
||||
userDAO = new HibernateUserDAOImpl(server);
|
||||
userService = new UserService(userDAO);
|
||||
Runnable init = () -> {
|
||||
Configuration cfg = new Configuration()
|
||||
.addAnnotatedClass(User.class)
|
||||
|
@ -35,7 +32,8 @@ public void init(LaunchServer server) {
|
|||
.setProperty("hibernate.connection.pool_size", pool_size);
|
||||
if(hibernateConfig != null)
|
||||
cfg.configure(Paths.get(hibernateConfig).toFile());
|
||||
SessionFactoryManager.forLaunchServer(server).fact = cfg.buildSessionFactory();
|
||||
userDAO = new HibernateUserDAOImpl(cfg.buildSessionFactory());
|
||||
userService = new UserService(userDAO);
|
||||
};
|
||||
if(parallelHibernateInit)
|
||||
CommonHelper.newThread("Hibernate Thread", true, init);
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
package pro.gravit.launchserver.hibernate;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
import pro.gravit.launchserver.LaunchServer;
|
||||
|
||||
public class SessionFactoryManager {
|
||||
public SessionFactory fact;
|
||||
private static final Map<LaunchServer, SessionFactoryManager> sessionFactories = new ConcurrentHashMap<>();
|
||||
private static final Function<LaunchServer, SessionFactoryManager> cr = e -> new SessionFactoryManager();
|
||||
public static SessionFactoryManager forLaunchServer(LaunchServer srv) {
|
||||
return sessionFactories.computeIfAbsent(srv, cr);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue