Commit 1bfe1bb61e55f6a6d9ce71b2a00f3f76ae95be0e

Authored by Igor Kulikov
1 parent 08c8482f

Save and flush device during transaction to trigger constraint validation.

... ... @@ -55,6 +55,14 @@ public interface DeviceDao extends Dao<Device>, TenantEntityDao {
55 55 Device save(TenantId tenantId, Device device);
56 56
57 57 /**
  58 + * Save or update device object
  59 + *
  60 + * @param device the device object
  61 + * @return saved device object
  62 + */
  63 + Device saveAndFlush(TenantId tenantId, Device device);
  64 +
  65 + /**
58 66 * Find devices by tenantId and page link.
59 67 *
60 68 * @param tenantId the tenantId
... ...
... ... @@ -268,7 +268,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
268 268 }
269 269 device.setType(deviceProfile.getName());
270 270 device.setDeviceData(syncDeviceData(deviceProfile, device.getDeviceData()));
271   - return deviceDao.save(device.getTenantId(), device);
  271 + return deviceDao.saveAndFlush(device.getTenantId(), device);
272 272 } catch (Exception t) {
273 273 ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
274 274 if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_name_unq_key")) {
... ...
... ... @@ -17,6 +17,7 @@ package org.thingsboard.server.dao.sql.device;
17 17
18 18 import org.springframework.data.domain.Page;
19 19 import org.springframework.data.domain.Pageable;
  20 +import org.springframework.data.jpa.repository.JpaRepository;
20 21 import org.springframework.data.jpa.repository.Query;
21 22 import org.springframework.data.repository.PagingAndSortingRepository;
22 23 import org.springframework.data.repository.query.Param;
... ... @@ -30,7 +31,7 @@ import java.util.UUID;
30 31 /**
31 32 * Created by Valerii Sosliuk on 5/6/2017.
32 33 */
33   -public interface DeviceRepository extends PagingAndSortingRepository<DeviceEntity, UUID> {
  34 +public interface DeviceRepository extends JpaRepository<DeviceEntity, UUID> {
34 35
35 36 @Query("SELECT new org.thingsboard.server.dao.model.sql.DeviceInfoEntity(d, c.title, c.additionalInfo, p.name) " +
36 37 "FROM DeviceEntity d " +
... ...
... ... @@ -22,6 +22,7 @@ import org.springframework.data.domain.Page;
22 22 import org.springframework.data.domain.Pageable;
23 23 import org.springframework.data.repository.CrudRepository;
24 24 import org.springframework.stereotype.Component;
  25 +import org.springframework.transaction.annotation.Transactional;
25 26 import org.springframework.util.StringUtils;
26 27 import org.thingsboard.server.common.data.Device;
27 28 import org.thingsboard.server.common.data.DeviceInfo;
... ... @@ -72,6 +73,14 @@ public class JpaDeviceDao extends JpaAbstractSearchTextDao<DeviceEntity, Device>
72 73 }
73 74
74 75 @Override
  76 + @Transactional
  77 + public Device saveAndFlush(TenantId tenantId, Device device) {
  78 + Device result = this.save(tenantId, device);
  79 + deviceRepository.flush();
  80 + return result;
  81 + }
  82 +
  83 + @Override
75 84 public PageData<Device> findDevicesByTenantId(UUID tenantId, PageLink pageLink) {
76 85 if (StringUtils.isEmpty(pageLink.getTextSearch())) {
77 86 return DaoUtil.toPageData(
... ...