2

I have Condition entity and have one attribute ConditionType who have two enums, medical and behavioral, that I want to set after getting CONDITION_NAME from SQL Server.

@SuppressWarnings("rawtypes")
private List<Condition> getConditions(String patientId, Date runDate) {
    // TODO

    JdbcTemplate template = new JdbcTemplate(dataSource);  
    String sql = "select patient_id,condition_name from patient_condition, cpl_manage_condition"
    + " where patient_condition.condition_id = cpl_manage_condition.condition_id";

    List<Map<String, Object>> rows = template.queryForList(sql);
    List<Condition> conditions = new ArrayList<Condition>();

    for (Map row: rows) {
        Condition condition = new Condition();

        //getting error
        condition.setType((ConditionType)((String)row.get("CONDITION_NAME")));

        conditions.add(condition);
    }

    return conditions;
}
informatik01
  • 16,038
  • 10
  • 74
  • 104
user3865046
  • 59
  • 3
  • 9

2 Answers2

3

I'm assuming your error is

condition.setType((ConditionType)((String)row.get("CONDITION_NAME")));

And should be

condition.setType(ConditionType.valueOf(row.get("CONDITION_NAME").toString()));

See also this question.

Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • how to set in separately in medical and behavioral condition public enum ConditionType { MEDICAL, BEHAVIORAL } – user3865046 Jul 22 '14 at 14:56
  • @user3865046 Why did you post that as a comment here? You can use the String(s) "MEDICAL" and "BEHAVIORAL" with my answer above. – Elliott Frisch Jul 22 '14 at 14:58
  • i m new here don't know where to post ....i m getting error just like different conditions like asthma, diabetic etc //error// No enum constant com.mp.model.ConditionType.Asthama // i m using couchbase db (no sql db) – user3865046 Jul 22 '14 at 15:24
  • Sounds like you need to delete your version of `ConditionType` and use the version from `com.mp.model.ConditionType`. If that's you, then your enum is missing valid values. It's not a class. You might need one. – Elliott Frisch Jul 22 '14 at 15:26
2

If ConditionType is an enum, you can use ConditionType.valueOf(String) to convert a String value to that type. Notice you should take care of null and invalid arguments then.

Didier L
  • 18,905
  • 10
  • 61
  • 103