0

i am working on Jpa Native query,

I want all my all send with it count but this my query

select id,status,COUNT(*) as count from tb_abc where sender_id=?1 group by status

But problem is when i will run it on Mysql then it will run fine but when i will run using java code i.e native query then then count is return null or 0..

This my class which i have map from query

This Native Query

@Repository public class PatientAppointmentNativeDao {

@PersistenceContext
private EntityManager em;

public List<TableAbc> get(Long senderId) {

    @SuppressWarnings("unchecked")
    List<TableAbc> resultData = (List<TableAbc>) em
            .createNativeQuery(
                    "select *,COUNT(*) as count from tb_abc where sender_id=?1 group by status",
                    com.abc.TableAbc.class)
            .setParameter(1, senderId)
            .getResultList();

    return resultData;
}

}

and TableABC class is as:

public class PatientAppointment {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;



@ManyToOne
private Sender sender;

private Integer status;


@Transient
private int countTotal;

public int getCountTotal() {
    return countTotal;
}

public void setCountTotal(int countTotal) {
    this.countTotal = countTotal;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}




public Integer getStatus() {
    return status;
}

public void setStatus(Integer status) {
    this.status = status;
}

}

Please help me ...

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
Charnjeet Singh
  • 3,056
  • 6
  • 35
  • 65
  • For native query use '@Transactional` annotation – SpringLearner Jul 18 '14 at 06:48
  • No effect using @Transactional again get 0 value as count – Charnjeet Singh Jul 18 '14 at 06:51
  • Try `?` instead of `?1`. That is almost the only change w.r.t. your mysql manual test. Though I had my doubt on `as count` reusing a function name. – Joop Eggen Jul 18 '14 at 06:57
  • Unrelated but: your count will give you not the results you think it will because your `group by` usage is wrong (and every other DBMS would simply reject that query). See e.g. here for a explanation: http://www.mysqlperformanceblog.com/2006/09/06/wrong-group-by-makes-your-queries-fragile/ –  Jul 18 '14 at 07:05
  • But when i will try it on Mysql interface then it will work fine – Charnjeet Singh Jul 18 '14 at 07:07

0 Answers0