Details
diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/AttributeKvCompositeKey.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/AttributeKvCompositeKey.java
index 3001e12..1c9f17a 100644
--- a/dao/src/main/java/org/thingsboard/server/dao/model/sql/AttributeKvCompositeKey.java
+++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/AttributeKvCompositeKey.java
@@ -20,14 +20,29 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import org.thingsboard.server.common.data.EntityType;
+import javax.persistence.Column;
+import javax.persistence.Embeddable;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
import java.io.Serializable;
+import static org.thingsboard.server.dao.model.ModelConstants.ATTRIBUTE_KEY_COLUMN;
+import static org.thingsboard.server.dao.model.ModelConstants.ATTRIBUTE_TYPE_COLUMN;
+import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_ID_COLUMN;
+import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_TYPE_COLUMN;
+
@Data
@AllArgsConstructor
@NoArgsConstructor
+@Embeddable
public class AttributeKvCompositeKey implements Serializable {
+ @Enumerated(EnumType.STRING)
+ @Column(name = ENTITY_TYPE_COLUMN)
private EntityType entityType;
+ @Column(name = ENTITY_ID_COLUMN)
private String entityId;
+ @Column(name = ATTRIBUTE_TYPE_COLUMN)
private String attributeType;
+ @Column(name = ATTRIBUTE_KEY_COLUMN)
private String attributeKey;
}
diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/AttributeKvEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/AttributeKvEntity.java
index 587a314..515c86c 100644
--- a/dao/src/main/java/org/thingsboard/server/dao/model/sql/AttributeKvEntity.java
+++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/AttributeKvEntity.java
@@ -27,6 +27,7 @@ import org.thingsboard.server.common.data.kv.StringDataEntry;
import org.thingsboard.server.dao.model.ToData;
import javax.persistence.Column;
+import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
@@ -48,25 +49,10 @@ import static org.thingsboard.server.dao.model.ModelConstants.STRING_VALUE_COLUM
@Data
@Entity
@Table(name = "attribute_kv")
-@IdClass(AttributeKvCompositeKey.class)
public class AttributeKvEntity implements ToData<AttributeKvEntry>, Serializable {
- @Id
- @Enumerated(EnumType.STRING)
- @Column(name = ENTITY_TYPE_COLUMN)
- private EntityType entityType;
-
- @Id
- @Column(name = ENTITY_ID_COLUMN)
- private String entityId;
-
- @Id
- @Column(name = ATTRIBUTE_TYPE_COLUMN)
- private String attributeType;
-
- @Id
- @Column(name = ATTRIBUTE_KEY_COLUMN)
- private String attributeKey;
+ @EmbeddedId
+ private AttributeKvCompositeKey id;
@Column(name = BOOLEAN_VALUE_COLUMN)
private Boolean booleanValue;
@@ -87,13 +73,13 @@ public class AttributeKvEntity implements ToData<AttributeKvEntry>, Serializable
public AttributeKvEntry toData() {
KvEntry kvEntry = null;
if (strValue != null) {
- kvEntry = new StringDataEntry(attributeKey, strValue);
+ kvEntry = new StringDataEntry(id.getAttributeKey(), strValue);
} else if (booleanValue != null) {
- kvEntry = new BooleanDataEntry(attributeKey, booleanValue);
+ kvEntry = new BooleanDataEntry(id.getAttributeKey(), booleanValue);
} else if (doubleValue != null) {
- kvEntry = new DoubleDataEntry(attributeKey, doubleValue);
+ kvEntry = new DoubleDataEntry(id.getAttributeKey(), doubleValue);
} else if (longValue != null) {
- kvEntry = new LongDataEntry(attributeKey, longValue);
+ kvEntry = new LongDataEntry(id.getAttributeKey(), longValue);
}
return new BaseAttributeKvEntry(kvEntry, lastUpdateTs);
}
diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/AttributeKvRepository.java b/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/AttributeKvRepository.java
index c76cefe..d716886 100644
--- a/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/AttributeKvRepository.java
+++ b/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/AttributeKvRepository.java
@@ -15,7 +15,9 @@
*/
package org.thingsboard.server.dao.sql.attributes;
+import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
+import org.springframework.data.repository.query.Param;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.dao.model.sql.AttributeKvCompositeKey;
import org.thingsboard.server.dao.model.sql.AttributeKvEntity;
@@ -26,8 +28,11 @@ import java.util.List;
@SqlDao
public interface AttributeKvRepository extends CrudRepository<AttributeKvEntity, AttributeKvCompositeKey> {
- List<AttributeKvEntity> findAllByEntityTypeAndEntityIdAndAttributeType(EntityType entityType,
- String entityId,
- String attributeType);
+ @Query("SELECT a FROM AttributeKvEntity a WHERE a.id.entityType = :entityType " +
+ "AND a.id.entityId = :entityId " +
+ "AND a.id.attributeType = :attributeType")
+ List<AttributeKvEntity> findAllByEntityTypeAndEntityIdAndAttributeType(@Param("entityType") EntityType entityType,
+ @Param("entityId") String entityId,
+ @Param("attributeType") String attributeType);
}
diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/JpaAttributeDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/JpaAttributeDao.java
index 0dabf4c..4ac0c0c 100644
--- a/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/JpaAttributeDao.java
+++ b/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/JpaAttributeDao.java
@@ -79,10 +79,7 @@ public class JpaAttributeDao extends JpaAbstractDaoListeningExecutorService impl
@Override
public ListenableFuture<Void> save(EntityId entityId, String attributeType, AttributeKvEntry attribute) {
AttributeKvEntity entity = new AttributeKvEntity();
- entity.setEntityType(entityId.getEntityType());
- entity.setEntityId(fromTimeUUID(entityId.getId()));
- entity.setAttributeType(attributeType);
- entity.setAttributeKey(attribute.getKey());
+ entity.setId(new AttributeKvCompositeKey(entityId.getEntityType(), fromTimeUUID(entityId.getId()), attributeType, attribute.getKey()));
entity.setLastUpdateTs(attribute.getLastUpdateTs());
entity.setStrValue(attribute.getStrValue().orElse(null));
entity.setDoubleValue(attribute.getDoubleValue().orElse(null));
@@ -100,10 +97,7 @@ public class JpaAttributeDao extends JpaAbstractDaoListeningExecutorService impl
.stream()
.map(key -> {
AttributeKvEntity entityToDelete = new AttributeKvEntity();
- entityToDelete.setEntityType(entityId.getEntityType());
- entityToDelete.setEntityId(fromTimeUUID(entityId.getId()));
- entityToDelete.setAttributeType(attributeType);
- entityToDelete.setAttributeKey(key);
+ entityToDelete.setId(new AttributeKvCompositeKey(entityId.getEntityType(), fromTimeUUID(entityId.getId()), attributeType, key));
return entityToDelete;
}).collect(Collectors.toList());