Compare commits

...

2 Commits

Author SHA1 Message Date
Paulo Gustavo Veiga
2f32ef66e9 Migrate all to entity manager. 2024-01-13 18:49:49 -08:00
Paulo Gustavo Veiga
fbbc95fd59 Fix JPA load. 2024-01-13 18:31:21 -08:00
6 changed files with 72 additions and 100 deletions

View File

@ -20,10 +20,13 @@ public class Application {
public static void main(String[] args) { public static void main(String[] args) {
new SpringApplicationBuilder() new SpringApplicationBuilder()
.parent(Application.class, MethodSecurityConfig.class).web(WebApplicationType.NONE) .parent(Application.class, MethodSecurityConfig.class, HibernateConfig.class).web(WebApplicationType.NONE)
.child(MvcAppConfig.class, MvcSecurityConfig.class, HibernateConfig.class, ServletConfig.class).web(WebApplicationType.SERVLET) .child(MvcAppConfig.class, MvcSecurityConfig.class, ServletConfig.class).web(WebApplicationType.SERVLET)
// .sibling(RestAppConfig.class).web(WebApplicationType.SERVLET) .sibling(RestAppConfig.class).web(WebApplicationType.SERVLET)
.run(args); .run(args);
// new SpringApplicationBuilder(Application.class, MethodSecurityConfig.class,MvcAppConfig.class, MvcSecurityConfig.class, HibernateConfig.class, ServletConfig.class).web(WebApplicationType.SERVLET).run(args);
} }
@Bean @Bean

View File

@ -1,12 +1,16 @@
package com.wisemapping.config; 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.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration @Configuration
@EnableTransactionManagement @EnableAutoConfiguration
@EnableJpaRepositories("com.wisemapping.model") @EnableJpaRepositories(basePackages={"com.wisemapping.dao"})
@EntityScan(basePackageClasses= User.class)
public class HibernateConfig { public class HibernateConfig {
// @Bean // @Bean

View File

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

View File

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

View File

@ -21,12 +21,8 @@ package com.wisemapping.dao;
import com.wisemapping.model.*; import com.wisemapping.model.*;
import com.wisemapping.security.DefaultPasswordEncoderFactories; import com.wisemapping.security.DefaultPasswordEncoderFactories;
import com.wisemapping.security.LegacyPasswordEncoder; import com.wisemapping.security.LegacyPasswordEncoder;
import jakarta.annotation.Resource; 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.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -41,30 +37,27 @@ import java.util.concurrent.CopyOnWriteArraySet;
public class UserManagerImpl public class UserManagerImpl
implements UserManager { implements UserManager {
@Autowired @Autowired
private EntityManagerFactory entityManagerFactory; private EntityManager entityManager;
@Autowired @Autowired
private PasswordEncoder passwordEncoder; private PasswordEncoder passwordEncoder;
public UserManagerImpl() {
}
public void setEncoder(PasswordEncoder passwordEncoder) { public void setEncoder(PasswordEncoder passwordEncoder) {
this.passwordEncoder = passwordEncoder; this.passwordEncoder = passwordEncoder;
} }
public List<User> getAllUsers() { public List<User> getAllUsers() {
return getSession().createSelectionQuery("from com.wisemapping.model.User user", User.class).getResultList(); return entityManager.createQuery("from com.wisemapping.model.User user", User.class).getResultList();
} }
private Session getSession() {
return entityManagerFactory.unwrap(SessionFactory.class).getCurrentSession();
}
@Override @Override
@Nullable @Nullable
public User getUserBy(@NotNull final String email) { public User getUserBy(@NotNull final String email) {
User user = null; User user = null;
SelectionQuery<User> query = getSession().createSelectionQuery("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); query.setParameter("email", email);
final List<User> users = query.getResultList(); final List<User> users = query.getResultList();
@ -79,8 +72,8 @@ public class UserManagerImpl
@Override @Override
public Collaborator getCollaboratorBy(final String email) { public Collaborator getCollaboratorBy(final String email) {
final Collaborator result; 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); "email=:email", Collaborator.class);
query.setParameter("email", email); query.setParameter("email", email);
@ -97,13 +90,7 @@ public class UserManagerImpl
@Nullable @Nullable
@Override @Override
public User getUserBy(int id) { public User getUserBy(int id) {
User user = null; return entityManager.find(User.class, id);
try {
user = getSession().get(User.class, id);
} catch (ObjectNotFoundException e) {
// Ignore ...
}
return user;
} }
@Override @Override
@ -114,7 +101,7 @@ public class UserManagerImpl
} else { } else {
user.setPassword(""); user.setPassword("");
} }
getSession().persist(user); entityManager.persist(user);
} }
@Override @Override
@ -122,10 +109,9 @@ public class UserManagerImpl
assert user != null : "Trying to store a null user"; assert user != null : "Trying to store a null user";
// Migrate from previous temporal collab to new user ... // Migrate from previous temporal collab to new user ...
final Session session = getSession();
collaborator.setEmail(collaborator.getEmail() + "_toRemove"); collaborator.setEmail(collaborator.getEmail() + "_toRemove");
session.merge(collaborator); entityManager.merge(collaborator);
session.flush(); entityManager.flush();
// Save all new... // Save all new...
this.createUser(user); this.createUser(user);
@ -137,18 +123,18 @@ public class UserManagerImpl
} }
// Delete old user ... // Delete old user ...
session.remove(collaborator); entityManager.remove(collaborator);
return user; return user;
} }
@Override @Override
public void removeUser(@NotNull final User user) { public void removeUser(@NotNull final User user) {
getSession().remove(user); entityManager.remove(user);
} }
public void auditLogin(@NotNull AccessAuditory accessAuditory) { public void auditLogin(@NotNull AccessAuditory accessAuditory) {
assert accessAuditory != null : "accessAuditory is null"; assert accessAuditory != null : "accessAuditory is null";
getSession().persist(accessAuditory); entityManager.persist(accessAuditory);
} }
public void updateUser(@NotNull User user) { public void updateUser(@NotNull User user) {
@ -160,13 +146,13 @@ public class UserManagerImpl
user.setPassword(passwordEncoder.encode(user.getPassword())); user.setPassword(passwordEncoder.encode(user.getPassword()));
} }
getSession().merge(user); entityManager.merge(user);
} }
public User getUserByActivationCode(long code) { public User getUserByActivationCode(long code) {
final User user; 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); "activationCode=:activationCode", User.class);
query.setParameter("activationCode", code); query.setParameter("activationCode", code);

View File

@ -3,15 +3,19 @@ Springboot Common
################################################################################## ##################################################################################
spring.datasource.initialize=true spring.datasource.initialize=true
spring.jpa.open-in-view=true
#spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
spring.sql.init.mode=always
spring.main.allow-circular-references=true spring.main.allow-circular-references=true
spring.jpa.open-in-view=true
spring.jpa.properties.hibernate.current_session_context_class=thread spring.jpa.properties.hibernate.current_session_context_class=thread
spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.format_sql=true
spring.sql.init.mode=always
logging.level.root=TRACE ##################################################################################
Log Level
##################################################################################
logging.level.root=INFO
logging.level.org.apache.tomcat=INFO
################################################################################## ##################################################################################
# Mail configuration. Must be configured to enable user registration confirmation. # Mail configuration. Must be configured to enable user registration confirmation.