Migrate all to entity manager.

main
Paulo Gustavo Veiga 2024-01-13 18:49:49 -08:00
parent fbbc95fd59
commit 2f32ef66e9
4 changed files with 44 additions and 95 deletions

View File

@ -3,14 +3,8 @@ package com.wisemapping.config;
import com.wisemapping.model.User;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
//@Configuration
//@EnableTransactionManagement
//@EnableJpaRepositories("com.wisemapping.model")
@Configuration

View File

@ -19,10 +19,8 @@ package com.wisemapping.dao;
import com.wisemapping.model.Label;
import com.wisemapping.model.User;
import jakarta.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.SelectionQuery;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
@ -34,7 +32,7 @@ import java.util.List;
public class LabelManagerImpl
implements LabelManager {
@Autowired
private SessionFactory sessionFactory;
private EntityManager entityManager;
@Override
public void addLabel(@NotNull final Label label) {
@ -43,26 +41,21 @@ public class LabelManagerImpl
@Override
public void saveLabel(@NotNull final Label label) {
getSession().persist(label);
}
private Session getSession() {
return sessionFactory.getCurrentSession();
entityManager.persist(label);
}
@NotNull
@Override
public List<Label> getAllLabels(@NotNull final User user) {
final SelectionQuery<Label> query = getSession().createSelectionQuery("from com.wisemapping.model.Label wisemapping where creator=:creatorId", Label.class);
final TypedQuery<Label> query = entityManager.createQuery("from com.wisemapping.model.Label wisemapping where creator=:creatorId", Label.class);
query.setParameter("creatorId", user);
return query.list();
return query.getResultList();
}
@Nullable
@Override
public Label getLabelById(int id, @NotNull final User user) {
final Session session = getSession();
final SelectionQuery<Label> query = session.createSelectionQuery("from com.wisemapping.model.Label wisemapping where id=:id and creator=:creator", Label.class);
final TypedQuery<Label> query = entityManager.createQuery("from com.wisemapping.model.Label wisemapping where id=:id and creator=:creator", Label.class);
query.setParameter("id", id);
query.setParameter("creator", user);
@ -73,15 +66,15 @@ public class LabelManagerImpl
@Nullable
@Override
public Label getLabelByTitle(@NotNull String title, @NotNull final User user) {
final SelectionQuery<Label> query = getSession().createSelectionQuery("from com.wisemapping.model.Label wisemapping where title=:title and creator=:creator", Label.class);
final TypedQuery<Label> query = entityManager.createQuery("from com.wisemapping.model.Label wisemapping where title=:title and creator=:creator", Label.class);
query.setParameter("title", title);
query.setParameter("creator", user);
return getFirst(query.list());
return query.getSingleResult();
}
@Override
public void removeLabel(@NotNull Label label) {
getSession().remove(label);
entityManager.remove(label);
}
@Nullable

View File

@ -19,15 +19,12 @@
package com.wisemapping.dao;
import com.wisemapping.model.*;
import jakarta.annotation.Resource;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaDelete;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.SelectionQuery;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
@ -41,15 +38,12 @@ public class MindmapManagerImpl
implements MindmapManager {
@Autowired
private EntityManagerFactory entityManagerFactory;
@Autowired
private SessionFactory sessionFactory;
private EntityManager entityManager;
@Override
public Collaborator findCollaborator(@NotNull final String email) {
final Collaborator collaborator;
final SelectionQuery<Collaborator> query = getSession().createSelectionQuery("from com.wisemapping.model.Collaborator collaborator where email=:email", Collaborator.class);
final TypedQuery<Collaborator> query = entityManager.createQuery("from com.wisemapping.model.Collaborator collaborator where email=:email", Collaborator.class);
query.setParameter("email", email);
final List<Collaborator> collaborators = query.getResultList();
@ -62,14 +56,9 @@ public class MindmapManagerImpl
return collaborator;
}
private Session getSession() {
return sessionFactory.getCurrentSession();
}
@Override
public List<MindMapHistory> getHistoryFrom(int mindmapId) {
final Session session = getSession();
final CriteriaBuilder cb = session.getCriteriaBuilder();
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaQuery<MindMapHistory> cr = cb.createQuery(MindMapHistory.class);
final Root<MindMapHistory> root = cr.from(MindMapHistory.class);
@ -78,7 +67,7 @@ public class MindmapManagerImpl
.where(cb.equal(root.get("mindmapId"), mindmapId))
.orderBy(cb.desc(root.get("creationTime")));
return session.
return entityManager.
createQuery(select)
.setMaxResults(30)
.getResultList();
@ -86,21 +75,19 @@ public class MindmapManagerImpl
@Override
public MindMapHistory getHistory(int historyId) {
final Session session = getSession();
return session.find(MindMapHistory.class, historyId);
return entityManager.find(MindMapHistory.class, historyId);
}
@Override
public void updateCollaboration(@NotNull Collaboration collaboration) {
final Session session = getSession();
session.persist(collaboration);
entityManager.persist(collaboration);
}
@Override
public List<Mindmap> findMindmapByUser(@NotNull User user) {
final SelectionQuery<Mindmap> query = getSession()
.createSelectionQuery("from com.wisemapping.model.Mindmap m where m.id in (select c.mindMap.id from com.wisemapping.model.Collaboration as c where c.collaborator.id=:collabId )", Mindmap.class);
final TypedQuery<Mindmap> query = entityManager
.createQuery("from com.wisemapping.model.Mindmap m where m.id in (select c.mindMap.id from com.wisemapping.model.Collaboration as c where c.collaborator.id=:collabId )", Mindmap.class);
query.setParameter("collabId", user.getId());
return query.getResultList();
@ -108,41 +95,37 @@ public class MindmapManagerImpl
@Override
public List<Collaboration> findCollaboration(final int collaboratorId) {
final SelectionQuery<Collaboration> query = getSession().createSelectionQuery("from com.wisemapping.model.Collaboration c where c.collaborator.id=:collaboratorId", Collaboration.class);
final TypedQuery<Collaboration> query = entityManager.createQuery("from com.wisemapping.model.Collaboration c where c.collaborator.id=:collaboratorId", Collaboration.class);
query.setParameter("collaboratorId", collaboratorId);
return query.getResultList();
}
@Override
public void addCollaborator(@NotNull Collaborator collaborator) {
final Session session = getSession();
assert collaborator != null : "ADD MINDMAP COLLABORATOR: Collaborator is required!";
session.persist(collaborator);
entityManager.persist(collaborator);
}
@Override
public void removeCollaboration(Collaboration collaboration) {
final Session session = getSession();
session.remove(collaboration);
entityManager.remove(collaboration);
}
@Override
public void removeCollaborator(@NotNull Collaborator collaborator) {
final Session session = getSession();
session.remove(collaborator);
entityManager.remove(collaborator);
}
@Override
@Nullable
public Mindmap getMindmapById(int id) {
final Session session = getSession();
return session.get(Mindmap.class, id);
return entityManager.find(Mindmap.class, id);
}
@Override
public Mindmap getMindmapByTitle(final String title, final User user) {
final Mindmap result;
final SelectionQuery<Mindmap> query = getSession().createSelectionQuery("from com.wisemapping.model.Mindmap wisemapping where title=:title and creator=:creator", Mindmap.class);
final TypedQuery<Mindmap> query = entityManager.createQuery("from com.wisemapping.model.Mindmap wisemapping where title=:title and creator=:creator", Mindmap.class);
query.setParameter("title", title);
query.setParameter("creator", user);
@ -164,13 +147,13 @@ public class MindmapManagerImpl
@Override
public void saveMindmap(Mindmap mindMap) {
assert mindMap != null : "Save Mindmap: Mindmap is required!";
getSession().persist(mindMap);
entityManager.persist(mindMap);
}
@Override
public void updateMindmap(@NotNull Mindmap mindMap, boolean saveHistory) {
assert mindMap != null : "Save Mindmap: Mindmap is required!";
getSession().merge(mindMap);
entityManager.merge(mindMap);
if (saveHistory) {
saveHistory(mindMap);
}
@ -179,20 +162,19 @@ public class MindmapManagerImpl
@Override
public void removeMindmap(@NotNull final Mindmap mindmap) {
// Delete history first ...
final Session session = getSession();
final CriteriaBuilder cb = session.getCriteriaBuilder();
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaDelete<MindMapHistory> cr = cb.createCriteriaDelete(MindMapHistory.class);
final Root<MindMapHistory> root = cr.from(MindMapHistory.class);
final CriteriaDelete<MindMapHistory> deleteStatement = cr.where(cb.equal(root.get("mindmapId"), mindmap.getId()));
session.createMutationQuery(deleteStatement).executeUpdate();
entityManager.createQuery(deleteStatement).executeUpdate();
// Remove collaborations ...
mindmap.removedCollaboration(mindmap.getCollaborations());
// Delete mindmap ....
getSession().remove(mindmap);
entityManager.remove(mindmap);
}
private void saveHistory(@NotNull final Mindmap mindMap) {
@ -202,6 +184,6 @@ public class MindmapManagerImpl
history.setCreationTime(Calendar.getInstance());
history.setEditor(mindMap.getLastEditor());
history.setMindmapId(mindMap.getId());
getSession().merge(history);
entityManager.merge(history);
}
}

View File

@ -22,12 +22,7 @@ import com.wisemapping.model.*;
import com.wisemapping.security.DefaultPasswordEncoderFactories;
import com.wisemapping.security.LegacyPasswordEncoder;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.TypedQuery;
import org.hibernate.ObjectNotFoundException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.SelectionQuery;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
@ -41,9 +36,6 @@ import java.util.concurrent.CopyOnWriteArraySet;
@Repository
public class UserManagerImpl
implements UserManager {
@Autowired
private EntityManagerFactory entityManagerFactory;
@Autowired
private EntityManager entityManager;
@Autowired
@ -60,17 +52,12 @@ public class UserManagerImpl
return entityManager.createQuery("from com.wisemapping.model.User user", User.class).getResultList();
}
private Session getSession() {
return entityManagerFactory.unwrap(SessionFactory.class).getCurrentSession();
}
@Override
@Nullable
public User getUserBy(@NotNull final String email) {
User user = null;
TypedQuery<User> query = entityManager.createQuery("from com.wisemapping.model.User colaborator where email=:email",User.class);
TypedQuery<User> query = entityManager.createQuery("from com.wisemapping.model.User colaborator where email=:email", User.class);
query.setParameter("email", email);
final List<User> users = query.getResultList();
@ -85,8 +72,8 @@ public class UserManagerImpl
@Override
public Collaborator getCollaboratorBy(final String email) {
final Collaborator result;
Session session = getSession();
final SelectionQuery<Collaborator> query = session.createSelectionQuery("from com.wisemapping.model.Collaborator colaborator where " +
final TypedQuery<Collaborator> query = entityManager.createQuery("from com.wisemapping.model.Collaborator colaborator where " +
"email=:email", Collaborator.class);
query.setParameter("email", email);
@ -103,13 +90,7 @@ public class UserManagerImpl
@Nullable
@Override
public User getUserBy(int id) {
User user = null;
try {
user = getSession().get(User.class, id);
} catch (ObjectNotFoundException e) {
// Ignore ...
}
return user;
return entityManager.find(User.class, id);
}
@Override
@ -120,7 +101,7 @@ public class UserManagerImpl
} else {
user.setPassword("");
}
getSession().persist(user);
entityManager.persist(user);
}
@Override
@ -128,10 +109,9 @@ public class UserManagerImpl
assert user != null : "Trying to store a null user";
// Migrate from previous temporal collab to new user ...
final Session session = getSession();
collaborator.setEmail(collaborator.getEmail() + "_toRemove");
session.merge(collaborator);
session.flush();
entityManager.merge(collaborator);
entityManager.flush();
// Save all new...
this.createUser(user);
@ -143,18 +123,18 @@ public class UserManagerImpl
}
// Delete old user ...
session.remove(collaborator);
entityManager.remove(collaborator);
return user;
}
@Override
public void removeUser(@NotNull final User user) {
getSession().remove(user);
entityManager.remove(user);
}
public void auditLogin(@NotNull AccessAuditory accessAuditory) {
assert accessAuditory != null : "accessAuditory is null";
getSession().persist(accessAuditory);
entityManager.persist(accessAuditory);
}
public void updateUser(@NotNull User user) {
@ -166,13 +146,13 @@ public class UserManagerImpl
user.setPassword(passwordEncoder.encode(user.getPassword()));
}
getSession().merge(user);
entityManager.merge(user);
}
public User getUserByActivationCode(long code) {
final User user;
final SelectionQuery<User> query = getSession().createSelectionQuery("from com.wisemapping.model.User user where " +
final TypedQuery<User> query = entityManager.createQuery("from com.wisemapping.model.User user where " +
"activationCode=:activationCode", User.class);
query.setParameter("activationCode", code);