Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java DAO如何映射SQL函数返回的字段?_Java_Hibernate_Dropwizard - Fatal编程技术网

Java DAO如何映射SQL函数返回的字段?

Java DAO如何映射SQL函数返回的字段?,java,hibernate,dropwizard,Java,Hibernate,Dropwizard,我正在为我的API使用dropwizard框架。我有一个命名查询,如下所示: @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"

我正在为我的API使用dropwizard框架。我有一个命名查询,如下所示:

@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") })
我将
count(test)
函数的结果返回为name
count
。下面是我的实体类:

@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;
        }

    }
我可以在这里做什么来获得正确的映射,将count函数的值设置为count


为什么我的映射不正确,我必须给它一些注释吗?

您从上面的查询中得到任何结果吗


如果是,请注意count()函数的返回类型将很长。所以,希望您已经尝试将响应映射到DTO(应该包含两个字段,如name和count)

您使用的是OracleSQL还是POSTgreSQL

Count是一个保留字,重命名列会像这样解决此问题

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