我正在尝试通过ID从“ User_Details”表中获取用户详细信息。
但是,它给出了异常,我知道这很简单,但是我正在使用HikariCP Connection pool尝试。需要帮助来解决。
该表中只有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;
}
}