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 ;
}

}

hibernate-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)

これに関する助けは大歓迎です。

4

2 に答える 2

0

春の構成でエンティティのアノテーションのスキャンが設定されていることを確認してください。

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

hibernate.cfg.xmlのsessionfactoryBeanで

次に、を使用してスプリング構成でコンポーネントスキャンを有効にする必要があります

<!-- Auto scan the components -->
<context:component-scan base-package="com.YOUR.BASE.PATH" />

もちろん、アノテーション用の適切な休止状態コンポーネントがあることを確認してください。使用しているものである場合は、次の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

プロパティの名前をに変更IsEnableしますisEnable。Hibernateは最初の大文字と混同されていると思いますが、とにかくこれはJavaの規則です...

于 2013-02-20T17:32:12.910 に答える