簡體   English   中英

播放!2.2.1 Java Ebean-Finder.select(字符串列)不起作用:所有列均已選中

[英]Play!2.2.1 Java Ebean - Finder.select(String columns) doesn't work : all the columns are selected

通過將ebean與Play一起使用, Finder對象的select函數遇到了問題! 框架(2.2.1)。

我有桌子AnneePromotion:

CREATE TABLE AnneePromotion (
  anneePromotion_ID INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('AnneePromotionSequence'),
  anneePromotion_libelle INTEGER NOT NULL 
);

我的實體AnneePromotion:

@Entity
@Table(name = "AnneePromotion")
@SequenceGenerator(name = "AnneePromotionSequenceGenerator", sequenceName =         "AnneePromotionSequence")
public class AnneePromotion extends Model {

    /** serial ID */
    private static final long serialVersionUID = -2072489268439045171L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AnneePromotionSequenceGenerator")
    @Column(name = "anneePromotion_ID")
    private Integer id;

    @Column(name = "anneePromotion_libelle")
    private String libelle;

    public Integer getId() {
        return id;
    }

    public String getLibelle() {
        return libelle;
    }

    public void setLibelle(String libelle) {
        this.libelle = libelle;
    }

    public static Finder<Integer, AnneePromotion> find = new Finder<Integer, AnneePromotion>(
        Integer.class, AnneePromotion.class);

}

當我嘗試使用select函數以具有libelle時:

    List<AnneePromotion> listeDesAnneesdePromotion = AnneePromotionDao.find
            .select("libelle").orderBy("libelle asc").findList();
    for (AnneePromotion anneepromotion : listeDesAnneesdePromotion){
        System.out.println(anneepromotion.getId()+" "+anneepromotion.getLibelle());
    }

我收到了帶有id和libelle列的對象:

1 2003
2 2004
3 2005
4 2006
5 2007
6 2008
7 2009
8 2010
9 2011
10 2012
11 2013
12 2014
13 2015
14 2016

我不知道選擇功能不起作用,如果我犯了一個愚蠢的錯誤:/

希望你能幫助我。

問候,安東尼。

這是Ebean的常見行為,原因很簡單:您要查詢具體類型的對象列表,因此它不能僅僅返回字符串列表(無id ),因為它需要以某種方式標識行。

最簡單的解決方案是將其重寫為新列表。

List<String> libelles = new ArrayList<>();
for (AnneePromotion anneepromotion : listeDesAnneesdePromotion){
    libelles.add(anneepromotion.getLibelle());
}

(可選)您還可以使用Ebean的SqlQuery ,然后遍歷SqlRow的列表以獲取相同的libelles列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM