2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.spi.repository;
23 import java.sql.PreparedStatement;
24 import javax.persistence.EntityManager;
25 import javax.persistence.PersistenceContext;
26 import org.hibernate.Session;
27 import org.springframework.stereotype.Repository;
30 public class FragmentNativeRepositoryImpl implements FragmentNativeRepository {
32 private static final String DROP_FRAGMENT_CONSTRAINT
33 = "ALTER TABLE fragment DROP CONSTRAINT fragment_parent_id_fkey;";
34 private static final String ADD_FRAGMENT_CONSTRAINT_WITH_CASCADE
35 = "ALTER TABLE fragment ADD CONSTRAINT fragment_parent_id_fkey FOREIGN KEY (parent_id) "
36 + "REFERENCES fragment (id) ON DELETE CASCADE;";
37 private static final String DELETE_FRAGMENT = "DELETE FROM fragment WHERE id =?;";
38 private static final String ADD_ORIGINAL_FRAGMENT_CONSTRAINT
39 = "ALTER TABLE fragment ADD CONSTRAINT fragment_parent_id_fkey FOREIGN KEY (parent_id) "
40 + "REFERENCES fragment (id) ON DELETE NO ACTION;";
43 private EntityManager entityManager;
46 public void deleteFragmentEntity(final long fragmentEntityId) {
47 final Session session = entityManager.unwrap(Session.class);
48 session.doWork(connection -> {
49 try (PreparedStatement preparedStatement = connection.prepareStatement(
50 DROP_FRAGMENT_CONSTRAINT
51 + ADD_FRAGMENT_CONSTRAINT_WITH_CASCADE
53 + DROP_FRAGMENT_CONSTRAINT
54 + ADD_ORIGINAL_FRAGMENT_CONSTRAINT)) {
55 preparedStatement.setLong(1, fragmentEntityId);
56 preparedStatement.executeUpdate();