0

I am running a Spring Boot application with a PostConstruct method to populate a POJO before application initialization. This is to ensure that the database isn't hit by multiple requests to get the POJO content after it starts running.

I'm able to pull the data from Oracle database through Hibernate query and store it in my POJO. The problem arises when I try to access the stored data. The dataset contains a list of objects that contain strings and numbers. Just trying to print the description of the object at the top of the list raises a class cast exception. How should I mitigate this issue?

@Autowired
private TaskDescrBean taskBean;
@PostConstruct
public void loadDescriptions() {

TaskDataLoader taskData = new TaskDataLoader(taskBean.acquireDataSourceParams());
List<TaskDescription> taskList = tdf.getTaskDescription();
taskBean.setTaskDescriptionList(taskList);
System.out.println("Task description size: " + taskBean.getTaskDescriptionList().get(0).getTaskDescription());
}

My POJO class:

@Component
public class TaskDescrBean implements ApplicationContextAware {

@Resource
private Environment environment;
protected List<TaskDescription> taskDescriptionList;

public Properties acquireDataSourceParams() {

    Properties dataSource = new Properties();
    dataSource.setProperty("hibernate.connection.driver_class", environment.getProperty("spring.datasource.driver-class-name"));
    dataSource.setProperty("hibernate.connection.url", environment.getProperty("spring.datasource.url"));
    dataSource.setProperty("hibernate.connection.username", environment.getProperty("spring.datasource.username"));
    dataSource.setProperty("hibernate.connection.password", environment.getProperty("spring.datasource.password"));
    return dataSource;
}


public List<TaskDescription> getTaskDescriptionList() {
    return taskDescriptionList;
}


public void setTaskDescriptionList(List<TaskDescription> taskDescriptionList) {
    this.taskDescriptionList = taskDescriptionList;
}

public ApplicationContext getApplicationContext() {
    return applicationContext;
}


public void setApplicationContext(ApplicationContext applicationContext) {
    this.applicationContext = applicationContext;
}
}

My DAO class:

public class TaskDataLoader {

private Session session;
private SessionFactory sessionFactory;

public TaskDataLoader(Properties connectionProperties) {

    Configuration config = new Configuration().setProperties(connectionProperties);
    config.addAnnotatedClass(TaskDescription.class);
    sessionFactory = config.buildSessionFactory();
}

@SuppressWarnings("unchecked")
public List<TaskDescription> getTaskDescription() {

    List<TaskDescription> taskList = null;
    session = sessionFactory.openSession();
    try {

        String description = "from TaskDescription des";
        Query taskDescriptionQuery = session.createQuery(description);
        taskList = taskDescriptionQuery.list();
        System.out.println("Task description fetched. " + taskList.getClass());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.close();
    }
    return taskList;
}

TaskDescription Entity:

@Entity
@Table(name="TASK_DESCRIPTION")

@JsonIgnoreProperties
public class TaskDescription implements Serializable {
    private static final long serialVersionUID = 1L;

@Id
@Column(name="TASK_DESCRIPTION_ID")
private Long taskDescriptionId;

@Column(name="TASK_DESCRIPTION")
private String taskDescription;

public Long getTaskDescriptionId() {
    return taskDescriptionId;
}

public void setTaskDescriptionId(Long taskDescriptionId) {
    this.taskDescriptionId = taskDescriptionId;
}    

public String getTaskDescription() {
    return taskDescription;
}

public void setTaskDescription(String taskDescription) {
    this.taskDescription = taskDescription;
}

}

StackTrace

Mano
  • 1
  • 3

1 Answers1

0

Instead of sending the List in the return statement, I transformed it into a JSON object and sent its String representation which I mapped back to the Object after transforming it using mapper.readValue()

Mano
  • 1
  • 3