2017-03-09 62 views
0

林在彈簧引導應用程式H2數據的基礎上(MYSQL投訴)更新查詢不工作eventhough沒有錯誤表示

工作我有以下

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更新查詢正在工作..)

是什麼問題?

感謝您的幫助!

UPDATE

我想的問題是,我需要首先檢查行表已經存在 - 如果不創建一個,否則更新現有的一個。

如何使用@Query來做到這一點?我看到一些與@SQLinsert但我不能得到它的工作!

helppp :(

回答

0

很難說這個問題是在這裏什麼

我看到兩個主要候選人:。一些問題與事務,因此實際的變化確實發生了,但沒有得到COMMITED或者您where子句是關閉的。

爲了調試這個激活SQL日誌記錄和事務日誌記錄。

此外,刪除where子句,並檢查是否沒有where子句工作的。如果它確實增加了,其中的內容更新條款一個一個地找回問題狀況。

+0

我認爲它是因爲我不檢查線是否存在,然後嘗試更新!你有什麼想法,我會怎麼做?像「插入...重複密鑰更新」? – nadavgam

+0

你可以有一個返回類型的int並獲取更新的行數(我認爲)。如果0做一個插入。 –

+0

但那不是有效的,我不想等待從dababase repsonse,只有比更新..不是? – nadavgam