0

Entity Class

@Entity
@Table("abc")
public class Portfolio {

    @Id
    @Column(name="account_number")
    @JsonProperty("account_number")
    @ApiModelProperty(value = "account_number")
    private String accountNumber;

    @Column(name="query_email")
    private String query_email;
}

HQL Query : From Portfolio where query_email='abc@gamil.com'

above query selecting query_email, i don't need that. i want to ignore the query_email on select. if i use @Transient its not allowing me to query with query_email

is there any other solution for this scenario ?

1 Answers1

0

Use projections on constructor.

Query:

select new Portfolio(accountNumber) From Portfolio where query_email='abc@gamil.com'

You need add the contrucor with the same types that the query.

@Entity
@Table("abc")
public class Portfolio {

    @Id
    @Column(name="account_number")
    @JsonProperty("account_number")
    @ApiModelProperty(value = "account_number")
    private String accountNumber;

    @Column(name="query_email")
    private String query_email;

    public Portfolio(String accountNumber) {
        this.accountNumber = accountNumber;
    }

    public Portfolio() {
    }
}

Or then use query.setResultTransformer(Portfolio.class):

Hibernate SQL Query result Mapping/Convert TO Object/Class/Bean

Ady Junior
  • 1,040
  • 2
  • 10
  • 18
  • thanks for quick replay but, i have to distinctly select around 50 other fields from that table. can i select distinct row using projections on constructor? –  Jun 12 '17 at 16:43
  • Then you can use @Formula("SOME QUERY HERE") on query_email field, with any query that returns empty value. But it is a big and ugly work around.... – Ady Junior Jun 12 '17 at 16:46
  • Yes, you can select distinct row using **distinct** keyword. – Ady Junior Jun 12 '17 at 16:53