hibernate 5 org.hibernate.hql.internal.ast.querysyntaxexception:未MapEmploye

zynd9foi  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(460)

我将我的应用程序从Hibernate4迁移到了5,现在我可以看到查询被弃用了。
我用现有的hql实现运行了我的代码。它在运行时失败。
原因:org.hibernate.hql.internal.ast.querysyntaxexception:未Map员工。

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();

有人能帮我解决这个问题吗。我试图删除不推荐使用的查询接口,并与查询一起使用。但这并不能解决问题。

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>
k4emjkb1

k4emjkb11#

假设你有 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();
vatpfxk5

vatpfxk52#

请检查员工实体Map,
如果您的员工实体Map如下所示:

@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= ?");

如果您的员工实体Map如下所示:

@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= ?");

如果您的员工实体Map如下所示:

@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实体/表未Map

相关问题