如何将此sql查询更改为hibernate查询?

时间:2014-10-31 11:59:34

标签: sql

我想将此sql查询更改为hibernate查询。

SQL查询

SELECT *
FROM User
WHERE (DATE_SUB(NOW(), INTERVAL 1 DAY) >= AccountStartDate)
    AND PINStatus = 'N'
    AND PetName IS NULL
    AND SchoolName IS NULL
    AND AccountType = 'Supervisor'

这是我的User班级

@Entity
@Table(name = "User")
public class User{

    @Id
    @GeneratedValue
    @Column(name="UserID")
    private long userID; 

    @Column(name="AccountStartDate")
    private Date accountStartDate;

    @Column(name="PINStatus")
    private String pinStatus;

    @Column(name="PetName")
    private String petName;;

    @Column(name="SchoolName")
    private String schoolName;

    @Column(name="AccountType")
    private String accountType;
}

2 个答案:

答案 0 :(得分:1)

以下是答案的一部分......试试这个

User user = _session.CreateCriteria<User>()
 .Add(Restrictions.Eq("PINStatus", "[VARIABLE PARAMETER]"))
 .AddOrder(new Order("_id", false))
 .List<User>()
 .FirstOrDefault();

这将导致类似:

从用户中选择*,其中PINStatus = ???按订单排序

答案 1 :(得分:0)

Query query=sessionFactory.getCurrentSession().createQuery("FROM User WHERE " +
                    "pinStatus=:pinStatus AND petName IS NULL AND schoolName IS NULL AND accountType=:accountType")
                    .setString("accountType", "User").setString("pinStatus", "N");
            List<User> userList=query.list();
            Iterator<User> itr = userList.iterator();
              while(itr.hasNext()) {
                 User user = (User) itr.next();
                 long diff=currentDate.getTime() - user.getAccountStartDate().getTime();
                 long diffHours = diff / (60 * 60 * 1000);
                 if(diffHours>=24)
                 {
                     System.out.println("User added "+user.getFirstName());
                     userList1.add(user);
                 }
              }