0

我是使用 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 ;
}

}

休眠上下文.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>

休眠.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)

对此的任何帮助将不胜感激。

4

2 回答 2

0

确保我们的 spring 配置为您的实体设置了注释扫描,您需要

<property name="annotatedClasses">
    <list>
      <value>com.path.to.entities.MyEntity</value>
    </list>
</property>

在 hibernate.cfg.xml 的 sessionfactory bean 中

然后你会想要在你的 spring 配置中启用组件扫描

<!-- 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>
于 2013-02-20T17:21:23.080 回答
0

将您的属性重命名IsEnableisEnable. 我认为 Hibernate 与您的第一个大写字母混淆了,无论如何这是 Java 约定...

于 2013-02-20T17:32:12.910 回答