简体   繁体   中英

String Builder Enum checking

I have collection table in database which have fields:

published,name,descrption and collection_type.

And this collection_type can be three different String values : Collection,Trend,Occasion .

Showing whole elements with this working.

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

But showing specific elements for example Occasions failed. How to fix the code to show the elements?

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;
    }           
}

Suppose, the problem is, that you don't set a ENUM's name, but a "CollectionType.OCCASION.getName()'" string. Try to change your code to:

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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