简体   繁体   中英

sql query problems with hibernate

I am simply trying to load my user from my user table via the username and password. However the properties of the user are not getting initialized. I have set fetch="join" (eager loading) in my mapping files. Here is my query:

SQLQuery q = session.createQuery("SELECT u.* FROM users WHERE username=? AND password=?);
q.setString(0, "valid_username");
q.setString(0, "valid_password");

List users = q.list();

The various properties of my users such as its department, user level, etc.. is not getting initialized properly.

My second query: I am simply trying to get the list of tickets from the db using the same method

SQLQuery q = session.createQuery("SELECT t.* FROM tickets WHERE assignee_id=?);
    q.setString(0, "valid_assignee_id");

    List users = q.list();

Same thing is happening... the fields of my ticket such as its status, priority, etc.. are not getting initialized. Once again i have the fetch=join property set for the necessary mapping files. Can anyone point me in the right direction, thanks!!

Edit: Below is my database structure and pojo files for reference"

   users table
    -----------
    u_id
    username
    password
    salt
    email
    firstName
    lastName
    department_id
    userlevel_id

    tickets table
    -------------
    ticket_id
    title
    description
    department_id 
    status_id
    priority_id
    assignee_id
    creator_id

public class Ticket implements java.io.Serializable {

    private Integer tickId;
    private Department department;
    private Status status;
    private User creatorUser;
    private User assignedUser;
    private Priority priority;
    private String title;
    private String description;
    private Set ticketNotes = new HashSet(0);
    private Set attachments = new HashSet(0);

//Getters and setters
}

public class User  implements java.io.Serializable {


     private Integer UId;
     private Department department;
     private Userlevel userlevel;
     private String username;
     private String password;
     private String salt;
     private String email;
     private String firstName;
     private String lastName;
     private Set createdTickets = new HashSet(0);
     private Set assignedTickets = new HashSet(0);

    public User() {
    }

Note im using the hibernate .hbm.xml mapping structure and not annotations.

I think, these all are correct queries.you have to define your objects(u,t) like this in the query. it will fetch all the columns in the table

SELECT u FROM users u WHERE u.username=? AND u.password=?

and

SELECT t FROM tickets t WHERE t.assignee_id=?

Yes, i agree with kartik. The parameter values should be in the correct position

q.setString(0, "valid_username");

q.setString(1, "valid_password");

this also needs.

Also need to provide correct position of the parameters in the query. You have set value of both the parameter to 0 position. That is the reason why its not getting proper data.

q.setString(0, "valid_username");

q.setString(1, "valid_password");

A few problems with your queries:

  1. Your are missing closing quotes at the end of your queries

     SQLQuery q = session.createQuery("SELECT u.* FROM users WHERE username=? AND password=?); 

    should be

     SQLQuery q = session.createQuery("SELECT u.* FROM users WHERE username=? AND password=?"); 
  2. You want to retrieve all columns from aliases u and r but you never define them in your queries, should be:

     SELECT u.* FROM users u WHERE username=? AND password=? 

    or

     SELECT * FROM users WHERE username=? AND password=? 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM