我正在使用 h2 数据库( MYSQL 投诉)在 spring boot 应用中工作
我有以下存储库:
public interface IGrpAvgMetricTimeSeriesRepository extends
CrudRepository<GrpAvgMetricTimeSeriesEntity, GrpAvgMetricTimeSeriesEntityPK> {
@Transactional
@Modifying(clearAutomatically = true)
@Query(
"update GrpAvgMetricTimeSeriesEntity ge Set ge.sampleCount = ge.sampleCount + 1 ,ge.sumVal = ge.sumVal + ?1 "
+ "where ge.groupId = ?2 and ge.metric = ?3 and ge.normalizedTimeStamp = ?4 ")
void updateSumAndCounter(double value, String grpId, String metric, long normalizedTimeStamp);
@Query("select g from GrpAvgMetricTimeSeriesEntity g where g.normalizedTimeStamp >= ?1 "
+ "and g.normalizedTimeStamp <= ?2 and g.groupId = ?3 and g.metric = ?4 order by g.normalizedTimeStamp ")
List<GrpAvgMetricTimeSeriesEntity> getPointsBetween(long fromTime, long toTime, String grpId,
String metric);
}
使用以下实体:
@Entity
@IdClass(GrpAvgMetricTimeSeriesEntityPK.class)
public class GrpAvgMetricTimeSeriesEntity {
@NotNull
@Id
private String groupId;
@NotNull
@Id
private String metric;
@NotNull
@Id
private long normalizedTimeStamp;
@NotNull
private double sumVal;
@NotNull
private long sampleCount;
public GrpAvgMetricTimeSeriesEntity(){
//For mapper
}
public GrpAvgMetricTimeSeriesEntity(String groupId, String metric, long normalizedTimeStamp,
float sumVal, long sampleCount) {
this.groupId = groupId;
this.metric = metric;
this.normalizedTimeStamp = normalizedTimeStamp;
this.sumVal = sumVal;
this.sampleCount = sampleCount;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public String getMetric() {
return metric;
}
public void setMetric(String metric) {
this.metric = metric;
}
public long getNormalizedTimeStamp() {
return normalizedTimeStamp;
}
public void setNormalizedTimeStamp(long normalizedTimeStamp) {
this.normalizedTimeStamp = normalizedTimeStamp;
}
public double getSumVal() {
return sumVal;
}
public void setSumVal(float sumVal) {
this.sumVal = sumVal;
}
public long getSampleCount() {
return sampleCount;
}
public void setSampleCount(long sampleCount) {
this.sampleCount = sampleCount;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
GrpAvgMetricTimeSeriesEntity that = (GrpAvgMetricTimeSeriesEntity) o;
return normalizedTimeStamp == that.normalizedTimeStamp &&
Double.compare(that.sumVal, sumVal) == 0 &&
sampleCount == that.sampleCount &&
Objects.equal(groupId, that.groupId) &&
Objects.equal(metric, that.metric);
}
@Override
public int hashCode() {
return Objects.hashCode(groupId, metric, normalizedTimeStamp, sumVal, sampleCount);
}
@Override
public String toString() {
return "GrpAvgMetricTimeSeriesEntity{" +
"groupId='" + groupId + '\'' +
", metric='" + metric + '\'' +
", normalizedTimeStamp=" + normalizedTimeStamp +
", sumVal=" + sumVal +
", sampleCount=" + sampleCount +
'}';
}
}
以下 sql脚本:
create table host_time_series_entity (
time_stamp long not null,
host_name varchar(255) not null,
metric_name varchar(255) not null,
metric_value double(255),
primary key (time_stamp, host_name, metric_name)
);
create table grp_avg_metric_time_series_entity (group_id varchar(255) not null, metric varchar(255) not null, normalized_time_stamp bigint not null, sum_val DOUBLE not null,sample_count bigint not null, primary key(group_id, metric, normalized_time_stamp));
当应用程序运行并且执行此查询时,不会抛出任何异常。
在Jpa单元测试中,一切都在过去。
H2正在使用所有正确的列创建所有模式..
但是,执行查询后,h2中没有更新新行(第二个表的其他repo只是“保存”查询正在运行..)
问题是什么?
感谢您的帮助!!
更新
我认为问题是我需要首先检查表中是否存在该行 - 如果不存在,则更新现有行。
如何使用@Query执行此操作?我看到@SQLinsert的东西,但我无法让它工作!!
helppp :(
答案 0 :(得分:0)
很难说出这里的问题。
我看到两个主要的候选人:交易的一些问题,所以实际上确实发生了变化,但没有得到提交。或者你的where子句已关闭。
为了调试此激活SQL日志记录和事务日志记录。
此外,删除where子句并检查没有where子句的更新是否有效。如果它确实添加了where子句的元素,则逐个查找有问题的条件。