Java Tutorial/JPA/Query Parameter

Материал из Java эксперт
Перейти к: навигация, поиск

EJB QL Date Value As Parameter

File: Main.java



import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TemporalType;
public class Main {
  static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
  static EntityManager em = emf.createEntityManager();
  public static void main(String[] a) throws Exception {
    em.getTransaction().begin();
    Date start = new Date();
    Date end = new Date();
    em.createQuery("SELECT e " + "FROM Student e " + "WHERE e.startDate BETWEEN :start AND :end")
        .setParameter("start", start, TemporalType.DATE)
        .setParameter("end", end, TemporalType.DATE).getResultList();
    em.getTransaction().rumit();
    em.close();
    emf.close();
    Helper.checkData();
  }
}