1

i want to ask some question, how to get specific value from list and add to another list, lets say i have list (this is cust list from hibernate DAO) like:

[["marketplace","001-002-003"],["insurance","142-523-132"],["car purchase","982349824"]]

i just want to get a value of "marketplace","insurance", and "car purchase" from that list and add to new list called "bu"

here is my code

public @ResponseBody String findBU(@RequestBody AllCustomerHist customer){
        BigDecimal id= customer.getId();
        String message;
        List<String> bu= new ArrayList<>();
        int i;

        System.out.println("ID = "+id);
        List<AllCustomerHist> cust = allCustomerHistService.findBU(id);


        for (i=0; i<cust.size(); i++){
            System.out.println("iteration = "+i);

            // stumbled here //
        }

        JSONObject json = new JSONObject();
        json.put("id", id);
        json.put("BU", bu);

        message = json.toString();
        return message;

    }

this is my AllCustomerHistDaoImpl class

//release 1.3
@SuppressWarnings("unchecked")
public List<AllCustomerHist> findBU(BigDecimal adpId) {
    // TODO cek kodingan
    Criteria criteria = getSession().createCriteria(AllCustomerHist.class)
            .setProjection(Projections.projectionList()
                    .add(Projections.property("srctable"), "srctable")
                    .add(Projections.property("customerId"), "customerId"))
    .add(Restrictions.eq("adpId", adpId));

    return (List<AllCustomerHist>)criteria.list();
}

Notice that AllCustomerHist is an entity class to define the table in hibernate

thank you for your helps :D

11
  • What's the structure of AllCustomerHist? More importantly, why do assume we would know? Commented Aug 25, 2017 at 3:14
  • Why are you using ID as BigDecimal? Commented Aug 25, 2017 at 3:16
  • You have shown the cust list from Hibernate in a way it looks like a Map. Then you're using a list with AllCustomerHist type elements. It seems it's not compatible. Commented Aug 25, 2017 at 3:18
  • @PauloPedroso the result of cust is not a pair of key:value, its result set from 2 column, first is srctable column ("marketplace, insurance, car purchase" is come from here) and the second is from customerID column Commented Aug 25, 2017 at 3:26
  • @galih maybe you should post the AllCustomerHist code. Commented Aug 25, 2017 at 3:31

2 Answers 2

1

Since you need to make some validation and you need to take off the whole AllCustomerHistobject what I would do is the following code

List<AllCustomerHist> cust = allCustomerHistService.findBU(id);
List<String> bu = new ArrayList<String>(cust.size());

        for (i=0; i<cust.size(); i++){
            System.out.println("iteration = "+i);
            AllCustomerHist aCust = cust.get(i);
            bu.add(aCust.getSrctable());

        }
//here your bu list should be ready to be used.....

I hope it's what you needed

Sign up to request clarification or add additional context in comments.

2 Comments

it cannot casted from object to allcustomerhist, your code is correct, but i need to transform my resultset using setResultTransformer(Transformers.aliasToBean(AllCustomerHis‌​t.class))
Ah so the AllCustomerHist was not the entity class.... I understood it was the entity class otherwise I would have told you to use the hibernate transformer. Anyway I'm glad it works
0

If you use JDK1.8+, you can also do it like this:

List<AllCustomerHist> cust = allCustomerHistService.findBU(id);
List<String> bu = cust.stream()
    .map(AllCustomerHist::getSrctable)
    .collect(Collectors.toList());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.