Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAK-47604 GB Adding or removing a TA's permissions causes duplicate values #11712

Merged
merged 5 commits into from
Jul 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
Expand All @@ -28,14 +29,12 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import org.sakaiproject.springframework.data.PersistableEntity;
import org.sakaiproject.springframework.data.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
@Transactional(readOnly = true)
public abstract class SpringCrudRepositoryImpl<T extends PersistableEntity<ID>, ID extends Serializable> implements SpringCrudRepository<T, ID> {

Expand Down Expand Up @@ -89,14 +88,14 @@ public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
public Optional<T> findById(ID id) {

Assert.notNull(id, "The id cannot be null");
return Optional.ofNullable((T) sessionFactory.getCurrentSession().get(domainClass, id));
return Optional.ofNullable(sessionFactory.getCurrentSession().get(domainClass, id));
}

@Override
public T getById(ID id) {

Assert.notNull(id, "The id cannot be null");
return (T) sessionFactory.getCurrentSession().load(domainClass, id);
return sessionFactory.getCurrentSession().load(domainClass, id);
}

@Override
Expand All @@ -116,7 +115,7 @@ public Page<T> findAll(Pageable pageable) {

Criteria criteria = sessionFactory.getCurrentSession().createCriteria(domainClass);
criteria.setFirstResult((int) pageable.getOffset());
criteria.setMaxResults((int) pageable.getPageSize());
criteria.setMaxResults(pageable.getPageSize());
return new PageImpl(criteria.list());
}

Expand All @@ -125,21 +124,18 @@ public Iterable<T> findAllById(Iterable<ID> ids) {

List<T> list = new ArrayList<>();
if (ids != null) {
ids.forEach(id -> findById(id).ifPresent(found -> list.add(found)));
ids.forEach(id -> findById(id).ifPresent(list::add));
}
return list;
}

@Override
@Transactional
public void delete(T entity) {

Session session = sessionFactory.getCurrentSession();

try {
session.delete(entity);
} catch (Exception he) {
session.delete(session.merge(entity));
if (entity != null) {
deleteById(entity.getId());
} else {
log.warn("Can not perform delete on a null entity");
}
}

Expand All @@ -161,7 +157,12 @@ public void deleteAll(Iterable<? extends T> entities) {
@Override
@Transactional
public void deleteById(ID id) {
findById(id).ifPresent(found -> delete(found));
if (id != null) {
Session session = sessionFactory.getCurrentSession();
findById(id).ifPresent(session::delete);
} else {
log.warn("Can not perform delete with a null id");
}
}

/**
Expand Down
Loading