Commit a7307d6f authored by Matija Obreza's avatar Matija Obreza
Browse files

CRUDService: do not lazyLoad before calling `update(updated, target)`

parent e2393077
......@@ -35,11 +35,9 @@ public class AccessionInvNameServiceImpl extends CRUDServiceImpl<AccessionInvNam
@Transactional
// FIXME @PreAuthorize
public AccessionInvName create(AccessionInvName source) {
assert(source.getId() == null);
LOG.debug("Create AccessionInvName. Input data {}", source);
AccessionInvName invName = new AccessionInvName();
invName.apply(source);
AccessionInvName saved = repository.save(invName);
AccessionInvName saved = repository.save(source);
return _lazyLoad(saved);
}
......
......@@ -147,14 +147,9 @@ public class AccessionServiceImpl extends FilteredCRUDServiceImpl<Accession, Acc
@Override
@Transactional
public AccessionAction create(AccessionAction source) {
LOG.debug("Create AccessionAction. Input data {}", source);
AccessionAction entity = new AccessionAction();
entity.apply(source);
AccessionAction saved = get(repository.save(entity));
saved.lazyLoad();
return saved;
assert(source.getId() == null);
AccessionAction saved = repository.save(source);
return _lazyLoad(saved);
}
@Override
......
......@@ -26,7 +26,6 @@ import org.gringlobal.api.InvalidApiUsageException;
import org.gringlobal.api.NotFoundElement;
import org.gringlobal.model.LazyLoading;
import org.gringlobal.service.CRUDService;
import org.hibernate.Hibernate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
......@@ -141,11 +140,10 @@ public abstract class CRUDServiceImpl<T extends EmptyModel, R extends JpaReposit
public abstract T update(T updated, T target);
@Transactional
@SuppressWarnings("unchecked")
public T update(T updated) {
T target = reload(updated);
T target = get(updated);
// entityManager.detach(target);
return (T) Hibernate.unproxy(update(updated, target));
return update(updated, target);
}
/**
......
......@@ -47,6 +47,7 @@ import org.gringlobal.model.CodeValueLang;
import org.gringlobal.model.Cooperator;
import org.gringlobal.model.Inventory;
import org.gringlobal.model.InventoryMaintenancePolicy;
import org.gringlobal.model.NameGroup;
import org.gringlobal.model.QAccession;
import org.gringlobal.model.QAccessionInvAnnotation;
import org.gringlobal.model.QInventory;
......@@ -60,11 +61,13 @@ import org.gringlobal.persistence.AccessionInvGroupRepository;
import org.gringlobal.persistence.AccessionSourceMapRepository;
import org.gringlobal.persistence.CodeValueLangRepository;
import org.gringlobal.persistence.CodeValueRepository;
import org.gringlobal.persistence.NameGroupRepository;
import org.gringlobal.service.AccessionInvGroupService;
import org.gringlobal.service.AccessionService;
import org.gringlobal.service.AccessionService.AcquisitionData;
import org.gringlobal.service.CodeValueService;
import org.gringlobal.service.LanguageService;
import org.gringlobal.service.NameGroupService;
import org.gringlobal.service.filter.AccessionFilter;
import org.gringlobal.service.filter.SiteFilter;
import org.junit.After;
......@@ -113,6 +116,11 @@ public class AccessionServiceTest extends AbstractServicesTest {
@Autowired
private AccessionSourceMapRepository accessionSourceMapRepository;
@Autowired
private NameGroupService nameGroupService;
@Autowired
private NameGroupRepository nameGroupRepository;
@After
@Transactional
public void cleanup() throws Exception {
......@@ -134,6 +142,7 @@ public class AccessionServiceTest extends AbstractServicesTest {
taxonomyFamilyRepository.deleteAllInBatch();
geographyRepository.deleteAllInBatch();
cvlRepository.deleteAllInBatch();
nameGroupRepository.deleteAllInBatch();
super.cleanup();
}
......@@ -188,6 +197,7 @@ public class AccessionServiceTest extends AbstractServicesTest {
// insert new accession to the DB
Accession accession = addAccessionToDB();
assertThat(accessionInvNameRepository.count(), is(1L));
// change accession number
accession.setAccessionNumberPart3("part3");
......@@ -197,6 +207,20 @@ public class AccessionServiceTest extends AbstractServicesTest {
List<AccessionInvName> allNames = accessionInvNameRepository.findAll();
assertThat(allNames.stream().anyMatch(a -> a.getPlantName().equals(accession.getAccessionNumber())), is(true));
assertThat(allNames.stream().anyMatch(a -> a.getPlantName().equals(updated.getAccessionNumber())), is(true));
// assert that name is registered with name group
var nameGroup = new NameGroup();
nameGroup.setGroupName("TMe");
var savedNameGroup = nameGroupService.create(nameGroup);
updated.setAccessionNumberPart1("TMe");
updated.setAccessionNumberPart2(-1l);
var newUpdated = accessionService.update(accession);
assertThat(accessionInvNameRepository.count(), is(3L));
allNames = accessionInvNameRepository.findAll();
// A name with NameGroup exists!
assertThat(allNames.stream().anyMatch(a -> a.getPlantName().equals(newUpdated.getAccessionNumber()) && a.getNameGroup() != null && savedNameGroup.getId().equals(a.getNameGroup().getId())), is(true));
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment