From 9fde458a5c3efe083710eef0815e3964ba873f88 Mon Sep 17 00:00:00 2001 From: ToineSiebelink Date: Wed, 17 May 2023 16:40:44 +0100 Subject: Entity ID types do not match types in database definition As Liquibase is used, JPA does not manage database definition. Within Liquibase changelog, only Anchor and Fragment have BIGINT (Long) IDs. Other entities have INTEGER IDs. - Change AnchorEntity/AnchorRepository ID type to Long - Change YangResourceEntity/YangResourceRepository and ModuleReferenceRepository ID to Integer - Modify datatypes of foreign key references in liquibase changelog to use correct types for dataspace_id, schema_set_id, yang_resource_id - Update test liquibase changelog Issue-ID: CPS-1676 Signed-off-by: danielhanrahan Change-Id: I26de6712b2e74d166fc5a48b1f8149de6a1e5199 Signed-off-by: ToineSiebelink --- .../org/onap/cps/spi/entities/AnchorEntity.java | 2 +- .../cps/spi/entities/FragmentEntityArranger.java | 2 +- .../org/onap/cps/spi/entities/FragmentExtract.java | 2 +- .../onap/cps/spi/entities/YangResourceEntity.java | 4 ++-- .../spi/impl/CpsDataPersistenceServiceImpl.java | 4 ++-- .../spi/impl/CpsModulePersistenceServiceImpl.java | 2 +- .../onap/cps/spi/repository/AnchorRepository.java | 2 +- .../cps/spi/repository/FragmentRepository.java | 22 +++++++++++----------- .../spi/repository/ModuleReferenceRepository.java | 4 ++-- .../SchemaSetYangResourceRepository.java | 4 ++-- .../SchemaSetYangResourceRepositoryImpl.java | 8 ++++---- .../repository/YangResourceNativeRepository.java | 4 ++-- .../YangResourceNativeRepositoryImpl.java | 8 ++++---- .../cps/spi/repository/YangResourceRepository.java | 2 +- .../org/onap/cps/spi/utils/SessionManager.java | 2 +- 15 files changed, 36 insertions(+), 36 deletions(-) (limited to 'cps-ri/src/main/java') diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/AnchorEntity.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/AnchorEntity.java index b59150f2e4..403741a2d6 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/entities/AnchorEntity.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/AnchorEntity.java @@ -55,7 +55,7 @@ public class AnchorEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Integer id; + private Long id; @NotNull @Column diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntityArranger.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntityArranger.java index c1b1322bb5..697eb8de00 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntityArranger.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntityArranger.java @@ -58,7 +58,7 @@ public class FragmentEntityArranger { * @return a collection of FragmentEntities (trees) with descendants. */ public static Collection toFragmentEntityTreesAcrossAnchors( - final Map anchorEntityPerId, final Collection fragmentExtracts) { + final Map anchorEntityPerId, final Collection fragmentExtracts) { final Map fragmentEntityPerId = new HashMap<>(); for (final FragmentExtract fragmentExtract : fragmentExtracts) { final AnchorEntity anchorEntity = anchorEntityPerId.get(fragmentExtract.getAnchorId()); diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentExtract.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentExtract.java index 3aa19e670b..50be3c7b7a 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentExtract.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentExtract.java @@ -24,7 +24,7 @@ public interface FragmentExtract { Long getId(); - Integer getAnchorId(); + Long getAnchorId(); String getXpath(); diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResourceEntity.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResourceEntity.java index 53ae820262..71d97a3ed2 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResourceEntity.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResourceEntity.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2020 Pantheon.tech - * Modifications Copyright (C) 2021-2022 Nordix Foundation + * Modifications Copyright (C) 2021-2023 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,7 +48,7 @@ public class YangResourceEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + private Integer id; @NotNull @Column diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java index 890941bf33..fdb01485ad 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java @@ -346,10 +346,10 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService fragmentRepository.findExtractsWithDescendantsByIds(fragmentEntityIds, fetchDescendantsOption.getDepth()); if (anchorEntity == ALL_ANCHORS) { - final Collection anchorIds = fragmentExtracts.stream() + final Collection anchorIds = fragmentExtracts.stream() .map(FragmentExtract::getAnchorId).collect(Collectors.toSet()); final List anchorEntities = anchorRepository.findAllById(anchorIds); - final Map anchorEntityPerId = anchorEntities.stream() + final Map anchorEntityPerId = anchorEntities.stream() .collect(Collectors.toMap(AnchorEntity::getId, Function.identity())); return FragmentEntityArranger.toFragmentEntityTreesAcrossAnchors(anchorEntityPerId, fragmentExtracts); } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java index 5eda15a3d7..e149a1f149 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java @@ -172,7 +172,7 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final SchemaSetEntity schemaSetEntity = schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName); - final List allYangResourceIds = + final List allYangResourceIds = yangResourceRepository.getResourceIdsByModuleReferences(allModuleReferences); yangResourceRepository.insertSchemaSetIdYangResourceId(schemaSetEntity.getId(), allYangResourceIds); } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java index fe9ff9e2f0..5bb5857810 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java @@ -33,7 +33,7 @@ import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @Repository -public interface AnchorRepository extends JpaRepository { +public interface AnchorRepository extends JpaRepository { Optional findByDataspaceAndName(DataspaceEntity dataspaceEntity, String name); diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java index 2691be9844..425a7267d9 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java @@ -47,7 +47,7 @@ public interface FragmentRepository extends JpaRepository, new DataNodeNotFoundException(anchorEntity.getDataspace().getName(), anchorEntity.getName(), xpath)); } - List findByAnchorIdAndXpathIn(int anchorId, String[] xpaths); + List findByAnchorIdAndXpathIn(long anchorId, String[] xpaths); default List findByAnchorAndXpathIn(final AnchorEntity anchorEntity, final Collection xpaths) { @@ -64,41 +64,41 @@ public interface FragmentRepository extends JpaRepository, return findByDataspaceIdAndXpathIn(dataspaceEntity.getId(), xpaths.toArray(new String[0])); } - boolean existsByAnchorId(int anchorId); + boolean existsByAnchorId(long anchorId); @Query("SELECT f FROM FragmentEntity f WHERE anchor = :anchor") List findAllExtractsByAnchor(@Param("anchor") AnchorEntity anchorEntity); @Modifying @Query(value = "DELETE FROM fragment WHERE anchor_id = ANY (:anchorIds)", nativeQuery = true) - void deleteByAnchorIdIn(@Param("anchorIds") int[] anchorIds); + void deleteByAnchorIdIn(@Param("anchorIds") long[] anchorIds); default void deleteByAnchorIn(final Collection anchorEntities) { - deleteByAnchorIdIn(anchorEntities.stream().map(AnchorEntity::getId).mapToInt(id -> id).toArray()); + deleteByAnchorIdIn(anchorEntities.stream().map(AnchorEntity::getId).mapToLong(id -> id).toArray()); } @Modifying @Query(value = "DELETE FROM fragment WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths)", nativeQuery = true) - void deleteByAnchorIdAndXpaths(@Param("anchorId") int anchorId, @Param("xpaths") String[] xpaths); + void deleteByAnchorIdAndXpaths(@Param("anchorId") long anchorId, @Param("xpaths") String[] xpaths); - default void deleteByAnchorIdAndXpaths(final int anchorId, final Collection xpaths) { + default void deleteByAnchorIdAndXpaths(final long anchorId, final Collection xpaths) { deleteByAnchorIdAndXpaths(anchorId, xpaths.toArray(new String[0])); } @Modifying @Query(value = "DELETE FROM fragment f WHERE anchor_id = :anchorId AND xpath LIKE ANY (:xpathPatterns)", nativeQuery = true) - void deleteByAnchorIdAndXpathLikeAny(@Param("anchorId") int anchorId, + void deleteByAnchorIdAndXpathLikeAny(@Param("anchorId") long anchorId, @Param("xpathPatterns") String[] xpathPatterns); - default void deleteListsByAnchorIdAndXpaths(int anchorId, Collection xpaths) { + default void deleteListsByAnchorIdAndXpaths(long anchorId, Collection xpaths) { final String[] listXpathPatterns = xpaths.stream().map(xpath -> xpath + "[%").toArray(String[]::new); deleteByAnchorIdAndXpathLikeAny(anchorId, listXpathPatterns); } @Query(value = "SELECT xpath FROM fragment WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths)", nativeQuery = true) - List findAllXpathByAnchorIdAndXpathIn(@Param("anchorId") int anchorId, + List findAllXpathByAnchorIdAndXpathIn(@Param("anchorId") long anchorId, @Param("xpaths") String[] xpaths); default List findAllXpathByAnchorAndXpathIn(final AnchorEntity anchorEntity, @@ -125,11 +125,11 @@ public interface FragmentRepository extends JpaRepository, + "FROM fragment f INNER JOIN parent_search p ON f.id = p.id", nativeQuery = true ) - List findExtractsWithDescendants(@Param("anchorId") int anchorId, + List findExtractsWithDescendants(@Param("anchorId") long anchorId, @Param("xpaths") String[] xpaths, @Param("maxDepth") int maxDepth); - default List findExtractsWithDescendants(final int anchorId, final Collection xpaths, + default List findExtractsWithDescendants(final long anchorId, final Collection xpaths, final int maxDepth) { return findExtractsWithDescendants(anchorId, xpaths.toArray(new String[0]), maxDepth); } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleReferenceRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleReferenceRepository.java index ef701bc7dc..15ffa372f9 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleReferenceRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleReferenceRepository.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2022 Nordix Foundation. + * Copyright (C) 2022-2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,5 +25,5 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface ModuleReferenceRepository extends JpaRepository, ModuleReferenceQuery {} +public interface ModuleReferenceRepository extends JpaRepository, ModuleReferenceQuery {} diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepository.java index 0361749cdc..aacebd63b3 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepository.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. + * Copyright (C) 2021-2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,6 @@ import java.util.List; public interface SchemaSetYangResourceRepository { - void insertSchemaSetIdYangResourceId(final Integer schemaSetId, final List yangResourceId); + void insertSchemaSetIdYangResourceId(final Integer schemaSetId, final List yangResourceIds); } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepositoryImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepositoryImpl.java index c87e15adbf..0f7f2c0658 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepositoryImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepositoryImpl.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021-2022 Nordix Foundation. + * Copyright (C) 2021-2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,15 +37,15 @@ public class SchemaSetYangResourceRepositoryImpl implements SchemaSetYangResourc private EntityManager entityManager; @Override - public void insertSchemaSetIdYangResourceId(final Integer schemaSetId, final List yangResourceIds) { + public void insertSchemaSetIdYangResourceId(final Integer schemaSetId, final List yangResourceIds) { final Session session = entityManager.unwrap(Session.class); session.doWork(connection -> { try (PreparedStatement preparedStatement = connection.prepareStatement( "INSERT INTO SCHEMA_SET_YANG_RESOURCES (SCHEMA_SET_ID, YANG_RESOURCE_ID) VALUES ( ?, ?)")) { int sqlQueryCount = 1; - for (final long yangResourceId : yangResourceIds) { + for (final int yangResourceId : yangResourceIds) { preparedStatement.setInt(1, schemaSetId); - preparedStatement.setLong(2, yangResourceId); + preparedStatement.setInt(2, yangResourceId); preparedStatement.addBatch(); if (sqlQueryCount % MAX_INSERT_BATCH_SIZE == 0 || sqlQueryCount == yangResourceIds.size()) { preparedStatement.executeBatch(); diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepository.java index 335c971f97..9ae32b3e78 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepository.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2022 Nordix Foundation. + * Copyright (C) 2022-2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,6 @@ import org.onap.cps.spi.model.ModuleReference; public interface YangResourceNativeRepository { - List getResourceIdsByModuleReferences(Collection moduleReferences); + List getResourceIdsByModuleReferences(Collection moduleReferences); } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepositoryImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepositoryImpl.java index 850b274c91..f09a1a0c46 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepositoryImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepositoryImpl.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2022 Nordix Foundation. + * Copyright (C) 2022-2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,14 +42,14 @@ public class YangResourceNativeRepositoryImpl implements YangResourceNativeRepos @Override @Transactional - public List getResourceIdsByModuleReferences(final Collection moduleReferences) { + public List getResourceIdsByModuleReferences(final Collection moduleReferences) { if (moduleReferences.isEmpty()) { return Collections.emptyList(); } final Query query = entityManager.createNativeQuery(getCombinedSelectSqlQuery(moduleReferences)) .unwrap(org.hibernate.query.NativeQuery.class) - .addScalar("id", StandardBasicTypes.LONG); - final List yangResourceIds = query.getResultList(); + .addScalar("id", StandardBasicTypes.INTEGER); + final List yangResourceIds = query.getResultList(); if (yangResourceIds.size() != moduleReferences.size()) { log.warn("ModuleReferences size : {} and QueryResult size : {}", moduleReferences.size(), yangResourceIds.size()); diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java index 7584ff65c0..df21ccc52e 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java @@ -32,7 +32,7 @@ import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @Repository -public interface YangResourceRepository extends JpaRepository, +public interface YangResourceRepository extends JpaRepository, YangResourceNativeRepository, SchemaSetYangResourceRepository { List findAllByChecksumIn(String[] checksums); diff --git a/cps-ri/src/main/java/org/onap/cps/spi/utils/SessionManager.java b/cps-ri/src/main/java/org/onap/cps/spi/utils/SessionManager.java index 6f96cffdc1..eb3c5289af 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/utils/SessionManager.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/utils/SessionManager.java @@ -163,7 +163,7 @@ public class SessionManager { final Session session = getSession(sessionId); final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName); - final int anchorId = anchorEntity.getId(); + final long anchorId = anchorEntity.getId(); log.debug("Attempting to lock anchor {} for session {}", anchorName, sessionId); session.get(AnchorEntity.class, anchorId, LockMode.PESSIMISTIC_WRITE); log.info("Anchor {} successfully locked", anchorName); -- cgit 1.2.3-korg