JPA条件选择子句

SELECT子句用于从数据库中获取数据。可以以单个表达式或多个表达式的形式检索数据。在Criteria API中, 每种形式都有不同的表达方式。
条件选择示例 【JPA条件选择子句】通常, select()方法用于SELECT子句以获取所有类型的表单。在这里, 我们将对学生表执行几个SELECT操作。让我们假设该表包含以下记录:-

JPA条件选择子句

文章图片
现在, 请按照以下步骤执行操作:-
  • 创建一个实体类。在这里, 我们在com.srcmini.jpa包下创建了StudentEntity.java。此类包含三个属性s_id, s_name, s_age和所有必需的注释。
StudentEntity.java
package com.srcmini.jpa; import javax.persistence.*; @Entity @Table(name="student") public class StudentEntity {@Idprivate int s_id; private String s_name; private int s_age; public StudentEntity(int s_id, String s_name, int s_age) {super(); this.s_id = s_id; this.s_name = s_name; this.s_age = s_age; }public StudentEntity() {super(); }public int getS_id() {return s_id; }public void setS_id(int s_id) {this.s_id = s_id; }public String getS_name() {return s_name; }public void setS_name(String s_name) {this.s_name = s_name; }public int getS_age() {return s_age; }public void setS_age(int s_age) {this.s_age = s_age; } }

  • 现在, 在Persistence.xml文件中映射实体类和其他数据库配置。
Persistence.xml
< persistence> < persistence-unit name="Student_details"> < class> com.srcmini.jpa.StudentEntity< /class> < properties> < property name="javax.persistence.jdbc.driver" value="http://www.srcmini.com/com.mysql.jdbc.Driver"/> < property name="javax.persistence.jdbc.url" value="http://www.srcmini.com/jdbc:mysql://localhost:3306/studentdata"/> < property name="javax.persistence.jdbc.user" value="http://www.srcmini.com/root"/> < property name="javax.persistence.jdbc.password" value=""/> < property name="eclipselink.logging.level" value="http://www.srcmini.com/SEVERE"/> < property name="eclipselink.ddl-generation" value="http://www.srcmini.com/create-or-extend-tables"/> < /properties> < /persistence-unit> < /persistence>

  • 一旦创建了基本实体类, 并将配置映射到persistence.xml文件中, 就可以通过以下方式执行不同类型的选择操作:-
选择单个表达式
在这里, 我们将借助一个简单的示例从数据库中获取单个列。
SingleFetch.java
package com.srcmini.jpa.jpql; import com.srcmini.jpa.StudentEntity; import javax.persistence.*; import javax.persistence.criteria.*; import java.util.*; public class SingleFetch { public static void main( String args[]) {EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" ); EntityManager em = emf.createEntityManager(); em.getTransaction().begin( ); CriteriaBuilder cb=em.getCriteriaBuilder(); CriteriaQuery< StudentEntity> cq=cb.createQuery(StudentEntity.class); Root< StudentEntity> stud=cq.from(StudentEntity.class); cq.select(stud.get("s_name")); CriteriaQuery< StudentEntity> select = cq.select(stud); TypedQuery< StudentEntity> q = em.createQuery(select); List< StudentEntity> list = q.getResultList(); System.out.println("s_id"); for(StudentEntity s:list){System.out.println(s.getS_id()); }em.getTransaction().commit(); em.close(); emf.close(); }}

输出:
JPA条件选择子句

文章图片
选择多个表达式
在这里, 我们将借助一个简单的示例从数据库中获取多个列。
MultiFetch.java
package com.srcmini.jpa.jpql; import com.srcmini.jpa.StudentEntity; import javax.persistence.*; import javax.persistence.criteria.*; import java.util.*; public class MultiFetch { public static void main( String args[]) {EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" ); EntityManager em = emf.createEntityManager(); em.getTransaction().begin( ); CriteriaBuilder cb=em.getCriteriaBuilder(); CriteriaQuery< StudentEntity> cq=cb.createQuery(StudentEntity.class); Root< StudentEntity> stud=cq.from(StudentEntity.class); cq.multiselect(stud.get("s_id"), stud.get("s_name"), stud.get("s_age") ); CriteriaQuery< StudentEntity> select = cq.select(stud); TypedQuery< StudentEntity> q = em.createQuery(select); List< StudentEntity> list = q.getResultList(); System.out.print("s_id"); System.out.print("\t s_name"); System.out.println("\t s_age"); for(StudentEntity s:list){System.out.print(s.getS_id()); System.out.print("\t"+s.getS_name()); System.out.println("\t"+s.getS_age()); }em.getTransaction().commit(); em.close(); emf.close(); }}

输出:
JPA条件选择子句

文章图片

    推荐阅读