我正在使用Play2.0 Framework作为后端API。所以我想列出数据库中的游乐设施,我想要排除具有重复“地点”名称的游乐设施。
我正在使用此代码,但这为我提供了存储在数据库中的所有Rides记录。如何排除假冒入境?
这是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);
}
答案 0 :(得分: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();