2015-09-09 85 views
0

我在其中有场数据库采集表:字符串构建枚举检查

published,name,descrption and collection_type. 

collection_type可以有三种不同的字符串值:Collection,Trend,Occasion

显示与此工作的整个元素。

public List<Collection> list() { 
    QueryParams queryParams = new QueryParams(); 
    queryParams.setWhere("published = true"); 
    return list(queryParams); 
} 

但显示特定元素例如场合失败。如何修复代码以显示元素?

public List<Collection> occasions() { 
    QueryParams queryParams = new QueryParams(); 
    final StringBuilder sb = new StringBuilder(); 
    sb.append("published = true "); 
    sb.append("AND collection_type = '"); 
    sb.append("CollectionType.OCCASION.getName()'"); 
    queryParams.setWhere(sb.toString());  
    return list(queryParams); 
}   

public enum CollectionType {   
    COLLECTION("COLLECTION"), TREND("TREND"), OCCASION("OCCASION");   
    private String name;   
    private CollectionType(String name) { 
     this.name = name; 
    }   
    public String getName() { 
     return name; 
    }   
} 
+2

我不能回答你的问题,因为我不知道你在做什么,但我知道'Enum'已经有一个方法'name',但这正是你的'getName'方法做了什么。所有你需要的是'public enum CollectionType {COLLECTION,TREND,OCCASION}' –

+1

当你说'sb.append(“CollectionType.OCCASION.getName()'”);',你的意思是'sb.append(CollectionType.OCCASION .getName()); sb.append(“'”)'?或'sb.append(“OCCASION'”);' –

+0

我有数据库中的元素列表。这个数据库中的表包含collection_type(字符串),名称,描述等。我想显示元素的collection_type例如=“OCCASION”; – waaaas

回答

0

假设,问题是,你没有设置ENUM的名字,而是一个“CollectionType.OCCASION.getName()'”的字符串。尝试更改您的代码:

public List<Collection> occasions() { 
    QueryParams queryParams = new QueryParams(); 
    final StringBuilder sb = new StringBuilder(); 
    sb.append("published = true "); 
    sb.append("AND collection_type = '"); 
    sb.append(CollectionType.OCCASION.getName()); 
    sb.append("'"); 
    queryParams.setWhere(sb.toString()); 

    return list(queryParams); 
}