From 5000405959b05fb3562a2f2cd55a1ba10e6464e2 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 5 Sep 2012 15:08:48 +0200 Subject: [PATCH] SONAR-3778 Bad performances when Hibernate requests measures on quality models --- .../org/sonar/batch/DefaultTimeMachine.java | 4 +- .../sonar/batch/index/MeasurePersister.java | 4 +- .../api/database/model/MeasureModel.java | 39 +++++++++++++++---- .../api/qualitymodel/Characteristic.java | 2 +- 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultTimeMachine.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultTimeMachine.java index ad2ec8007b8..8d34661cb25 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/DefaultTimeMachine.java +++ b/sonar-batch/src/main/java/org/sonar/batch/DefaultTimeMachine.java @@ -99,7 +99,7 @@ public class DefaultTimeMachine implements TimeMachine { params.put("status", Snapshot.STATUS_PROCESSED); params.put("lib", Qualifiers.LIBRARY); - sb.append(" AND m.characteristic IS NULL"); + sb.append(" AND m.characteristicId IS NULL"); sb.append(" AND m.personId IS NULL"); sb.append(" AND m.ruleId IS NULL AND m.rulePriority IS NULL"); if (!metricIds.isEmpty()) { @@ -161,8 +161,6 @@ public class DefaultTimeMachine implements TimeMachine { measure.setVariation4(model.getVariationValue4()); measure.setVariation5(model.getVariationValue5()); measure.setUrl(model.getUrl()); - measure.setCharacteristic(model.getCharacteristic()); - measure.setPersonId(model.getPersonId()); return measure; } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java index ec036f13da4..36b1e3837fb 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java @@ -137,7 +137,9 @@ public final class MeasurePersister { model.setVariationValue4(measure.getVariation4()); model.setVariationValue5(measure.getVariation5()); model.setUrl(measure.getUrl()); - model.setCharacteristic(measure.getCharacteristic()); + if (measure.getCharacteristic()!=null) { + model.setCharacteristicId(measure.getCharacteristic().getId()); + } model.setPersonId(measure.getPersonId()); if (measure.getValue() != null) { model.setValue(measure.getValue().doubleValue()); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureModel.java b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureModel.java index 0e82d047a1d..3485cbecbe9 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureModel.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureModel.java @@ -19,6 +19,7 @@ */ package org.sonar.api.database.model; +import com.google.common.base.Preconditions; import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import org.sonar.api.database.DatabaseSession; @@ -27,7 +28,6 @@ import org.sonar.api.qualitymodel.Characteristic; import org.sonar.api.rules.RulePriority; import javax.persistence.*; - import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -112,9 +112,8 @@ public class MeasureModel implements Cloneable { @OneToMany(mappedBy = "measure", fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}) private List measureData = new ArrayList(); - @ManyToOne(fetch = FetchType.EAGER) - @JoinColumn(name = "characteristic_id") - private Characteristic characteristic; + @Column(name = "characteristic_id", updatable = true, nullable = true) + private Integer characteristicId; @Column(name = "person_id", updatable = true, nullable = true) private Integer personId; @@ -497,12 +496,38 @@ public class MeasureModel implements Cloneable { return this; } + public Integer getCharacteristicId() { + return characteristicId; + } + + public MeasureModel setCharacteristicId(Integer i) { + this.characteristicId = i; + return this; + } + + /** + * @deprecated replaced by {@link org.sonar.api.database.model.MeasureModel#getCharacteristicId()} since 3.3. See https://jira.codehaus.org/browse/SONAR-3778 + */ + @Deprecated public Characteristic getCharacteristic() { - return characteristic; + Characteristic c = null; + if (characteristicId != null) { + c = Characteristic.create().setId(characteristicId); + } + return c; } + /** + * @deprecated replaced by {@link org.sonar.api.database.model.MeasureModel#setCharacteristicId(Integer)} since 3.3. See https://jira.codehaus.org/browse/SONAR-3778 + */ + @Deprecated public MeasureModel setCharacteristic(Characteristic c) { - this.characteristic = c; + if (c == null) { + this.characteristicId = null; + } else { + Preconditions.checkArgument(c.getId()!=null, "Characteristic id must not be null"); + this.characteristicId = c.getId(); + } return this; } @@ -535,7 +560,7 @@ public class MeasureModel implements Cloneable { clone.setSnapshotId(getSnapshotId()); clone.setMeasureDate(getMeasureDate()); clone.setUrl(getUrl()); - clone.setCharacteristic(getCharacteristic()); + clone.setCharacteristicId(getCharacteristicId()); clone.setPersonId(getPersonId()); return clone; } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/Characteristic.java b/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/Characteristic.java index c2a5da7fb9f..523ec19c2f3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/Characteristic.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/Characteristic.java @@ -94,7 +94,7 @@ public final class Characteristic implements Comparable { return id; } - Characteristic setId(Integer id) { + public Characteristic setId(Integer id) { this.id = id; return this; } -- 2.39.5