mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 09:01:08 +03:00
[FIX] Переписан код на try-with-resources в HibernateDAOImpl
This commit is contained in:
parent
2dde0bb5e1
commit
11a23b4464
3 changed files with 29 additions and 44 deletions
|
@ -2,13 +2,13 @@
|
|||
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
|
||||
import pro.gravit.launchserver.LaunchServer;
|
||||
import pro.gravit.launchserver.dao.LaunchServerDaoFactory;
|
||||
import pro.gravit.launchserver.dao.User;
|
||||
import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl;
|
||||
import pro.gravit.launchserver.hibernate.HibernateManager;
|
||||
import pro.gravit.utils.helper.CommonHelper;
|
||||
|
||||
public class HibernateConfiguratorComponent extends Component {
|
||||
|
@ -19,6 +19,7 @@ public class HibernateConfiguratorComponent extends Component {
|
|||
public String pool_size;
|
||||
public String hibernateConfig;
|
||||
public boolean parallelHibernateInit;
|
||||
public SessionFactory sessionFactory = null;
|
||||
@Override
|
||||
public void preInit(LaunchServer launchServer) {
|
||||
LaunchServerDaoFactory.setUserDaoProvider(launchServer, HibernateUserDAOImpl::new);
|
||||
|
@ -32,7 +33,7 @@ public void preInit(LaunchServer launchServer) {
|
|||
.setProperty("hibernate.connection.pool_size", pool_size);
|
||||
if(hibernateConfig != null)
|
||||
cfg.configure(Paths.get(hibernateConfig).toFile());
|
||||
HibernateManager.setSessionFactory(launchServer, cfg.buildSessionFactory());
|
||||
sessionFactory = cfg.buildSessionFactory();
|
||||
};
|
||||
if(parallelHibernateInit)
|
||||
CommonHelper.newThread("Hibernate Thread", true, init);
|
||||
|
|
|
@ -12,23 +12,25 @@
|
|||
import org.hibernate.Transaction;
|
||||
|
||||
import pro.gravit.launchserver.LaunchServer;
|
||||
import pro.gravit.launchserver.components.HibernateConfiguratorComponent;
|
||||
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;
|
||||
private final HibernateConfiguratorComponent hibernate;
|
||||
|
||||
public HibernateUserDAOImpl(LaunchServer srv) {
|
||||
this.srv = srv;
|
||||
hibernate = (HibernateConfiguratorComponent) srv.config.components.values().stream().filter(e -> HibernateConfiguratorComponent.class.isInstance(e)).findFirst().get();
|
||||
}
|
||||
|
||||
public User findById(int id) {
|
||||
return HibernateManager.getSessionFactory(srv).openSession().get(User.class, id);
|
||||
try (Session s = hibernate.sessionFactory.openSession()) {
|
||||
return s.get(User.class, id);
|
||||
}
|
||||
}
|
||||
|
||||
public User findByUsername(String username) {
|
||||
EntityManager em = HibernateManager.getSessionFactory(srv).createEntityManager();
|
||||
EntityManager em = hibernate.sessionFactory.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
|
||||
|
@ -40,7 +42,7 @@ public User findByUsername(String username) {
|
|||
}
|
||||
|
||||
public User findByUUID(UUID uuid) {
|
||||
EntityManager em = HibernateManager.getSessionFactory(srv).createEntityManager();
|
||||
EntityManager em = hibernate.sessionFactory.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
|
||||
|
@ -52,31 +54,33 @@ public User findByUUID(UUID uuid) {
|
|||
}
|
||||
|
||||
public void save(User user) {
|
||||
Session session = HibernateManager.getSessionFactory(srv).openSession();
|
||||
Transaction tx1 = session.beginTransaction();
|
||||
session.save(user);
|
||||
tx1.commit();
|
||||
session.close();
|
||||
try (Session session = hibernate.sessionFactory.openSession()) {
|
||||
Transaction tx1 = session.beginTransaction();
|
||||
session.save(user);
|
||||
tx1.commit();
|
||||
}
|
||||
}
|
||||
|
||||
public void update(User user) {
|
||||
Session session = HibernateManager.getSessionFactory(srv).openSession();
|
||||
Transaction tx1 = session.beginTransaction();
|
||||
session.update(user);
|
||||
tx1.commit();
|
||||
session.close();
|
||||
try (Session session = hibernate.sessionFactory.openSession()) {
|
||||
Transaction tx1 = session.beginTransaction();
|
||||
session.update(user);
|
||||
tx1.commit();
|
||||
}
|
||||
}
|
||||
|
||||
public void delete(User user) {
|
||||
Session session = HibernateManager.getSessionFactory(srv).openSession();
|
||||
Transaction tx1 = session.beginTransaction();
|
||||
session.delete(user);
|
||||
tx1.commit();
|
||||
session.close();
|
||||
try (Session session = hibernate.sessionFactory.openSession()) {
|
||||
Transaction tx1 = session.beginTransaction();
|
||||
session.delete(user);
|
||||
tx1.commit();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<User> findAll() {
|
||||
return (List<User>) HibernateManager.getSessionFactory(srv).openSession().createQuery("From User").list();
|
||||
try (Session s = hibernate.sessionFactory.openSession()) {
|
||||
return (List<User>) s.createQuery("From User").list();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
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 {
|
||||
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);
|
||||
};
|
||||
}
|
Loading…
Reference in a new issue