Change createSelectionQuery

This commit is contained in:
Paulo Gustavo Veiga 2023-10-29 22:15:35 -07:00
parent be20a85c19
commit 2bc9d41e2f
3 changed files with 26 additions and 22 deletions

View File

@ -22,7 +22,7 @@ import com.wisemapping.model.User;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
import org.hibernate.query.Query; 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.stereotype.Repository; import org.springframework.stereotype.Repository;
@ -51,9 +51,8 @@ public class LabelManagerImpl
@NotNull @NotNull
@Override @Override
@SuppressWarnings("unchecked")
public List<Label> getAllLabels(@NotNull final User user) { public List<Label> getAllLabels(@NotNull final User user) {
final Query query = getSession().createQuery("from com.wisemapping.model.Label wisemapping where creator=:creatorId"); final SelectionQuery<Label> query = getSession().createSelectionQuery("from com.wisemapping.model.Label wisemapping where creator=:creatorId", Label.class);
query.setParameter("creatorId", user); query.setParameter("creatorId", user);
return query.list(); return query.list();
} }
@ -61,16 +60,19 @@ public class LabelManagerImpl
@Nullable @Nullable
@Override @Override
public Label getLabelById(int id, @NotNull final User user) { public Label getLabelById(int id, @NotNull final User user) {
var query = getSession().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("id", id);
query.setParameter("creator", user); query.setParameter("creator", user);
return getFirst(query.list());
final List<Label> resultList = query.getResultList();
return getFirst(resultList);
} }
@Nullable @Nullable
@Override @Override
public Label getLabelByTitle(@NotNull String title, @NotNull final User user) { public Label getLabelByTitle(@NotNull String title, @NotNull final User user) {
var query = getSession().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("title", title);
query.setParameter("creator", user); query.setParameter("creator", user);
return getFirst(query.list()); return getFirst(query.list());
@ -82,7 +84,7 @@ public class LabelManagerImpl
} }
@Nullable @Nullable
private Label getFirst(List<Label> labels) { private Label getFirst(final List<Label> labels) {
Label result = null; Label result = null;
if (labels != null && !labels.isEmpty()) { if (labels != null && !labels.isEmpty()) {
result = labels.get(0); result = labels.get(0);

View File

@ -20,13 +20,13 @@ package com.wisemapping.dao;
import com.wisemapping.model.*; import com.wisemapping.model.*;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.persistence.Query;
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.Session;
import org.hibernate.SessionFactory; 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.stereotype.Repository; import org.springframework.stereotype.Repository;
@ -43,7 +43,7 @@ public class MindmapManagerImpl
@Override @Override
public Collaborator findCollaborator(@NotNull final String email) { public Collaborator findCollaborator(@NotNull final String email) {
final Collaborator collaborator; final Collaborator collaborator;
Query query = getSession().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); query.setParameter("email", email);
final List<Collaborator> collaborators = query.getResultList(); final List<Collaborator> collaborators = query.getResultList();
@ -93,8 +93,8 @@ public class MindmapManagerImpl
@Override @Override
public List<Mindmap> findMindmapByUser(@NotNull User user) { public List<Mindmap> findMindmapByUser(@NotNull User user) {
final Query query = getSession() final SelectionQuery<Mindmap> query = getSession()
.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 )"); .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()); query.setParameter("collabId", user.getId());
return query.getResultList(); return query.getResultList();
@ -102,7 +102,7 @@ public class MindmapManagerImpl
@Override @Override
public List<Collaboration> findCollaboration(final int collaboratorId) { public List<Collaboration> findCollaboration(final int collaboratorId) {
Query query = getSession().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); query.setParameter("collaboratorId", collaboratorId);
return query.getResultList(); return query.getResultList();
} }
@ -136,7 +136,7 @@ public class MindmapManagerImpl
@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;
Query query = getSession().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("title", title);
query.setParameter("creator", user); query.setParameter("creator", user);

View File

@ -26,6 +26,7 @@ import org.hibernate.ObjectNotFoundException;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
import org.hibernate.query.Query; import org.hibernate.query.Query;
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.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
@ -76,19 +77,20 @@ public class UserManagerImpl
@Override @Override
public Collaborator getCollaboratorBy(final String email) { public Collaborator getCollaboratorBy(final String email) {
final Collaborator cola; final Collaborator result;
Query query = getSession().createQuery("from com.wisemapping.model.Collaborator colaborator where " + Session session = getSession();
"email=:email"); final SelectionQuery<Collaborator> query = session.createSelectionQuery("from com.wisemapping.model.Collaborator colaborator where " +
"email=:email", Collaborator.class);
query.setParameter("email", email); query.setParameter("email", email);
final List<User> cols = query.list(); final List<Collaborator> cols = query.getResultList();
if (cols != null && !cols.isEmpty()) { if (cols != null && !cols.isEmpty()) {
assert cols.size() == 1 : "More than one colaborator with the same email!"; assert cols.size() == 1 : "More than one colaborator with the same email!";
cola = cols.get(0); result = cols.get(0);
} else { } else {
cola = null; result = null;
} }
return cola; return result;
} }
@Nullable @Nullable
@ -163,8 +165,8 @@ public class UserManagerImpl
public User getUserByActivationCode(long code) { public User getUserByActivationCode(long code) {
final User user; final User user;
var query = getSession().createQuery("from com.wisemapping.model.User user where " + final SelectionQuery<User> query = getSession().createSelectionQuery("from com.wisemapping.model.User user where " +
"activationCode=:activationCode"); "activationCode=:activationCode", User.class);
query.setParameter("activationCode", code); query.setParameter("activationCode", code);
final List users = query.list(); final List users = query.list();