jasonflaherty
jasonflaherty

Reputation: 1944

Java query db issues. using hibernate and struts2

I am getting into java here. Fun and frustrating all at the same time :)

I have a simple method called showUsernames():

public String showUsernames(){
    TimesheetUserDAO su = new TimesheetUserDAO();
    Session session = su.getSession();
    setUsers(su.findByUsername(_users));
    session.close();
    return SUCCESS;
}

...however, I am having a time getting just the usernames out of the database. It is possible with the Hibernate DAO to get this correct? I am able to use su.findAll() and return everything.

Any thoughts? Need more code? Thanks :)

Upvotes: 0

Views: 192

Answers (1)

JB Nizet
JB Nizet

Reputation: 691625

The DAO probably executee a request like

select u from User u where ...

Change the query to

select u.name from User u where ...

Of course, instead of having a List<User> as a result, you'll have a List<String>.

This is basic stuff described in the Hibernate reference documentation. Have you read it?

Also, getting the session from the DAO and closing it manually like this shows a design problem. This should be encapsulated by the service layer or, even better, by the declarative transaction handling.

Upvotes: 1

Related Questions