hibernate 5 org.hibernate.hql.internal.ast.QuerySyntaxException: Emplooye 未映射

hibernate 5 org.hibernate.hql.internal.ast.QuerySyntaxException: Emplooye is not mapped

我将我的应用程序从 hibernate 4 迁移到 5,现在我可以看到 Query 已被弃用。

我 运行 我的代码与现有的 HQL 实现。它在运行时失败。

原因:org.hibernate.hql.internal.ast.QuerySyntaxException:员工未映射。

Query query = session.createQuery("SELECT employee FROM Employee employee WHERE a = ? and b= ?");
      query = query.setParameter(0, a);
      query = query.setParameter(1, b);
      List<Employee > resultList = query.list();

谁能帮我解决这个问题。 我试图删除已弃用的 Query 接口并与 Query 一起使用。 但这并不能解决问题。

public class HibernateUtil {

    private static Logger logger = LoggerFactory.getLogger(HibernateUtil.class);

    private static SessionFactory sessionFactory;
    private static Properties aProp = new Properties();

    static {
        try (InputStream is = ClassLoader.class.getResourceAsStream("/"
                + CrasConstants.PROPERTIES_PATH);) {
            aProp.load(is);
            String filepath = aProp
                    .getProperty(CrasConstants.HIBERNATE_PROPERTIES_PATH);
            String path = FilenameUtils.normalize(filepath);
            try (InputStream hibernatePropertyfile = new FileInputStream(path
                    + "hibernate.properties")) {
                aProp.load(hibernatePropertyfile);
            }
            // configuration for encryption/decryption
            Configuration configuration = new Configuration();
            configuration.configure("hibernate.cfg.xml")
                    .addProperties(aProp);

            StandardPBEStringEncryptor strongEncryptor = new StandardPBEStringEncryptor();
            strongEncryptor.setProvider(new BouncyCastleProvider());
            strongEncryptor.setAlgorithm("PBEWITHSHA256AND256BITAES-CBC-BC");
            strongEncryptor.setPassword("pwd");
            HibernatePBEEncryptorRegistry registry = HibernatePBEEncryptorRegistry
                    .getInstance();
            registry.registerPBEStringEncryptor(
                    "configurationHibernateEncryptor", strongEncryptor);
            PBEStringEncryptor encryptor = registry
                    .getPBEStringEncryptor("configurationHibernateEncryptor");
            configuration.setProperty("hibernate.connection.password",
                    encryptor.decrypt(configuration
                            .getProperty("hibernate.connection.password")));

            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();

            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (HibernateException | IOException e) {
            logger.error(e);
            System.exit(-1);
        }
        logger.info("HibernateUtil: Sessionfactory Initialized!!! ");

    }

    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
    
Session session = null;
    Transaction tx = null;
    List<Employee> requestResultList =
        new ArrayList<>();
    try {
      SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
      session = sessionFactory.getCurrentSession();
      tx = session.beginTransaction();
      Query query = session.createQuery("SELECT d FROM com.skf.model.Employee 
   d where d.name=:name");
      List<Employee> resultList = query.list();




Employee 
@Data
@Entity
@Table(name = "EMPLOYEE")
public class Employee {

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EMPLOYEE_SEQ")
  @SequenceGenerator(name = "EMPLOYEE_SEQ", sequenceName = "EMPLOYEE_SEQ",allocationSize = 1)
  @Column(name = "EMP_ID")
  private long empId;
  @Column(name = "name", nullable = false)
  private String name;
}

休眠 cfg 文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
            <!-- Database connection settings -->
        <property name="connection.provider_class">
            net.lizalab.util.jasypt.h4.ext.connectionprovider.EncryptedDriverManagerConnectionProviderImpl
        </property>
        <property name="connection.encryptor_registered_name">configurationHibernateEncryptor</property>
        
        <!-- <property name="hibernate.connection.datasource">java:comp/env/jdbc/cras</property> -->
        <property name="connection.driver_class">${hibernate.connection.driver_class}</property>    
        <property name="connection.url">${hibernate.connection.url}</property>
        <property name="connection.username">${hibernate.connection.username}</property>
        <property name="connection.password">${hibernate.connection.password}</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

        <!-- jdbc driver hint for number of fetched rows for select statement -->
        <property name="hibernate.jdbc.fetch_size">1000</property>

        <!-- improve app startup performance -->
        <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property> 

        <property name="hibernate.cache.use_second_level_cache">false</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="hibernate.current_session_context_class">thread</property>

        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <property name="hbm2ddl.auto">validate</property>
        
        <mapping class="com.skf.model.Employee" />
        
    </session-factory>
</hibernate-configuration>

假设您有 Employee 实体,您可以通过以下方式更正您的查询:

List<Employee> resultList = session.createQuery(
   "SELECT e FROM Employee e WHERE e.a = :a and e.b= :b",
   Employee.class
)
.setParameter("a", a)
.setParameter("b", b)
.getResultList();

请检查您的员工实体映射,

如果您的员工实体映射如下:

@Table(name="EMPLOYEE_TABLE") // table name
@Entity(name = "employee") // entity name
public class Employee

那么您的查询应该是:

Query query = session.createQuery("SELECT e FROM employee e WHERE e.a = ? and e.b= ?");

如果您的员工实体映射如下:

@Table(name="EMPLOYEE_TABLE") // table name
@Entity(name = "emp") // entity name
public class Employee

那么您的查询应该是:

Query query = session.createQuery("SELECT e FROM emp e WHERE e.a = ? and e.b= ?");

如果您的员工实体映射如下:

@Table(name="EMPLOYEE_TABLE") // table name
@Entity // default entity name is class name
public class EmployeeEntity

那么您的查询应该是:

Query query = session.createQuery("SELECT e FROM EmployeeEntity e WHERE e.a = ? and e.b= ?");

供参考:org.hibernate.hql.internal.ast.querysyntaxexception entity/table is not mapped