2

気象データを収集するためのアプリケーションを設計しています。「場所」と「記録」の 2 つの POJO オブジェクトがあります。Location には緯度と経度、および現在の気象条件に関する情報が含まれており、Record には特定の場所の経時的なすべての気象情報が含まれているため、Location と多対 1 の関係があります。私が持っているクラスの定義は次のとおりです。

Location.java

@Entity
@Table(name = "location")
@NamedQueries( {
 @NamedQuery(name = "findLocations", query = "SELECT e FROM Location e ORDER BY e.longitude, e.latitude"),
 @NamedQuery(name = "findLocationByLatLong", query = "SELECT e from Location e WHERE e.latitude = :latitude AND e.longitude = :longitude"),
 @NamedQuery(name = "findLocationById", query = "SELECT e from Location e WHERE e.id = :id"),
 @NamedQuery(name = "deleteLocationById", query= "DELETE Location e WHERE e.id = :id"),
 @NamedQuery(name = "updateLocation", query = "UPDATE Location e SET e.lastModifiedDate = :lastModifiedDate WHERE e.id = :id")})

public class Location implements Serializable {

 /**
  * 
  */
 private static final long serialVersionUID = 1L;

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 protected Long id;

 @Column(name="latitude", nullable=false)
 protected String latitude;

 @Column(name="longitude", nullable=false)
 protected String longitude;

 @Column(name="lastModifiedDate")
 @Temporal(TemporalType.TIMESTAMP)
 private Date lastModifiedDate;

 @Column(name="windDirection")
 private float windDirection;

 @Column(name="windSpeed")
 private float windSpeed;

 @Column(name="temperature")
 private float temperature;
}

そして Record.java

@Entity
@Table(name = "weatherdata")
@NamedQueries( {
  @NamedQuery(name = "findWeatherRecordById", query = "SELECT e from Record e WHERE e.id = :id"),
  @NamedQuery(name = "findWeatherRecords", query = "SELECT e from Record e WHERE e.parent = :parent") })
public class Record implements Serializable{

 /**
  * 
  */
 private static final long serialVersionUID = 1L;

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name="id")
 protected Long id;

 @Column(name="mTime")
 @Temporal(TemporalType.TIMESTAMP)
 private Date mtime;

 @Column(name="windDirection")
 private float windDirection;

 @Column(name="windSpeed")
 private float windSpeed;

 @Column(name="temperature")
 private float temperature;

 @ManyToOne(cascade = { CascadeType.ALL }, targetEntity=Location.class, fetch = FetchType.EAGER)
 @JoinColumn(name="locationId")
 protected Location parent;
}

そして、私の受信データは次の形式になっています。

latitude,longitude,date,winddirection,windspeed,temperature
36.9822,-122.0153,20100907000000.00,158,2.68,20.57
38.1838,-120.54,20100907000000.00,248,0,26.68
38.3495,-121.9688,20100907000000.00,149,0.45,33.9
38.41935,-121.36029,20100907000000.00,322,0.9,33.9
37.91617,-122.286,20100907000000.00,224,0,24.46
38.587,-121.3162,20100907000000.00,315,0,34.46
36.8717,-121.6555,20100907000000.00,294,3.13,18.34

レコードを取得するたびに、レコード テーブルに挿入したいと考えています。また、Location への外部キーがあるため、Location テーブルの locationId も追加します。もう1つのことは、ロケーションテーブルが事前設定されていないことです。したがって、新しいレコードが来るたびに、まずそれを Location テーブルに挿入し、次に Record テーブルに外部キーを設定します。そして、場所テーブルに重複する場所のエントリは必要ありません。ロケーション テーブルには、ご覧のとおり、最新の気温、風速、風向データも含まれます。

私はそれを達成するために次のコードを使用しています:

Location loc = handler.getLocation(line);   
//loc.setTemperature(0);

Location dbLoc = null;

try {
    Query q = eManager.createNamedQuery("findLocationByLatLong");
    q.setParameter("latitude", loc.getLatitude());
    q.setParameter("longitude", loc.getLongitude());
    dbLoc = (Location) q.getSingleResult();                     
} catch (Exception e) {
    System.out.println("Location not found! Creating new location");
    Logger.getLogger("WeatherRecorderBean.class").log(Level.WARNING, e.getMessage());
}


Record r = handler.getRecord(line);

if(dbLoc!=null) {
    r.setParent(dbLoc);

    dbLoc.setLastModifiedDate(r.getMtime());//I am doing this so as to know what time the weather change entry is about
    dbLoc.setWindDirection(r.getWindDirection());
    dbLoc.setWindSpeed(r.getWindSpeed());
    dbLoc.setTemperature(r.getTemperature());

    eManager.merge(r);                      
}
else {
    dbLoc = new Location();
    dbLoc.setLatitude(loc.getLatitude());
    dbLoc.setLongitude(loc.getLongitude());
    //eManager.persist(dbLoc);

    r.setParent(dbLoc);                     

    dbLoc.setLastModifiedDate(r.getMtime());
    dbLoc.setWindDirection(r.getWindDirection());
    dbLoc.setWindSpeed(r.getWindSpeed());
    dbLoc.setTemperature(r.getTemperature());

    eManager.merge(r);
    //eManager.merge(dbLoc);

}

しかし、これを行うことで、場所が複製されています。つまり、同じ経度と緯度に対して複数のエントリがありますが、場所テーブルには温度と風速のデータが異なります。私が達成したいのは、1 つの緯度と経度に対して 1 つのエントリを作成し、最新のデータで風速、温度、および風向のフィールドを更新することです。

助けてください!

4

2 に答える 2

2

ALL操作を からRecordにカスケードしてLocationいるため、 newRecord その新しい parentをマージするときLocationに、一時的なものを再度マージする必要はありませんLocation(そうしないと、重複した行が得られます)。

以下のコードにいくつかのコメントを入れました(すべてを修正したわけではありません。IMOにはさらに問題がありますが、提案された変更により、少なくとも重複Locationエントリの作成が削除されるはずです):

if(dbLoc==null) {
  dbLoc = new Location();
  dbLoc.setLatitude(loc.getLatitude());
  dbLoc.setLongitude(loc.getLongitude());
  r.setParent(dbLoc);

  // add changes on the dbLoc here

  eManager.merge(r);  // here you're merging r and the dbLoc 

  //loc.setLastModifiedDate(r.getMtime()); // why are you modifying the line from the file?
  //loc.setWindDirection(r.getWindDirection());
  //loc.setWindSpeed(r.getWindSpeed());
  //loc.setTemperature(r.getTemperature());
  //eManager.persist(loc);    
  //System.out.println("Location id : "+loc.getId());

  //eManager.merge(dbLoc); // here you're merging a transient dbLoc again

}
于 2010-09-29T23:24:24.883 に答える
0

解決しました:-)

public Location saveLocation(Location loc) {
    eManager.clear();
    eManager.setFlushMode(FlushModeType.COMMIT);
    //eManager.setFlushMode(FlushModeType.COMMIT);
    Query q = eManager.createNamedQuery("findLocationByLatLong");
    q.setParameter("latitude", loc.getLatitude());
    q.setParameter("longitude", loc.getLongitude());
    try {
        Location dummy = (Location) q.getSingleResult();
        eManager.clear();
        // eManager.flush();
        return dummy;
    } catch (NoResultException ex) {
        Logger.getLogger("WeatherRecorderBean.class").log(Level.WARNING,
                ex.getMessage());
        eManager.clear();
        eManager.merge(loc);
        eManager.flush();
        return loc;
    } catch (Exception ex) {
        Logger.getLogger("WeatherRecorderBean.class").log(Level.WARNING, "Should never get here! "+ex.getMessage());
        return null;
    }
}

を保存するための新しい関数を作成しますLocation。また、いくつかの同期の問題もありました。この関数は MDBonMessage()関数にありました。そのため、1 つが終了する前にonMessage()別のものが開始されたため、重複したエントリが作成されました!

これが将来誰かに役立つことを願っています!

于 2010-10-01T23:14:26.350 に答える