如何在spring数据jpa中映射特定列?

时间:2017-06-06 10:34:36

标签: java spring jpa

每当我点击提交按钮时会有一些值,我想从数据库中选择一些列并在我的jsp页面上显示它。表单已成功提交,但我收到此错误:

  

java.lang.ClassCastException:[Ljava.lang.Object;无法投射到com.psc.Entity.CandidateappearagainstadvtcodeEntity

所以我认为存储库返回的任何数据都没有映射到Entity。请提出建议,如何解决这个问题?

控制器:

   @RequestMapping(value = "/getCandidateDetails", method = RequestMethod.POST)
    public String getCandidateDetails(Model model,@RequestParam("studentmasterid") String studentmasterid){
        System.out.println(studentmasterid);
        List<CandidateappearagainstadvtcodeEntity> candidates=candidateappearagainstadvtcodeEntityRepository.findBystudentmasterid(studentmasterid);

        return "payments";
    }

模型看起来像这样:

@Entity
@Table(name = "CANDIDATEAPPEARAGAINSTADVTCODE", schema = "PSCNEPALCOMMERCIALDATABASEU1", catalog = "")
public class CandidateappearagainstadvtcodeEntity {
    private long id;
    private String advertisementcode;
    private Integer ageonlastdateday;
    private Integer ageonlastdatemonth;
    private Integer ageonlastdateyear;
    private String applicationnumber;
    private String attendancestatus;
    private String candidatefirstname;
    private String dateofbirthinnepali;
    private String documentverificationstatus;
    private String examinationcenterid;
    private String examresultsstatus;
    private String fathername;
    private Long firstcoding;
    private String studentmasterid;
    //getters setters//

存储库:

public interface CandidateappearagainstadvtcodeEntityRepository extends JpaRepository<CandidateappearagainstadvtcodeEntity,Long> {

    @Query("select c.studentmasterid,c.advertisementcode,c.candidatefirstname from CandidateappearagainstadvtcodeEntity c where c.studentmasterid=?1")
    List<CandidateappearagainstadvtcodeEntity> findBystudentmasterid(String masterId);
}

的applicationContext.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:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
           http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.psc"/>

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
    <value>/WEB-INF/db.properties</value>
    </property>
    </bean>

    <!-- BoneCP configuration -->
    <bean id="mainDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driver}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="idleConnectionTestPeriodInMinutes" value="60"/>
        <property name="idleMaxAgeInMinutes" value="240"/>
        <property name="maxConnectionsPerPartition" value="30"/>
        <property name="minConnectionsPerPartition" value="10"/>
        <property name="partitionCount" value="3"/>
        <property name="acquireIncrement" value="5"/>
        <property name="statementsCacheSize" value="100"/>
        <property name="releaseHelperThreads" value="3"/>
    </bean>

    <!-- SPRING - JPA -->
    <jpa:repositories
            base-package="com.psc" />

    <bean class="org.springframework.orm.jpa.JpaTransactionManager"
          id="transactionManager">
        <property name="entityManagerFactory"
                  ref="entityManagerFactory" />
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
    </bean>

    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="mainDataSource" />
        <property name="packagesToScan" value="com.psc"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="true" />
                <property name="showSql" value="false"/>
                <property name="databasePlatform" value="org.hibernate.dialect.Oracle12cDialect"/>
                <property name="database" value="ORACLE"/>
            </bean>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle12cDialect</prop>
            </props>
        </property>
    </bean>

</beans>

1 个答案:

答案 0 :(得分:0)

JPA存储库通常返回域模型。来自docs

  

Spring Data Repositories通常在使用查询方法时返回域模型。但是,有时,您可能需要根据各种原因更改该模型的视图。在本节中,您将学习如何定义投影以提供简化和简化的资源视图。

您的存储库方法findBystudentmasterid将返回列表Object[],因此返回ClassCastException。如果您想获取特定列,请尝试Projections

关于如何使用投影的docsthis答案。