使用 SQL 查询在 CrudRepository 中显示 COUNT(变量) 时出错



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

类:

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();
}

道码:

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();
}

控制器:

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

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

最新更新