Migrate to hibernate 6

main
Paulo Veiga 2023-10-30 05:34:45 +00:00
parent 881a16b908
commit 315267e68c
9 changed files with 131 additions and 206 deletions

View File

@ -13,10 +13,8 @@
</parent>
<properties>
<org.springframework.version>6.0.11</org.springframework.version>
<org.springframework.version>6.0.13</org.springframework.version>
<org.springframework.addons>6.1.2</org.springframework.addons>
<hibernate.version>6.2.6.Final</hibernate.version>
<hibernate-validator.version>6.0.21.Final</hibernate-validator.version>
<spring-security-taglibs.version>6.0.2</spring-security-taglibs.version>
</properties>
@ -86,8 +84,14 @@
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core-jakarta</artifactId>
<version>5.6.15.Final</version>
<artifactId>hibernate-core</artifactId>
<version>6.1.7.Final</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>4.0.1</version>
</dependency>
<!-- Hibernate Validator -->
@ -130,12 +134,12 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.6.1</version>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.15</version>
<version>6.0.6</version>
<scope>test</scope>
</dependency>
@ -189,16 +193,6 @@
<artifactId>jakarta.mail</artifactId>
<version>2.0.2</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.hibernate</groupId>-->
<!-- <artifactId>hibernate-ehcache</artifactId>-->
<!-- <version>5.6.15.Final</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>javax.cache</groupId>-->
<!-- <artifactId>cache-api</artifactId>-->
<!-- <version>1.1.1</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
@ -362,27 +356,6 @@
</profiles>
<build>
<plugins>
<!-- <plugin>-->
<!-- <groupId>org.hibernate.orm.tooling</groupId>-->
<!-- <artifactId>hibernate-enhance-maven-plugin</artifactId>-->
<!-- <version>${hibernate.version}</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <phase>compile</phase>-->
<!-- <configuration>-->
<!-- <failOnError>true</failOnError>-->
<!-- <enableLazyInitialization>true</enableLazyInitialization>-->
<!-- <enableDirtyTracking>true</enableDirtyTracking>-->
<!-- <enableAssociationManagement>true</enableAssociationManagement>-->
<!-- <enableExtendedEnhancement>false</enableExtendedEnhancement>-->
<!-- <enableExtendedEnhancement>false</enableExtendedEnhancement>-->
<!-- </configuration>-->
<!-- <goals>-->
<!-- <goal>enhance</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>

View File

@ -19,14 +19,21 @@ 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 org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import java.util.List;
public class LabelManagerImpl extends HibernateDaoSupport
@Repository
public class LabelManagerImpl
implements LabelManager {
@Resource
private SessionFactory sessionFactory;
@Override
public void addLabel(@NotNull final Label label) {
@ -35,31 +42,37 @@ public class LabelManagerImpl extends HibernateDaoSupport
@Override
public void saveLabel(@NotNull final Label label) {
currentSession().save(label);
getSession().persist(label);
}
private Session getSession() {
return sessionFactory.getCurrentSession();
}
@NotNull
@Override
@SuppressWarnings("unchecked")
public List<Label> getAllLabels(@NotNull final User user) {
var query = currentSession().createQuery("from com.wisemapping.model.Label wisemapping where creator_id=:creatorId");
query.setParameter("creatorId", user.getId());
final SelectionQuery<Label> query = getSession().createSelectionQuery("from com.wisemapping.model.Label wisemapping where creator=:creatorId", Label.class);
query.setParameter("creatorId", user);
return query.list();
}
@Nullable
@Override
public Label getLabelById(int id, @NotNull final User user) {
var query = currentSession().createQuery("from com.wisemapping.model.Label wisemapping where id=:id and creator=:creator");
final Session session = getSession();
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("creator", user);
return getFirst(query.list());
final List<Label> resultList = query.getResultList();
return getFirst(resultList);
}
@Nullable
@Override
public Label getLabelByTitle(@NotNull String title, @NotNull final User user) {
var query = currentSession().createQuery("from com.wisemapping.model.Label wisemapping where title=:title and creator=:creator");
final SelectionQuery<Label> query = getSession().createSelectionQuery("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());
@ -67,10 +80,11 @@ public class LabelManagerImpl extends HibernateDaoSupport
@Override
public void removeLabel(@NotNull Label label) {
getHibernateTemplate().delete(label);
getSession().remove(label);
}
@Nullable private Label getFirst(List<Label> labels) {
@Nullable
private Label getFirst(final List<Label> labels) {
Label result = null;
if (labels != null && !labels.isEmpty()) {
result = labels.get(0);

View File

@ -50,17 +50,11 @@ public interface MindmapManager {
void removeCollaboration(Collaboration collaboration);
List<Mindmap> search(MindMapCriteria criteria);
List<Mindmap> search(MindMapCriteria criteria, int maxResult);
List<MindMapHistory> getHistoryFrom(int mindmapId);
MindMapHistory getHistory(int historyId);
void updateCollaboration(@NotNull Collaboration collaboration);
void purgeHistory(int mapId) throws IOException;
List<Mindmap> findMindmapByUser(User user);
}

View File

@ -19,30 +19,31 @@
package com.wisemapping.dao;
import com.wisemapping.model.*;
import com.wisemapping.util.ZipUtils;
import jakarta.persistence.Query;
import org.hibernate.Criteria;
import org.hibernate.criterion.Junction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
import jakarta.annotation.Resource;
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.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
@Repository
public class MindmapManagerImpl
extends HibernateDaoSupport
implements MindmapManager {
@Resource
private SessionFactory sessionFactory;
@Override
public Collaborator findCollaborator(@NotNull final String email) {
final Collaborator collaborator;
Query query = currentSession().createQuery("from com.wisemapping.model.Collaborator collaborator where email=:email");
final SelectionQuery<Collaborator> query = getSession().createSelectionQuery("from com.wisemapping.model.Collaborator collaborator where email=:email", Collaborator.class);
query.setParameter("email", email);
final List<Collaborator> collaborators = query.getResultList();
@ -55,137 +56,87 @@ public class MindmapManagerImpl
return collaborator;
}
@Override
public List<Mindmap> search(MindMapCriteria criteria) {
return search(criteria, -1);
private Session getSession() {
return sessionFactory.getCurrentSession();
}
@Override
public List<MindMapHistory> getHistoryFrom(int mindmapId) {
final Criteria hibernateCriteria = currentSession().createCriteria(MindMapHistory.class);
hibernateCriteria.add(Restrictions.eq("mindmapId", mindmapId));
hibernateCriteria.addOrder(Order.desc("creationTime"));
final Session session = getSession();
final CriteriaBuilder cb = session.getCriteriaBuilder();
// This line throws errors in some environments, so getting all history and taking firsts 10 records
hibernateCriteria.setMaxResults(30);
return hibernateCriteria.list();
final CriteriaQuery<MindMapHistory> cr = cb.createQuery(MindMapHistory.class);
final Root<MindMapHistory> root = cr.from(MindMapHistory.class);
final CriteriaQuery<MindMapHistory> select = cr.select(root)
.where(cb.equal(root.get("mindmapId"), mindmapId))
.orderBy(cb.desc(root.get("creationTime")));
return session.
createQuery(select)
.setMaxResults(30)
.getResultList();
}
@Override
public MindMapHistory getHistory(int historyId) {
return getHibernateTemplate().get(MindMapHistory.class, historyId);
final Session session = getSession();
return session.find(MindMapHistory.class, historyId);
}
@Override
public void updateCollaboration(@NotNull Collaboration collaboration) {
getHibernateTemplate().save(collaboration);
}
@Override
public void purgeHistory(int mapId) throws IOException {
final Criteria hibernateCriteria = currentSession().createCriteria(MindMapHistory.class);
hibernateCriteria.add(Restrictions.eq("mindmapId", mapId));
hibernateCriteria.addOrder(Order.desc("creationTime"));
final List<MindMapHistory> historyList = hibernateCriteria.list();
final Mindmap mindmap = this.getMindmapById(mapId);
if (mindmap != null) {
final Calendar yearAgo = Calendar.getInstance();
yearAgo.add(Calendar.MONTH, -12);
// If the map has not been modified in the last months, it means that I don't need to keep all the history ...
int max = mindmap.getLastModificationTime().before(yearAgo) ? 10 : 25;
final HibernateTemplate hibernateTemplate = getHibernateTemplate();
for (MindMapHistory history : historyList) {
byte[] zippedXml = history.getZippedXml();
if (new String(zippedXml).startsWith("<map")) {
history.setZippedXml(ZipUtils.bytesToZip(zippedXml));
hibernateTemplate.update(history);
}
}
if (historyList.size() > max) {
for (int i = max; i < historyList.size(); i++) {
hibernateTemplate.delete(historyList.get(i));
}
}
}
final Session session = getSession();
session.persist(collaboration);
}
@Override
public List<Mindmap> findMindmapByUser(@NotNull User user) {
final Mindmap collaborator;
final Query query = currentSession()
.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 )");
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);
query.setParameter("collabId", user.getId());
return query.getResultList();
}
@Override
public List<Mindmap> search(MindMapCriteria criteria, int maxResult) {
final Criteria hibernateCriteria = currentSession().createCriteria(Mindmap.class);
//always search public maps
hibernateCriteria.add(Restrictions.like("public", Boolean.TRUE));
if (criteria != null) {
final Junction junction;
if (criteria.isOrCriteria()) {
junction = Restrictions.disjunction();
} else {
junction = Restrictions.conjunction();
}
if (criteria.getTitle() != null && criteria.getTitle().length() > 0) {
final SimpleExpression titleRestriction = Restrictions.like("title", "%" + criteria.getTitle() + "%");
junction.add(titleRestriction);
}
if (criteria.getDescription() != null && criteria.getDescription().length() > 0) {
final SimpleExpression descriptionRestriction = Restrictions.like("description", "%" + criteria.getDescription() + "%");
junction.add(descriptionRestriction);
}
hibernateCriteria.add(junction);
}
return hibernateCriteria.list();
}
@Override
public List<Collaboration> findCollaboration(final int collaboratorId) {
Query query = currentSession().createQuery("from com.wisemapping.model.Collaboration c where c.collaborator.id=:collaboratorId");
final SelectionQuery<Collaboration> query = getSession().createSelectionQuery("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!";
getHibernateTemplate().save(collaborator);
session.persist(collaborator);
}
@Override
public void removeCollaboration(Collaboration collaboration) {
getHibernateTemplate().delete(collaboration);
final Session session = getSession();
session.remove(collaboration);
}
@Override
public void removeCollaborator(@NotNull Collaborator collaborator) {
getHibernateTemplate().delete(collaborator);
final Session session = getSession();
session.remove(collaborator);
}
@Override
@Nullable
public Mindmap getMindmapById(int id) {
return getHibernateTemplate().get(Mindmap.class, id);
final Session session = getSession();
return session.get(Mindmap.class, id);
}
@Override
public Mindmap getMindmapByTitle(final String title, final User user) {
final Mindmap result;
Query query = currentSession().createQuery("from com.wisemapping.model.Mindmap wisemapping where title=:title and creator=:creator");
final SelectionQuery<Mindmap> query = getSession().createSelectionQuery("from com.wisemapping.model.Mindmap wisemapping where title=:title and creator=:creator", Mindmap.class);
query.setParameter("title", title);
query.setParameter("creator", user);
@ -207,13 +158,13 @@ public class MindmapManagerImpl
@Override
public void saveMindmap(Mindmap mindMap) {
assert mindMap != null : "Save Mindmap: Mindmap is required!";
currentSession().save(mindMap);
getSession().persist(mindMap);
}
@Override
public void updateMindmap(@NotNull Mindmap mindMap, boolean saveHistory) {
assert mindMap != null : "Save Mindmap: Mindmap is required!";
getHibernateTemplate().saveOrUpdate(mindMap);
getSession().merge(mindMap);
if (saveHistory) {
saveHistory(mindMap);
}
@ -222,16 +173,20 @@ public class MindmapManagerImpl
@Override
public void removeMindmap(@NotNull final Mindmap mindmap) {
// Delete history first ...
final Criteria hibernateCriteria = currentSession().createCriteria(MindMapHistory.class);
hibernateCriteria.add(Restrictions.eq("mindmapId", mindmap.getId()));
final List list = hibernateCriteria.list();
getHibernateTemplate().deleteAll(list);
final Session session = getSession();
final CriteriaBuilder cb = session.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();
// Remove collaborations ...
mindmap.removedCollaboration(mindmap.getCollaborations());
// Delete mindmap ....
getHibernateTemplate().delete(mindmap);
getSession().remove(mindmap);
}
private void saveHistory(@NotNull final Mindmap mindMap) {
@ -241,6 +196,6 @@ public class MindmapManagerImpl
history.setCreationTime(Calendar.getInstance());
history.setEditor(mindMap.getLastEditor());
history.setMindmapId(mindMap.getId());
getHibernateTemplate().saveOrUpdate(history);
getSession().merge(history);
}
}

View File

@ -18,27 +18,28 @@
package com.wisemapping.dao;
import com.wisemapping.model.AccessAuditory;
import com.wisemapping.model.AuthenticationType;
import com.wisemapping.model.Collaboration;
import com.wisemapping.model.Collaborator;
import com.wisemapping.model.User;
import com.wisemapping.model.*;
import com.wisemapping.security.DefaultPasswordEncoderFactories;
import com.wisemapping.security.LegacyPasswordEncoder;
import jakarta.annotation.Resource;
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.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
@Repository
public class UserManagerImpl
extends HibernateDaoSupport
implements UserManager {
@Resource
private SessionFactory sessionFactory;
private PasswordEncoder passwordEncoder;
@ -46,9 +47,12 @@ public class UserManagerImpl
this.passwordEncoder = passwordEncoder;
}
@SuppressWarnings("unchecked")
public List<User> getAllUsers() {
return currentSession().createQuery("from com.wisemapping.model.User user").list();
return getSession().createSelectionQuery("from com.wisemapping.model.User user", User.class).getResultList();
}
private Session getSession() {
return sessionFactory.getCurrentSession();
}
@ -57,10 +61,10 @@ public class UserManagerImpl
public User getUserBy(@NotNull final String email) {
User user = null;
var query = currentSession().createQuery("from com.wisemapping.model.User colaborator where email=:email");
SelectionQuery<User> query = getSession().createSelectionQuery("from com.wisemapping.model.User colaborator where email=:email",User.class);
query.setParameter("email", email);
final List<User> users = query.list();
final List<User> users = query.getResultList();
if (users != null && !users.isEmpty()) {
assert users.size() == 1 : "More than one user with the same email!";
user = users.get(0);
@ -71,19 +75,20 @@ public class UserManagerImpl
@Override
public Collaborator getCollaboratorBy(final String email) {
final Collaborator cola;
var query = currentSession().createQuery("from com.wisemapping.model.Collaborator colaborator where " +
"email=:email");
final Collaborator result;
Session session = getSession();
final SelectionQuery<Collaborator> query = session.createSelectionQuery("from com.wisemapping.model.Collaborator colaborator where " +
"email=:email", Collaborator.class);
query.setParameter("email", email);
final List<User> cols = query.list();
final List<Collaborator> cols = query.getResultList();
if (cols != null && !cols.isEmpty()) {
assert cols.size() == 1 : "More than one colaborator with the same email!";
cola = cols.get(0);
result = cols.get(0);
} else {
cola = null;
result = null;
}
return cola;
return result;
}
@Nullable
@ -91,7 +96,7 @@ public class UserManagerImpl
public User getUserBy(int id) {
User user = null;
try {
user = getHibernateTemplate().get(User.class, id);
user = getSession().get(User.class, id);
} catch (ObjectNotFoundException e) {
// Ignore ...
}
@ -106,7 +111,7 @@ public class UserManagerImpl
} else {
user.setPassword("");
}
getHibernateTemplate().saveOrUpdate(user);
getSession().persist(user);
}
@Override
@ -114,10 +119,10 @@ public class UserManagerImpl
assert user != null : "Trying to store a null user";
// Migrate from previous temporal collab to new user ...
final HibernateTemplate template = getHibernateTemplate();
final Session session = getSession();
collaborator.setEmail(collaborator.getEmail() + "_toRemove");
template.saveOrUpdate(collaborator);
template.flush();
session.merge(collaborator);
session.flush();
// Save all new...
this.createUser(user);
@ -129,18 +134,18 @@ public class UserManagerImpl
}
// Delete old user ...
template.delete(collaborator);
session.remove(collaborator);
return user;
}
@Override
public void removeUser(@NotNull final User user) {
getHibernateTemplate().delete(user);
getSession().remove(user);
}
public void auditLogin(@NotNull AccessAuditory accessAuditory) {
assert accessAuditory != null : "accessAuditory is null";
getHibernateTemplate().save(accessAuditory);
getSession().persist(accessAuditory);
}
public void updateUser(@NotNull User user) {
@ -152,21 +157,21 @@ public class UserManagerImpl
user.setPassword(passwordEncoder.encode(user.getPassword()));
}
getHibernateTemplate().update(user);
getSession().merge(user);
}
public User getUserByActivationCode(long code) {
final User user;
var query = currentSession().createQuery("from com.wisemapping.model.User user where " +
"activationCode=:activationCode");
final SelectionQuery<User> query = getSession().createSelectionQuery("from com.wisemapping.model.User user where " +
"activationCode=:activationCode", User.class);
query.setParameter("activationCode", code);
final List users = query.list();
final List<User> users = query.getResultList();
if (users != null && !users.isEmpty()) {
assert users.size() == 1 : "More than one user with the same username!";
user = (User) users.get(0);
user = users.get(0);
} else {
user = null;
}

View File

@ -49,8 +49,6 @@ public interface MindmapService {
void removeMindmap(@NotNull final Mindmap mindmap, @NotNull final User user) throws WiseMappingException;
List<Mindmap> search(MindMapCriteria criteria);
List<MindMapHistory> findMindmapHistory(int mindmapId);
boolean hasPermissions(@Nullable User user, Mindmap map, CollaborationRole allowedRole);

View File

@ -134,11 +134,6 @@ public class MindmapServiceImpl
mindmapManager.updateMindmap(mindMap, saveHistory);
}
@Override
public List<Mindmap> search(MindMapCriteria criteria) {
return mindmapManager.search(criteria);
}
@Override
public void removeCollaboration(@NotNull Mindmap mindmap, @NotNull Collaboration collaboration) throws CollaborationException {
// remove collaborator association

View File

@ -36,10 +36,6 @@
</bean>
<!-- Hibernate Template Definition -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="mindmapSessionFactory"/>
</bean>
<bean id="txAttributes"
class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">

View File

@ -6,17 +6,12 @@
http://www.springframework.org/schema/beans/spring-beans.xsd">
<beans>
<bean id="userManager" class="com.wisemapping.dao.UserManagerImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
<property name="encoder" ref="passwordEncoder"/>
</bean>
<bean id="mindmapManager" class="com.wisemapping.dao.MindmapManagerImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>
<bean id="mindmapManager" class="com.wisemapping.dao.MindmapManagerImpl"/>
<bean id="labelManager" class="com.wisemapping.dao.LabelManagerImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>
<bean id="labelManager" class="com.wisemapping.dao.LabelManagerImpl"/>
</beans>
</beans>