@Query returning Object instead of entity

前端 未结 2 1486
长情又很酷
长情又很酷 2020-12-18 06:03

When I use @Query annotation with select fields, I dont get the entity object back. How can I get the entity object back?

public interface CreditCenterReposi         


        
2条回答
  •  春和景丽
    2020-12-18 06:32

    Ok It seems that you are using a projection over the entity CreditCenter

    @Query("SELECT CC.id, CC.loanId, CC.clientId FROM CreditCenter CC")
    

    My first thought is : Why you dont use something like this.

     @Query("SELECT CC FROM CreditCenter CC")
    

    that will return the list of the entities, however probably you dont want to return all the fields so my second advice is use this query.

    @Query("SELECT new package.to.CreditCenter(CC.id, CC.loanId, CC.clientId) FROM CreditCenter CC")
    

    and add a constructor in Creditcenter that support the order and type of parameters. That will work using JPQL and as jpa repos use that it should work.

    public class CreditCenter {
    
     //Member vars
     public CreditCenter (int id, int loadid, int clientid){...}
    }
    

提交回复
热议问题