W. Praneeth Madusanka
W. Praneeth Madusanka

Reputation: 137

Hibernate SQL Error: 1064, SQLState: 42000

My hibernate query is like that.

@Override
    public List<Userdata> getUserDataWithUserType(String userName) {
        List<Userdata> africanContinents = getSession().createSQLQuery(
                "from userdata cont join cont.usertype ctry "
                + "where cont.userName = '" + userName + "")
                .list();
        return africanContinents;
    }

But when my method is running the system will give error message

SQL Error: 1064, SQLState: 42000
ERROR:   You have an error in your SQL syntax;

How I solve that. ***Above code used for create join operation for two tables. ** myuserdata class is

@Entity
@Table(name="userdata")
public class Userdata  implements java.io.Serializable {
     private Integer userId;
     private Usertype usertype;
     private String firstName;
     private String lastName;
     private String userName;
     private String passWord;
     private String email;
     private String tleNo;
     private Date birthDay;
     private String department;
     private String image;
     private Boolean checkBy;

    public Userdata() {
    }


    public Userdata(Usertype usertype) {
        this.usertype = usertype;
    }


     //get set method
}

Upvotes: 0

Views: 1537

Answers (3)

RoshanS
RoshanS

Reputation: 129

I also got that error and issue was , We have used name "key" as a column name . So what we did is in our entity class we have used "`" with the column name "key" like this

@Column(name = "`key`")
private String  key;

Upvotes: 0

Vikrant Kashyap
Vikrant Kashyap

Reputation: 6836

try this. Explanation will be added soon.

Query q = getSession().createQuery(
                "from userdata cont cont.usertype where cont.userName =: userName");
q.setParameter("userName","kumar");  

 List<Userdata> africanContinents  = query.list();

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520878

When you use createSQLQuery() you are executing a raw SQL query on the underlying database. However, the syntax you are using appears to be HQL, not raw SQL, so my guess is that you intended to use createQuery() instead of createSQLQuery(). Try this code:

@Override
public List<Userdata> getUserDataWithUserType(String userName) {
    List<Userdata> africanContinents = getSession().createQuery(
        "from Userdata cont where cont.userName = :userName")
       .setParameter("userName", userName)
       .list();
    return africanContinents;
}

You will notice that I also used setParameter to set the parameter in the WHERE clause. This is a precaution against SQL injection, and you should review the documentation on this.

Upvotes: 1

Related Questions