0

I am writing a spring boot + Hibernate JPA application and i my hibernate code is not working in that below are the details

Entity Class:

@Entity
@Table(name="user")
public class UserBean {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "idUser")
    String id;

    @Column(name = "UserName")
    String name;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Service/DAO:

@Transactional
@Repository
public class SpringBootService {

    @PersistenceContext 
    private EntityManager entityManager;

    public boolean userLogin(UserBean bean) {

        String hql = "FROM UserBean WHERE id = ? ";
        int count = entityManager.createQuery(hql).setParameter(0, bean.getId())
                .getResultList().size();
        return count > 0 ? true : false;
    }

}

and below is my table

enter image description here

I am getting the below exception

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'userbean0_.id_user' in 'field list'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_162]

I have checked the table name and the column names in the entity class all of then are correct and this is a simple mapping i am not involving second table as well.

3
  • I see your IdUser column is int whereas its defined as String in model class. Commented May 6, 2018 at 4:42
  • Will that be a problem ? that works in hibernate xml configaration. Commented May 6, 2018 at 4:44
  • i have changed it to integer and tried and i am getting the same issue Commented May 6, 2018 at 4:46

2 Answers 2

0

try with this:

String hql = "FROM UserBean WHERE id = ?1 ";
    int count = entityManager.createQuery(hql).setParameter(1, bean.getId())
            .getResultList().size();

EntityClass:

import javax.persistence.*;
@Entity
@Table(name="user")
public class UserBean {

@Id
@Column(name = "idUser")
String id;

@Column(name = "UserName")
String name;

public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

}

Sign up to request clarification or add additional context in comments.

7 Comments

You declare primary key as string. Remove Generated value to get string type id.
i have updated that as integer and i also tried by removing generator it did not work getting the same error
i used above enity class. its work for me. give ur stacktrace
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'userbean0_.id_user' in 'field list' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_162] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_162] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_162]
pls check my given entiry class & ur class and "id = ?1 ". don't miss 1 after '?'.
|
0

The UserBean model is not entity object. So you cannot use this statement for execute

1 Comment

i have declared entity annotation for the class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.