DAO How to match the field returned by the SQL function?

I use the dropwizard framework for my APIs. I have a named query as shown below:

@NamedQuery(name = "com.myapp.entity.MyEntity.findByMatchId", query = "SELECT test.name, count(test) as count FROM MyEntity as test,Entity1 as d where test.drug=d.id and test.drug.id= :drugId group by test.name") })

I return the result of the function count(test)as a name count. Below is my Entity class:

@NamedQuery(name = "com.myapp.entity.MyEntity.findByMatchId", query = "SELECT test.name, count(test) as count FROM MyEntity as test,Entity1 as d where test.drug=d.id and test.drug.id= :drugId group by test.name") })

@Entity
@Table(name = "my_entity")
@NamedQueries({
        @NamedQuery(name = "com.myapp.entity.MyEntity.findByMatchId", query = "SELECT test.name, count(test) as count FROM MyEntity as test,Entity1 as d where test.drug=d.id and test.drug.id= :drugId group by test.name") }))
public class MyEntity {

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

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

        @JsonBackReference("drug_id")
        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "drug_id", nullable = false)
        private Drug drug;

        @Column(name = "timestamp", nullable = false)
        private Date timestamp;

        // This value is not getting mapped 
        private Long count;

        public Long getId() {
            return id;
        }

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

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Drug getDrug() {
            return drug;
        }

        public void setDrug(Drug drug) {
            this.drug = drug;
        }

        public Date getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(Date timestamp) {
            this.timestamp = timestamp;
        }

        public SideEffectSeverity getSideEffectSeverity() {
            return sideEffectSeverity;
        }

        public void setSideEffectSeverity(SideEffectSeverity sideEffectSeverity) {
            this.sideEffectSeverity = sideEffectSeverity;
        }

        public Long getCount() {
            return count;
        }

        public void setCount(Long count) {
            this.count = count;
        }

    }

What can I do here to get the correct mapping to the value of the count function as count.

Why is my mapping not going right, do I need to give it some annotation?

+4
source share
2 answers

Do you have any result from the above query?

If so, note that the return function of count () will be long. Therefore, I hope that you tried to match the answer with the DTO (which should contain two fields as a name and a count).

+2

OracleSQL POSTgreSQL?

Count - , :

   @Column(name = "count1")
   private Long count;
0

Source: https://habr.com/ru/post/1686806/


All Articles