2016-09-28 62 views
0

我爲後端API使用Play2.0框架。所以我想從數據庫的遊樂設施列表,我想排除具有重複「地點」名稱的乘坐。排除PagedList中的重複數據

我正在使用此代碼,但這給了我存儲在數據庫中的所有遊樂設施記錄。如何排除細膩的條目?

這裏是Java代碼

public List<RideDTO> recent(long userId, int cursor, int count) { 
     PagedList<RideDTO> pagedList = RideDTO.find 
       .where() 
       .eq("user_id", userId) 
       .orderBy("created_on desc") 
       .findPagedList(cursor, count); 
     if (pagedList == null) 
      return null; 
     else 
      return pagedList.getList(); 
    } 

這裏是RideDTO模型類。我不想在數據庫中保持獨一無二的地位。

@Entity 
@Table(name = "m_ride") 
//@UniqueConstraint(columnNames = {"place"}) 
public class RideDTO extends Model { 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@NotNull 
@Constraints.Required 
private long id; 

@ManyToOne 
@NotNull 
@Constraints.Required 
private UserDTO user; 

@NotNull 
@Constraints.Required 
private String place; 

@NotNull 
@Constraints.Required 
private double startLat; 

@NotNull 
@Constraints.Required 
private double startLng; 

@NotNull 
@Constraints.Required 
private double destLat; 

@NotNull 
@Constraints.Required 
private double destLng; 

private double currLat; 

private double currLng; 

@NotNull 
@Constraints.Required 
private short isFav; 

private String image; 

@NotNull 
@CreatedTimestamp 
@Constraints.Required 
private Date createdOn; 

@Transient 
private double distance; 

public long getId() { 
    return id; 
} 

public void setId(long id) { 
    this.id = id; 
} 

public UserDTO getUser() { 
    return user; 
} 

public void setUser(UserDTO user) { 
    this.user = user; 
} 

public String getPlace() { 
    return place; 
} 

public void setPlace(String place) { 
    this.place = place; 
} 

public double getStartLat() { 
    return startLat; 
} 

public void setStartLat(double startLat) { 
    this.startLat = startLat; 
} 

public double getStartLng() { 
    return startLng; 
} 

public void setStartLng(double startLng) { 
    this.startLng = startLng; 
} 

public double getDestLat() { 
    return destLat; 
} 

public void setDestLat(double destLat) { 
    this.destLat = destLat; 
} 

public double getDestLng() { 
    return destLng; 
} 

public void setDestLng(double destLng) { 
    this.destLng = destLng; 
} 

public double getCurrLat() { 
    return currLat; 
} 

public void setCurrLat(double currLat) { 
    this.currLat = currLat; 
} 

public double getCurrLng() { 
    return currLng; 
} 

public void setCurrLng(double currLng) { 
    this.currLng = currLng; 
} 

public short getIsFav() { 
    return isFav; 
} 

public void setIsFav(short isFav) { 
    this.isFav = isFav; 
} 

public String getImage() { 
    return image; 
} 

public void setImage(String image) { 
    this.image = image; 
} 

public Date getCreatedOn() { 
    return createdOn; 
} 

public void setCreatedOn(Date createdOn) { 
    this.createdOn = createdOn; 
} 

public double getDistance() { 
    return distance; 
} 

public void setDistance(double distance) { 
    this.distance = distance; 
} 

@Override 
public String toString() { 
    return "RideDTO{" + 
      "id=" + id + 
      ", user=" + user + 
      ", place='" + place + '\'' + 
      ", startLat=" + startLat + 
      ", startLng=" + startLng + 
      ", destLat=" + destLat + 
      ", destLng=" + destLng + 
      ", currLat=" + currLat + 
      ", currLng=" + currLng + 
      ", isFav=" + isFav + 
      ", image='" + image + '\'' + 
      ", createdOn=" + createdOn + 
      ", distance=" + distance + 
      '}'; 
} 

public static final Finder<Long, RideDTO> find = new Finder<Long, RideDTO>(Long.class, RideDTO.class); 
} 

回答

1

可以在這裏(我在工作)

有幾個選項不測試代碼,你可以:

  • 做它在數據庫級(我的首選方式,因爲它的速度更快)

  • 電話.stream()方法,並做一些整理

試試這個:

public List<RideDTO> recent(long userId, int cursor, int count) { 
     PagedList<RideDTO> pagedList = RideDTO.find.select("*") 
       .where() 
       .eq("user_id", userId) 
       .orderBy("created_on desc") 
       .setDistinct(true) 
       .findPagedList(cursor, count); 

      return pagedList.getList(); 
    } 

短短几個音符,你不必在Ebean返回null,只是檢查list.isEmpty()在您的視圖。

Ebean不會拋出空指針異常。

編輯: 正如評論指出的,我沒有設置應該區分的值。

當在Ebean中使用setDistinct時,我們必須使用它與選擇

docs

設置此查詢是否使用DISTINCT。 當setDistinct(true)被設置時,必須指定select()子句。原因是通常ORM查詢包括「id」屬性,這對於不同的查詢是沒有意義的。

List<Customer> customers = 
     Ebean.find(Customer.class) 
     .setDistinct(true) 
     .select("name")  // only select the customer name 
     .findList(); 
+0

你錯過了重要的部分,你爲哪一列申請'DISTINCT'。這裏的專欄是'place' – Sivakumar

+0

@Sivakumar,謝謝。 –

+0

謝謝配偶。我會檢查按照這個按照下面的答案,然後會回來給你們 –