我可以使用createSQLQuery从R_States表中查看结果列表。
public List<String> getStates() throws Exception {
try
{
Session session = sessionFactory.openSession();
return session.createSQLQuery("select * from R_States").list();
}
catch(Exception e)
{
//Logging
}
finally
{
if(session !=null && session.isOpen())
{
session.close();
session=null;
}
}
return null;
}
但是当我尝试使用HQL createQuery方法时,我得到一个ORA-00942:表或视图不存在错误。
return session.createQuery("from States").list();
这是我的会话工厂(JNDI Datasource)。
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="packagesToScan">
<list>
<value>folder.dao</value>
<value>folder.domain.States</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9iDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</prop>
<prop key="hibernate.connection.datasource">java:/comp/env/jdbc/r</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>
这是我的域类。我错过了什么?
@Entity
@Table(name = "R_STATE")
public class States {
@Column(name = "SCode")
private String stateCode;
@Column(name = "SName")
private String stateName;
public String getStateCode() {
return stateCode;
}
public void setStateCode(String stateCode) {
this.stateCode = stateCode;
}
public String getStateName() {
return stateName;
}
public void setStateName(String stateName) {
this.stateName = stateName;
}
答案 0 :(得分:0)
<value>folder.domain.States</value>
这是一个类,而不是包,所以Hibernate可能找不到你的映射实体。尝试改变:
<value>folder.domain</value>
答案 1 :(得分:0)
有同样的问题。根据我自己的经验说,问题是我没有将查询视为针对Oracle的本机SQL查询(在我的案例中是后端)。我还必须确定指定架构。该查询一旦编写为引用实际的字段名称和表名(带有模式),最后运行正常。例如:
String sql = "Select fieldname as alias from myschema.mytablename where fieldname = 'somevalue'"; // This is all raw SQL, not Hibernate SQL
List<Double> dblList = null; // In my case, 'fieldname' is a NUMBER col type, so Hibernate returns a Double. If it were a VARCHAR, it'd be a String.
try {
dblList = session.createSQLQuery(sql).addScalar("alias", StandardBasicTypes.DOUBLE ).list(); // org.hibernate.session, org.hibernate.type.StandardBasicTypes
} catch (Exception e) {
e.printStackTrace();
}
int valuetoworkwith = 0;
if(dblList != null && dblList.size() > 0) {
valuetoworkwith = (dblList.get(0)).intValue();
} else
{
// ... If desired, something to alert the user that no value was returned so the default of '0' is being used, or throw an exception, or do nothing in particular.
}
另见:https://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/querysql.html#querysql-creating