简体   繁体   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

I've got an issue with the select function of the Finder object by using ebean with Play! 通过将ebean与Play一起使用, Finder对象的select函数遇到了问题! Framework (2.2.1). 框架(2.2.1)。

I've got my table AnneePromotion : 我有桌子AnneePromotion:

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

My entity AnneePromotion : 我的实体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);

}

And when I try to use the select function in order to just have libelle : 当我尝试使用select函数以具有libelle时:

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

I received my objects with id and libelle columns : 我收到了带有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

I don't know with the select function doesn't work, if I made a stupid mistake or not :/ 我不知道选择功能不起作用,如果我犯了一个愚蠢的错误:/

Hope you could help me. 希望你能帮助我。

Regards, Anthony. 问候,安东尼。

That's common behavior of Ebean, reason is simple: you are asking for list of objects of concrete type, so it can't just return list of strings (without id ) as it need to identify rows somehow. 这是Ebean的常见行为,原因很简单:您要查询具体类型的对象列表,因此它不能仅仅返回字符串列表(无id ),因为它需要以某种方式标识行。

The simplest solution is rewriting it to new list ie. 最简单的解决方案是将其重写为新列表。

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

Optionally you can also use Ebean's SqlQuery and then iterate through the list of SqlRow to get the same libelles list. (可选)您还可以使用Ebean的SqlQuery ,然后遍历SqlRow的列表以获取相同的libelles列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM