0

I've got List propertyIds. And I want to get propertyIds by Collections. This is my code:

Collection<Long> propertyIds = externalTaxManager.getPropertyIdsByTaxId(id);  //Return type must be Collection<Long>

This is my DAOImpl,

public Collection<Long> getPropertyIdsByTaxId(Long externalTaxId) {
    SQLQuery query = currentSession().createSQLQuery("select b.OMH_PROPERTY_ID from OMH_EXTERNAL_TAX a , " +
                "OMH_EXTERNAL_TAX_PROP_XREF b\n" +
                "where a.OMH_EXTERNAL_TAX_ID=b.OMH_EXTERNAL_TAX_ID and a.OMH_EXTERNAL_TAX_ID = :externalTaxId ")
                .addScalar("OMH_PROPERTY_ID" , LongType.INSTANCE);
        query.setParameter("externalTaxId", externalTaxId);
        Collection<BigDecimal> propertyIdsList = (Collection<BigDecimal>) query.list();
    Long val = null;
    Collection<Long> expRes = new HashSet<Long>();
    for(BigDecimal values : propertyIdsList){
       val = values.longValue();
       expRes.add(val);
    }
    return expRes;
}

above Query returns 500+ values in associating with externalTaxId.

Exception thrown is :

java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Long.

What's wrong ?

5
  • 1
    What is the definition of the column OMH_EXTERNAL_TAX_PROP_XREF.OMH_PROPERTY_ID? It is being mapped to BigDecimal somewhere, and that is not castable to Long. Commented May 26, 2016 at 6:08
  • OMH_PROPERTY_ID is NOT NULL NUMBER(38) Commented May 26, 2016 at 6:09
  • 1
    That's too long for a Long, so Hibernate is returning it as a BigDecimal. Change your collection to Collection<BigDecimal>. Do you really need a 38 digit ID? Commented May 26, 2016 at 6:11
  • Actually I need to convert it into a Collection<Long> as I have to pass Collection<Long> value to a dependent system of my project.That application accepts only Collection<Long> value. So is there a way to convert the value? Commented May 26, 2016 at 6:14
  • @yogesh I think you should answer your own question (instead of editing your it) if no one does because fixing the problem in question will confuse people reading it for the first time. Commented May 26, 2016 at 7:03

2 Answers 2

2

look at your expected return type:

Collection<Long>

but the DB returns :

Collection<BigDecimal>

so ,you should change your code to:

Collection<BigDecimal> propertyIdsList =  query.list();

then try to cast BigDecimal to Long in other way;

^_^ forgive my poor English..

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

15 Comments

Actually I need to convert it into a Collection<Long> as I have to pass Collection<Long> value to a dependent system of my project.That application accepts only Collection<Long> value. So is there a way to convert the value?
You should iterate over the returned array and copy to one with the proper type. However, what are you going to do when one of the returned value is too large to fit in a Long?
@JimGarrison Please see the answer I posted . Is it the right way I can proceed
@JimGarrison I have updated DaoImpl, Is it the right way you suggested to do
You must fetch it from the database into a Collection<BigDecimal> and then convert each element to Long.
|
0

Following snippet worked for me as expected.

   public Collection<Long> getPropertyIdsByTaxId(Long externalTaxId) {
        SQLQuery query = currentSession().createSQLQuery("select b.OMH_PROPERTY_ID from OMH_EXTERNAL_TAX a , " +
                "OMH_EXTERNAL_TAX_PROP_XREF b\n" +
                "where a.OMH_EXTERNAL_TAX_ID=b.OMH_EXTERNAL_TAX_ID and a.OMH_EXTERNAL_TAX_ID = :externalTaxId ")
                .addScalar("OMH_PROPERTY_ID", LongType.INSTANCE);
        query.setParameter("externalTaxId", externalTaxId);
        Collection<BigDecimal> propertyIdsList = (Collection<BigDecimal>) query.list();
        Long propertyIdsListValue = null;
        Collection<Long> propertyIds = new HashSet<Long>();
        for (BigDecimal propertyId : propertyIdsList) {
            propertyIdsListValue = propertyId.longValue();
            propertyIds.add(propertyIdsListValue);
        }
        return propertyIds;
    }

1 Comment

Do not post subsequent questions using the Answer box. edit your original post instead. Please copy this to your post and delete this answer.

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.