Hibernate 如何使用条件创建查询

Hibernate 如何使用条件创建查询,hibernate,hibernate-criteria,Hibernate,Hibernate Criteria,我有一门课叫投票: @Table(name = "PHY_VOTE") public class VoteEntity extends BaseEntity<Long> implements Comparable { public static final String TITLE = "title"; public static final String VALUE = "value"; public static final String USER =

我有一门课叫投票:

@Table(name = "PHY_VOTE")
public class VoteEntity extends BaseEntity<Long> implements Comparable {

    public static final String TITLE = "title";
    public static final String VALUE = "value";
    public static final String USER = "user";

    private String title;
    private String value;

    private UserEntity user;

    public VoteEntity() {}

    public VoteEntity(Long id) {
        setId(id);
    }

    public VoteEntity(String title, String value, UserEntity user) {
        this.title = title;
        this.value = value;
        this.user = user;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "VOTE_ID")
    @Override
    public Long getId() {
        return super.getId();
    }

    @FilterProperty(operation = FilterProperty.ILIKE)
    @Column(name = "TITLE", nullable = false, length = 100)
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @FilterProperty(operation = FilterProperty.ILIKE)
    @Column(name = "VALUE", nullable = false, length = 4)
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @ManyToOne
    @JoinColumn(name = "USER_ID", foreignKey = @ForeignKey(name = "PYH_VOTE_USER_FK"))
    public UserEntity getUser() {
        return user;
    }

    public void setUser(UserEntity user) {
        this.user = user;
    }

    @Transient
    @Override
    public String getOptionsTitleProperty() {
        return TITLE;
    }

}
@Table(name=“PHY\u投票”)
公共类VoteEntity扩展了BaseEntity实现{
公共静态最终字符串TITLE=“TITLE”;
公共静态最终字符串VALUE=“VALUE”;
公共静态最终字符串USER=“USER”;
私有字符串标题;
私有字符串值;
私有用户实体用户;
公共Votentity(){}
公众呕吐(长id){
setId(id);
}
公共vontity(字符串标题、字符串值、用户实体用户){
this.title=标题;
这个值=值;
this.user=用户;
}
@身份证
@GeneratedValue(策略=GenerationType.SEQUENCE)
@列(name=“VOTE\u ID”)
@凌驾
公共长getId(){
返回super.getId();
}
@FilterProperty(操作=FilterProperty.ILIKE)
@列(name=“TITLE”,null=false,长度=100)
公共字符串getTitle(){
返回标题;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
@FilterProperty(操作=FilterProperty.ILIKE)
@列(name=“VALUE”,null=false,长度=4)
公共字符串getValue(){
返回值;
}
公共void设置值(字符串值){
这个值=值;
}
@许多酮
@JoinColumn(name=“USER\u ID”,foreignKey=@foreignKey(name=“PYH\u VOTE\u USER\u FK”))
公共用户实体getUser(){
返回用户;
}
public void setUser(UserEntity用户){
this.user=用户;
}
@短暂的
@凌驾
公共字符串GetOptionTitleProperty(){
返回标题;
}
}
如何使用条件创建以下命令


< > T.TITE,Tyt(St(t值)/计数(t标题),2)值由TythPosit T组由T.Eng/P>< P> >您可以考虑用:

public AverageByTitle getAverageByTitle(){
FluentQuery=FluentJPA.SQL((votentity t)->{
别名avg=Alias(四舍五入(SUM(INT.raw(t.getValue()))/COUNT(t.getTitle()),2),
AverageByTitle::getAverage);
选择(t.getTitle(),平均值);
从(t);
组(t.getTitle());
});
返回query.createQuery(em,AverageByTitle.class).getSingleResult();
}
//声明
公共静态最终数据类型INT=DataTypes.INT;
@元组
@数据//龙目
公共静态类AverageByTitle{
私有字符串标题;
私人浮动平均线;
}
这将生成以下SQL:

SELECT t0.TITLE, ROUND((SUM(t0.VALUE) / COUNT(t0.TITLE)), 2) AS average
FROM PHY_VOTE t0
GROUP BY  t0.TITLE
谷歌在这方面帮助很大:。您可能需要在代码中进行舍入,因为我不确定条件是否支持开箱即用(因此选择
sum
count
)。