Querydsl java.lang.ClassCastException

时间:2018-03-08 12:48:31

标签: spring querydsl

我正在尝试使用Querydsl从帐户表中检索ninstid = 60的数据。但我得到了以下异常。

AccountModel

@Entity
@Table(name="account")
public class AccountModel implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "account_seq_generator")
    @SequenceGenerator(name="account_seq_generator", sequenceName = "account_seq")
    //@GeneratedValue(strategy = GenerationType.SEQUENCE)
    public Integer naccountid;
    public String namount;
    public String sacctdesc;
    public Integer naccountcpcmappingid;
    public Integer nindirectcostrate;
    public Integer nagencyid ;
    public Integer ndeptid ;
    public String sgrantnum;
    public Timestamp dstartdate;
    public Timestamp denddate;  
    public String slocation;
    public String sclientacctid;
    public Integer ninvestigatorid;
    public Integer ninstid;
    public Integer ntempaccountid;

    @ManyToOne(optional = true,cascade = {CascadeType.MERGE})
    @JoinColumn(name="ndeptid",insertable =  false, updatable = false)
    public DepartmentModel department;

    @ManyToOne(optional = true,cascade = {CascadeType.ALL})
    @JoinColumn(name="ninvestigatorid",insertable =  false, updatable = false)
    public InvestigatorModel investigator;

    @ManyToOne(optional = true,cascade = {CascadeType.ALL})  
    @JoinColumn(name="naccountcpcmappingid",insertable =  false, updatable = false)
    public AccountCPCMappingModel accountCPC;

的AccountService

public Set<AccountModel> gridLoad() {

        QAccountModel accountModel = QAccountModel.accountModel;
        @SuppressWarnings("rawtypes")
        JPAQuery query = new JPAQuery(em);      
                 query.from(accountModel)
                      .where(accountModel.ninstid.eq(60));

        return  (Set<AccountModel>) query.fetchAll();       
    }

我正在关注异常

java.lang.ClassCastException: com.querydsl.jpa.impl.JPAQuery cannot be cast to java.util.Set

我是querydsl的新手。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

@SuppressWarnings("unchecked")
    public List<AccountModel> gridLoad() {

        QAccountModel accountModel = QAccountModel.accountModel;
        @SuppressWarnings("rawtypes")
        JPAQuery query = new JPAQuery(em);      
                 query.from(accountModel)
                      .where(accountModel.ninstid.eq(60));

        return  query.fetch();         
    }