java - 由于此异常,休眠映射错误。org.hibernate.hql.internal.ast.QuerySyntaxException
问题描述
我正在尝试通过 'User_Details' 表中的 id 获取用户详细信息。
但是,它给出了例外,我知道这很简单,但我正在尝试使用 HikariCP 连接池。需要帮助来解决。
我在该表中只有 2 列:
- ID
- 姓名
这是实体映射 Pojo:
package com.example.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "USER_DETAILS")
public class UserDetails implements Serializable {
private static final long serialVersionUID = 1L;
//Mapping Unique ID
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", nullable = false)
private int id;
//User Name For Employee
@Column(name = "NAME", nullable = false )
private String name;
// getters and setters
}
映射异常:
> org.hibernate.hql.internal.ast.QuerySyntaxException: UserDetails is
> not mapped at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:171)~[na:1.8.0_131]
从 UserDetailsDaoImpl,我正在尝试获取详细信息。
package com.example.repository;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.example.model.UserDetails;
@Repository
@Transactional
public class UserDaoImpl {
@Autowired
@Qualifier("sessionFactory")
SessionFactory sessionFactory;
public UserDetails getUser(int id){
UserDetails userDetails = null;
Session s = sessionFactory.getCurrentSession();
Query q = s.createQuery("From UserDetails Where id =:id ").setParameter("id", id);
List<UserDetails> u = q.list();
if(!u.isEmpty()){
userDetails = u.get(0);
}
return userDetails!= null? userDetails : null;
}
}
解决方案
推荐阅读
- kubernetes - 为什么请求正文没有显示在 Kubernetes API 服务器审核日志中?
- javascript - 如何将预填充的 React 输入表单中的道具传递回 Express?
- c++ - 如何在 C++ 或 Qt 中获取所有货币符号和货币缩写的数组?
- javascript - 终端无法识别 nodemon 命令
- unity3d - 将着色器中的 float2/vector2 属性公开给 Unity 材质检查器
- python - 如何在训练测试拆分后仅标准化 int64 列?
- python - 当我试图在 python 中更改矩阵的第一行的值时,所有行的值都发生了变化
- c++ - 调用 NtCreateProcessEx 失败无异常
- android - 应用程序崩溃:方法抛出“com.google.firebase.components.MissingDependencyException”异常
- swift - Xcodebuild 不运行作为 XCTestCase 子类的子类的测试