Save and flush device during transaction to trigger constraint validation.
This commit is contained in:
parent
08c8482f06
commit
1bfe1bb61e
@ -54,6 +54,14 @@ public interface DeviceDao extends Dao<Device>, TenantEntityDao {
|
||||
*/
|
||||
Device save(TenantId tenantId, Device device);
|
||||
|
||||
/**
|
||||
* Save or update device object
|
||||
*
|
||||
* @param device the device object
|
||||
* @return saved device object
|
||||
*/
|
||||
Device saveAndFlush(TenantId tenantId, Device device);
|
||||
|
||||
/**
|
||||
* Find devices by tenantId and page link.
|
||||
*
|
||||
|
||||
@ -268,7 +268,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
|
||||
}
|
||||
device.setType(deviceProfile.getName());
|
||||
device.setDeviceData(syncDeviceData(deviceProfile, device.getDeviceData()));
|
||||
return deviceDao.save(device.getTenantId(), device);
|
||||
return deviceDao.saveAndFlush(device.getTenantId(), device);
|
||||
} catch (Exception t) {
|
||||
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
|
||||
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_name_unq_key")) {
|
||||
|
||||
@ -17,6 +17,7 @@ package org.thingsboard.server.dao.sql.device;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
@ -30,7 +31,7 @@ import java.util.UUID;
|
||||
/**
|
||||
* Created by Valerii Sosliuk on 5/6/2017.
|
||||
*/
|
||||
public interface DeviceRepository extends PagingAndSortingRepository<DeviceEntity, UUID> {
|
||||
public interface DeviceRepository extends JpaRepository<DeviceEntity, UUID> {
|
||||
|
||||
@Query("SELECT new org.thingsboard.server.dao.model.sql.DeviceInfoEntity(d, c.title, c.additionalInfo, p.name) " +
|
||||
"FROM DeviceEntity d " +
|
||||
|
||||
@ -22,6 +22,7 @@ import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.thingsboard.server.common.data.Device;
|
||||
import org.thingsboard.server.common.data.DeviceInfo;
|
||||
@ -71,6 +72,14 @@ public class JpaDeviceDao extends JpaAbstractSearchTextDao<DeviceEntity, Device>
|
||||
return DaoUtil.getData(deviceRepository.findDeviceInfoById(deviceId));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Device saveAndFlush(TenantId tenantId, Device device) {
|
||||
Device result = this.save(tenantId, device);
|
||||
deviceRepository.flush();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageData<Device> findDevicesByTenantId(UUID tenantId, PageLink pageLink) {
|
||||
if (StringUtils.isEmpty(pageLink.getTextSearch())) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user