java - 使用 SQL 查询在 CrudRepository 中显示 COUNT(variable) 时出错

标签 java spring hibernate

我在查询 MySQL 数据库时遇到显示变量计数的问题。我用注释 @Transient 创建了一个变量,以便它不包含在数据库中。但是,我在数据库中的同一个表中发布数据时遇到错误,因为发布时没有字段计数,计数仅用于获取 COUNT(u_type)。当我执行 GET 调用(使用 SQL 查询)时,有什么方法可以显示变量的 COUNT 并且无需发布它。 TIA。

类(class):

import java.sql.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.springframework.data.annotation.Transient;

@Entity // This tells Hibernate to make a table out of this class
public class UserClickData {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private String u_search_term;
    private String u_sysid;
    private String u_type;

    @Transient
    private long count;

    public UserClickData(String u_type, long Count) {       //, long count
        this.u_type = u_type;
        this.count=count;
    }
    public long getCount() {
        return count;
    }
    public void setCount(long count) {
        this.count=count;
    }

    public int getSys_id() {
        return sys_id;
    }
    public void setSys_id(int sys_id) {
        this.sys_id = sys_id;
    }
    public String getU_search_term() {
        return u_search_term;
    }
    public void setU_search_term(String u_search_term) {
        this.u_search_term = u_search_term;
    }

    public String getU_type() {
        return u_type;
    }
    public void setU_type(String u_type) {
        this.u_type = u_type;
    }

}

投影:

public interface UserClickProjection {
    String getU_type(); 
    long getCount();
}

DAO 代码:

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import com.abc.datacollection.entity.UserClickData;
import com.abc.datacollection.entity.UserClickProjection;
import com.abc.datacollection.entity.UserProjection;

public interface UserClickDataRepository extends CrudRepository<UserClickData, Integer> {
    public static final String FIND_QUERY = 
    "select new com.abc.datacollection.entity.UserClickData(user.u_type, COUNT(u_type)) from UserClickData user GROUP BY user.u_type ORDER BY COUNT(user.u_type) DESC";
    @Query(value = FIND_QUERY)
    //public List<UserProjection> getAllRequestResponseRecords();
     List<UserClickProjection> findAllProjectedBy();

}

Controller :

@CrossOrigin(origins = "*")
    @GetMapping(path="/all")
    public @ResponseBody List<UserClickProjection> getAllUserClickDataRecords() {
        return userClickDataRepository.findAllProjectedBy();
    }

最佳答案

导入 javax.persistence.Transient 而不是 org.springframework.data.annotation.Transient

关于java - 使用 SQL 查询在 CrudRepository 中显示 COUNT(variable) 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58233045/

相关文章:

java - 使用 Mustache 模板请求转发到 spring boot mvc 中的另一个 url

java - Selenium 点击浏览器的 url 而不是网站的搜索框

java - 我想使用两个不同表的 id 作为第三个表的 id(hibernate、java、spring)

hibernate - Grails Row 被另一个事务更新或删除(或未保存值映射不正确)

java - 以编程方式添加 Java 文件的 SerialVersion Id

java - 将实体的外键作为主键 JPA 持久化

java - 如何将我的 MapReduce 作业的结果存储在 Hashmap 中并按值排序?

java - InvokeLater() - 一次就够了吗?

java - Spring AOP - 为什么我需要 aspectjweaver?

java - Spring Boot 使用 1.5.6 发布重新打包的子模块 jar