简体   繁体   English

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

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

I migrated my application from hibernate 4 to 5 , Now I can see Query is deprecated .我将我的应用程序从 hibernate 4 迁移到 5,现在我可以看到 Query 已被弃用。

I ran my code with existing HQL implementation.我使用现有的 HQL 实现运行了我的代码。 It failed in runtime.它在运行时失败。

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped.引起: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();

Can someone help me to solve this issue.有人可以帮我解决这个问题。 I tried to remove deprecated Query interface and used with Query also.我试图删除不推荐使用的 Query 接口并与 Query 一起使用。 But it not solve problem.但它不能解决问题。

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;
}

hibernate cfg file休眠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>

Please Check your Employee entity mapping,请检查您的员工实体映射,

if your Employee entity mapping like following:如果您的 Employee 实体映射如下:

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

then your Query should be:那么你的查询应该是:

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

if your Employee entity mapping like following:如果您的 Employee 实体映射如下:

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

then your Query should be:那么你的查询应该是:

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

if your Employee entity mapping like following:如果您的 Employee 实体映射如下:

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

then your Query should be:那么你的查询应该是:

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

For reference: org.hibernate.hql.internal.ast.querysyntaxexception entity/table is not mapped供参考: org.hibernate.hql.internal.ast.querysyntaxexception entity/table 未映射

Assuming that you have the Employee entity, you can correct your query in the following way:假设您有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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate-org.hibernate.hql.internal.ast.QuerySyntaxException:未映射客户端 - Hibernate - org.hibernate.hql.internal.ast.QuerySyntaxException: Client is not mapped org.hibernate.hql.internal.ast.QuerySyntaxException:未映射产品 - org.hibernate.hql.internal.ast.QuerySyntaxException: Product is not mapped 错误:org.hibernate.hql.internal.ast.QuerySyntaxException:网站未映射 - Error:org.hibernate.hql.internal.ast.QuerySyntaxException: Website is not mapped org.hibernate.hql.internal.ast.QuerySyntaxException:实体未映射 - org.hibernate.hql.internal.ast.QuerySyntaxException: Entity is not mapped org.hibernate.hql.internal.ast.QuerySyntaxException:WalletInfo未映射 - org.hibernate.hql.internal.ast.QuerySyntaxException: WalletInfo is not mapped org.hibernate.hql.internal.ast.QuerySyntaxException:未映射BaseModel - org.hibernate.hql.internal.ast.QuerySyntaxException: BaseModel is not mapped org.hibernate.hql.internal.ast.QuerySyntaxException:表未映射 - org.hibernate.hql.internal.ast.QuerySyntaxException: table is not mapped IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: ClassName 未映射 - IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: ClassName is not mapped org.hibernate.hql.internal.ast.QuerySyntaxException:未映射[来自Team] - org.hibernate.hql.internal.ast.QuerySyntaxException: is not mapped [from Team] org.hibernate.hql.internal.ast.QuerySyntaxException:员工未映射 - org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM