0

I want to change this sql query to hibernate query.

SQL query:

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'

This is my User class

@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 Answers 2

1

Here is part of the answer... Try this

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

That will result in something like:

select * from User where PINStatus = ??? Order By Id desc

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.