我将我的应用程序从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;
}
hibernate 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>
请检查您的员工实体映射,
如果您的员工实体映射如下:
@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.hubinate.ql.internal.ast.querysyntaxexception实体/表未映射
假设您有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();