Coder
Coder

Reputation: 7731

For loop from Hibernate Entity not returning results as expected

I have an entity defined in Hibernate 4.3.5 and I am trying to get the values in the controller so that I could display it in the JSON response. But my for loop contents are printed in some different manner as shown below :

Result of System.out.println(emp.toString()+"\n"); in the code :

abc.cde.myproject.orm.Employee@599eeded

abc.cde.myproject.orm.Employee@75ffa715

abc.cde.myproject.orm.Employee@152beb23

abc.cde.myproject.orm.Employee@1f2f7ce1

abc.cde.myproject.orm.Employee@484ca3df

Here's my controller code where I am using the loop:

@RequestMapping(value="/get_employee_details", method=RequestMethod.GET)
    public String updateemployee
    (
            @RequestParam(value="emp_id", defaultValue="0") Integer emp_id,
            @RequestParam(value="name", defaultValue="") String name
    ) 
    {
        String responseJSON = null;
        boolean getStatus = true;       
        try {
            EmployeeDao employeeDao = (EmployeeDao)context.getBean("employeeDao");
            Employee employee = null;           
            List<Employee> empList = employeeDao.findByEmployeeId(emp_id);
            if ((empList != null) && (!empList.isEmpty())) {


                for(Employee emp : empList){
                System.out.println(emp.toString()+"\n");
            }

            if (getStatus) {
                    responseJSON = GenericOrmStatusView.OrmResponseToJsonString(true, 1,"List of Names here", true);
                } else {
                    responseJSON = GenericOrmStatusView.OrmStatusToJsonString(false, "Update of EMPLOYEE Record Problem!", true);
                }
            }                       
        } catch (Throwable th) {
            th.printStackTrace();
            responseJSON = GenericOrmStatusView.OrmStatusToJsonString(false, th.getMessage(), true);
        }

        return responseJSON;
    }

My Employee entity is as follows :

@Entity
@Table(name="EMPLOYEE") 
public class Employee 
{       
    public int getEmployeeId() {
        return EmployeeId;
    }

    public void setEmployeeId(int EmployeeId) {
        this.EmployeeId = EmployeeId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    @Id
    @Column(name="Employee_ID")
    @GeneratedValue(strategy=GenerationType.AUTO, generator="seqgen")
    @SequenceGenerator(name="seqgen", sequenceName="Employee_AUTOINC_SEQ")
    private int EmployeeId; 

    @Column(name="NAME")
    private String name;



}

And this is how the findByEmployeeId method in DAO is defined :

public List<Employee> findByEmployeeId(int empID);

Since I have hardcoded the ID and string content here :

responseJSON = GenericOrmStatusView.OrmStatusToJsonString(true, 1,"List of Names here", true);

I can see the following JSON getting printed right now:

{
  "status" : "SUCCESS",
  "workflowStatusID" : 1,
  "workflowStatus" : "List of Names here"
}

However, instead of hardcoding, I want to pass all the list of IDs and names so that I can display everything in the JSON response. Before I do this, I would need to retrieve the results properly using the for loop.

What am I doing wrong in the for loop in the controller code above?

Thanks

Upvotes: 3

Views: 145

Answers (2)

GolamMazid Sajib
GolamMazid Sajib

Reputation: 9447

If you use toString() in object without override toString() method then there will be print

<class name of object that you are creating>@<hashcode value in hexadecimal format returned by the hashCode() method of Object class>.

you can get details about toString() here

print like this:

System.out.println(new ObjectMapper().writeValueAsString(emp));

Upvotes: 1

Mart&#237;n Zaragoza
Mart&#237;n Zaragoza

Reputation: 1817

You must override toString in your Employee class in order to print your Employee instances the way you want (using println for example).

@Override
public String toString() {
Gson gson = new Gson();
String json = gson.toJson(this);
return json;
}

You can use libraries such as Gson in order to format POJOs as json strings

hope this helps

Upvotes: 1

Related Questions