我是初学者,使用Spring / hibernate。我在方法中编写了以下查询,以便该方法根据查询结果返回一个布尔值。
public Boolean getStatus(){
logger.debug("Get Emp Status");
Session session = sessionFactory.getCurrentSession();
String hql = "SELECT ec.IsEnable FROM Employee ec WHERE ec.UID=1";
Query query = session.createQuery(hql);
Boolean result = (Boolean) query.uniqueResult();
return result;
}
员工实体:
@Entity
@Table (name = "FS_Employee")
public class Employee {
@Id
@GeneratedValue
@Column (name = "UID")
private Integer UID;
@Column (name = "IsEnable")
private Boolean IsEnable;
public Employee(){ }
/**
* @return
*/
public Boolean getIsEnable()
{
return IsEnable;
}
/**
* @param boolean1
*/
public void setIsEnable(Boolean boolean1)
{
IsEnable = boolean1;
}
/*
*sets value of UID
*
* @param Integer
*/
public void setUID(Integer UID){
this.UID = UID;
}
/*
*gets value of UID
*
* @param
*/
public Integer getUID(){
return UID ;
}
}
冬眠-context.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<context:property-placeholder location="/WEB-INF/spring.properties" />
<!-- Enable annotation style of managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="/WEB-INF/hibernate.cfg.xml"
p:packagesToScan="test"/>
</beans>
hibernate.cfg.xml中:
<?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>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="show_sql">false</property>
</session-factory>
</hibernate-configuration>
这不起作用,我收到以下错误。
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.QueryException: could not resolve property: IsEnable of: test.spring.model.Employee [SELECT ec.IsEnable FROM test.spring.model.Employee ec WHERE ec.UID=1]
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
非常感谢任何帮助。
答案 0 :(得分:0)
确保我们的弹簧配置设置扫描您的实体的注释,您需要
<property name="annotatedClasses">
<list>
<value>com.path.to.entities.MyEntity</value>
</list>
</property>
在hibernate.cfg.xml的sessionfactory bean中
然后,您需要使用
在弹簧配置中启用组件扫描<!-- Auto scan the components -->
<context:component-scan base-package="com.YOUR.BASE.PATH" />
当然,请确保您有适当的hibernate组件用于注释。如果你正在使用它,那么需要以下maven工件。否则将这些文物导入您的项目,但我认为如果它没有抱怨就有它们。
<!-- Hibernate annotation -->
<dependency>
<groupId>hibernate-annotations</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.0.GA</version>
</dependency>
<dependency>
<groupId>hibernate-commons-annotations</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.0.0.GA</version>
</dependency>
答案 1 :(得分:0)
将您的媒体资源IsEnable
重命名为isEnable
。我认为Hibernate与你的第一个大写字母相混淆,无论如何这是一个Java约定......