首页 > 解决方案 > 由于此异常,休眠映射错误。org.hibernate.hql.internal.ast.QuerySyntaxException

问题描述

我正在尝试通过 'User_Details' 表中的 id 获取用户详细信息。

但是,它给出了例外,我知道这很简单,但我正在尝试使用 HikariCP 连接池。需要帮助来解决。

我在该表中只有 2 列:

  1. ID
  2. 姓名

这是实体映射 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;
    }
  }

标签: javahibernatespring-boothikaricp

解决方案


推荐阅读