我正在编写一个spring boot + Hibernate JPA应用程序,我的hibernate代码不能正常工作,下面是详细信息
实体类:
@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;
}
}
服务/ 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;
}
}
以下是我的表
我收到以下异常
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]
我已经检查了实体类中的表名和列名,所有这些都是正确的,这是一个简单的映射,我也不涉及第二个表。
答案 0 :(得分:0)
试试这个:
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;
}
}
答案 1 :(得分:0)
UserBean模型不是实体对象。因此,您不能将此语句用于执行