Java 两列之间差异的休眠条件
我有一个简单的hibernate实体,有两个字段-Java 两列之间差异的休眠条件,java,hibernate,criteria,Java,Hibernate,Criteria,我有一个简单的hibernate实体,有两个字段-a和b: @Entity public class PlayerEntity { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(nullable = false) private Integer a; @Column(nullable = false) private In
a
和b
:@Entity
public class PlayerEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(nullable = false)
private Integer a;
@Column(nullable = false)
private Integer b;
}
我需要选择
a-b>5
中的所有玩家使用标准的Hibernate标准API可以做到这一点吗?对于这种非常典型的情况,我是否可以避免使用SQL/HQL 谢谢 您可以使用SQL条件生成
标准
:
List<PlayerEntity> playerList = (List<PlayerEntity>)session.createCriteria(PlayerEntity.class)
.add(Restrictions.sqlRestriction("(a- b) > 5")).list();
请注意,@Formula的值是实际的列名,而不是映射的属性名。谢谢。但正如我在一个问题中提到的,我试图避免使用SQL/HQL。
@Entity
public class PlayerEntity {
@Column(nullable = false)
private Integer a;
@Column(nullable = false)
private Integer b;
@Formula("a - b")
private Integer delta
}
List<PlayerEntity> playerList = (List<PlayerEntity>)session.createCriteria(PlayerEntity.class)
.add(Restrictions.gt("delta", 5).list();