|
|
@ -19,10 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.jdbc.core.BatchPreparedStatementSetter; |
|
|
import org.springframework.jdbc.core.BatchPreparedStatementSetter; |
|
|
import org.springframework.jdbc.core.JdbcTemplate; |
|
|
import org.springframework.jdbc.core.JdbcTemplate; |
|
|
import org.springframework.stereotype.Repository; |
|
|
import org.springframework.stereotype.Repository; |
|
|
import org.springframework.transaction.TransactionStatus; |
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
import org.springframework.transaction.support.TransactionCallbackWithoutResult; |
|
|
|
|
|
import org.springframework.transaction.support.TransactionTemplate; |
|
|
|
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
import org.thingsboard.server.dao.model.sql.RelationEntity; |
|
|
import org.thingsboard.server.dao.model.sql.RelationEntity; |
|
|
|
|
|
|
|
|
@ -52,9 +49,6 @@ public class SqlRelationInsertRepository implements RelationInsertRepository { |
|
|
@Autowired |
|
|
@Autowired |
|
|
protected JdbcTemplate jdbcTemplate; |
|
|
protected JdbcTemplate jdbcTemplate; |
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private TransactionTemplate transactionTemplate; |
|
|
|
|
|
|
|
|
|
|
|
protected Query getQuery(RelationEntity entity, String query) { |
|
|
protected Query getQuery(RelationEntity entity, String query) { |
|
|
Query nativeQuery = entityManager.createNativeQuery(query, RelationEntity.class); |
|
|
Query nativeQuery = entityManager.createNativeQuery(query, RelationEntity.class); |
|
|
if (entity.getAdditionalInfo() == null) { |
|
|
if (entity.getAdditionalInfo() == null) { |
|
|
@ -78,36 +72,31 @@ public class SqlRelationInsertRepository implements RelationInsertRepository { |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void saveOrUpdate(List<RelationEntity> entities) { |
|
|
public void saveOrUpdate(List<RelationEntity> entities) { |
|
|
transactionTemplate.execute(new TransactionCallbackWithoutResult() { |
|
|
jdbcTemplate.batchUpdate(INSERT_ON_CONFLICT_DO_UPDATE_JDBC, new BatchPreparedStatementSetter() { |
|
|
@Override |
|
|
@Override |
|
|
protected void doInTransactionWithoutResult(TransactionStatus status) { |
|
|
public void setValues(PreparedStatement ps, int i) throws SQLException { |
|
|
jdbcTemplate.batchUpdate(INSERT_ON_CONFLICT_DO_UPDATE_JDBC, new BatchPreparedStatementSetter() { |
|
|
RelationEntity relation = entities.get(i); |
|
|
@Override |
|
|
ps.setObject(1, relation.getFromId()); |
|
|
public void setValues(PreparedStatement ps, int i) throws SQLException { |
|
|
ps.setString(2, relation.getFromType()); |
|
|
RelationEntity relation = entities.get(i); |
|
|
ps.setObject(3, relation.getToId()); |
|
|
ps.setObject(1, relation.getFromId()); |
|
|
ps.setString(4, relation.getToType()); |
|
|
ps.setString(2, relation.getFromType()); |
|
|
|
|
|
ps.setObject(3, relation.getToId()); |
|
|
ps.setString(5, relation.getRelationTypeGroup()); |
|
|
ps.setString(4, relation.getToType()); |
|
|
ps.setString(6, relation.getRelationType()); |
|
|
|
|
|
|
|
|
ps.setString(5, relation.getRelationTypeGroup()); |
|
|
if (relation.getAdditionalInfo() == null) { |
|
|
ps.setString(6, relation.getRelationType()); |
|
|
ps.setString(7, null); |
|
|
|
|
|
ps.setString(8, null); |
|
|
if (relation.getAdditionalInfo() == null) { |
|
|
} else { |
|
|
ps.setString(7, null); |
|
|
String json = JacksonUtil.toString(relation.getAdditionalInfo()); |
|
|
ps.setString(8, null); |
|
|
ps.setString(7, json); |
|
|
} else { |
|
|
ps.setString(8, json); |
|
|
String json = JacksonUtil.toString(relation.getAdditionalInfo()); |
|
|
} |
|
|
ps.setString(7, json); |
|
|
} |
|
|
ps.setString(8, json); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public int getBatchSize() { |
|
|
public int getBatchSize() { |
|
|
return entities.size(); |
|
|
return entities.size(); |
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|