0

I have a situation where I am trying to compare an input String value against a value in my List which is a List of ForecastTypes (my entity). The table I am hitting doesn't have a primary key, and that is out of my control, so I am trying to establish a unique key via the row number functionality with oracledb. When I run my program and compare my input String value to the List which holds the values retrieved via my entity, when they match (ex - String input value is THANKSGIVING and value in my ForecastTypes list is THANKSGIVING) it still returns false instead of true and populates an error instead of returning as true. I am trying to compare the input String value "value" to the "forecastName" column name specified in my ForecastTypes entity. I am not sure what I am doing wrong here, as I am using the contains() method to see if my forecastList contains the String value which is my input value. Any recommendations? I am using Java 8 EE, Spring, JPA, and Hibernate. I have other methods that works properly using this same pattern in my application, I am just trying to drilldown to this particular functionality alone and have removed those for futher readability. Any helps would be appreciated. Below is my code and in comments will tell you what functionality I am trying to achieve as well as the classes they belong to, etc.

//global

fcstList = OracleImpl.forecastIDList();

//boolean check in my Validator.java class

public static boolean isForecastIDValid(String value, List<ForecastTypes> fcstList) {
    value = Util.getTrimmedValue(value);
    forecastList = fcstList;
    if (forecastList.contains(value)) {
        return true;
    }
    return false;
}

//method that adds error for invalid forecast ID in Validator class

fc.forEach(fc -> {

    if (fc.getErrorList().size() == 0) {        
        if (!isForecastIDValid(fc.getForecast_ID(), fcstList)) {
            fc.getErrorList().add(Constants.FORECAST_ID_INVALID);
        }
    }
});

//query property from my query.properties file

isForecastIDValid = SELECT ROW_NUMBER() OVER( ORDER BY ID DESC ) row_num, ID, TYPE FROM SCHEMA.FCSTIDS

//My entity:

package com.lance.db.oracle.entity;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Column;

@Entity
public class ForecastTypes {
    @Id
    @Column(name="row_num")
    String rowNumber;

    public String getRowNumber() {
        return rowNumber;
    }
    public void setRowNumber(String rowNumber) {
        this.rowNumber = rowNumber;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }

    @Column(name="TYPE")
    String type;

    @Column(name="ID")
    String forecastName;

    public String getForecastName() {
        return forecastName;
    }
    public void setForecastName(String forecastName) {
        this.forecastName = forecastName;
    }
}

//Oracle entitymanagerfactory execution in OracleImpl class

package com.lance.db.oracle;

import java.math.BigDecimal;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;

import com.lance.db.oracle.entity.ForecastTypes;


@Configuration
@PropertySources({ @PropertySource("classpath:sql/query.properties") })
public class OracleImpl {
    private static Logger logger = LogManager.getLogger(OracleImpl.class);
    private static EntityManagerFactory emf;
    private static Environment env;

    public static void setMyConfig(EntityManagerFactory emf, Environment env) {
        OracleImpl.emf = emf;
        OracleImpl.env = env;       
    }

    public static List<ForecastTypes> forecastIDList() {
        EntityManager em = emf.createEntityManager();
    
        Query q = em.createNativeQuery(env.getProperty("isForecastIDValid"), ForecastTypes.class);
    
        List<ForecastTypes> res = q.getResultList();
    
        em.clear();
    
        em.close();
        return res;
    }
}

1 Answer 1

1

The problem is in your Validator.java class. You are comparing a String object with a ForecastTypes object and this can never match.
In other words your List of ForecastTypes can never contain (".contains") a String object, because it is a List of ForecastTypes (List).

It should work like this - by comparing String with String:

public static boolean isForecastIDValid(String value, List<ForecastTypes> fcstList) {
    value = Util.getTrimmedValue(value);
    for (ForecastTypes ft : fcstList) {
        if (ft.getForecastName().equals(value))
            return true;
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

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.