1. 获取查询结果
2. JPQL函数和JPQL表达式
#1. 使用from子句
#2. 查询部分属性
#3. 查询中使用构造器
3. JPQL的关联查询和多态查询
#1. 多态查询
#2. 隐式连接
#3. 显式连接
4. 对查询结果集进行分页
5. 使用JPQL子查询和命名查询
#1. 子查询
#2. 命名查询
6. JPQL的批量更新和删除
#1. 批量更新
#2. 批量删除
--------------------------------------------------
1. 获取查询结果(Net Beans创建Java Project, 项目名称:JpqlQs)
JpaQs.java
package lee; import java.util.List;
import javax.persistence.*; import org.crazyit.model.*; public class JpaQs { private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("qs"); public static void main(String[] args) {
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
Query query = em.createQuery("select news , news.title , news.content"
+ " from News as news where news.id > ?1 and"
+ " news.title not like :title"); List result = query.setParameter(1, 1).setParameter("title", "%Java%").getResultList();
for (int i = 0; i < result.size(); i++) {
Object[] row = (Object[]) result.get(i);
for (int j = 0; j < row.length; j++) {
System.out.println(row[j]);
}
}
em.getTransaction().commit();
}
finally {
em.close();
}
}
}
News.java(参见12.2)
persistence.xml(参见12.2)
2. JPQL函数和JPQL表达式
#1. 使用from子句(Net Beans创建Java Project, 项目名称:From)
JpaQs.java
package lee; import java.util.List;
import javax.persistence.*; import org.crazyit.model.*; public class JpaQs { private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("qs"); public static void main(String[] args) {
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
Query query = em.createQuery("from News as news where news.id < ?1"
+ " and news.title like :title"); List result = query.setParameter(1, 8).setParameter("title", "%Java%").getResultList(); for (int i = 0; i < result.size(); i++) {
News news = (News) result.get(i);
System.out.println(news.getTitle() + "-->" + news.getContent());
} em.getTransaction().commit();
}
finally {
em.close();
}
}
}
News.java(参见12.2)
persistence.xml(参见12.2)
#2. 查询部分属性(Net Beans创建Java Project, 项目名称:SubProperty)
JpaQs.java
package lee; import java.util.List;
import javax.persistence.*; import org.crazyit.model.*; public class JpaQs { private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("qs"); public static void main(String[] args) {
EntityManager em = emf.createEntityManager();
try { em.getTransaction().begin();
Query query = em.createQuery("select news.title , news.content"
+ " from News as news where news.id < ?1"
+ " and news.title like :title"); List result = query.setParameter(1, 8).setParameter("title", "%Java%").getResultList();
for (int i = 0; i < result.size(); i++) {
Object[] values = (Object[]) result.get(i);
System.out.println(values[0] + "-->" + values[1]);
} em.getTransaction().commit();
}
finally {
em.close();
}
}
}
News.java(参见12.2)
persistence.xml(参见12.2)
#3. 查询中使用构造器(Net Beans创建Java Project, 项目名称:Constructor)
NewsDTO.java
package org.crazyit.dto; public class NewsDTO { private String title;
private String content; // constructor // setter
// getter
}
JpaQs.java
package lee; import java.util.List;
import javax.persistence.*; import org.crazyit.model.*; public class JpaQs { private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("qs");
public static void main(String[] args) {
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
Query query = em.createQuery("select distinct new org.crazyit.dto.NewsDTO(news.title"
+ " , news.content) from News as news where news.id < ?1"
+ " and news.title like :title"); List result = query.setParameter(1, 8).setParameter("title", "%Java%").getResultList();
for (int i = 0; i < result.size(); i++) {
System.out.println(result.get(i));
} em.getTransaction().commit();
}
finally {
em.close();
}
}
}
News.java(参见12.2)
persistence.xml(参见12.2)
3. JPQL的关联查询和多态查询
#1. 多态查询(Net Beans创建Java Project, 项目名称:PolyQuery)
News.java(参见12.2)
persistence.xml(参见12.2)
#2. 隐式连接(Net Beans创建Java Project, 项目名称:ImplicitJoin)
News.java(参见12.2)
persistence.xml(参见12.2)
#3. 显式连接(Net Beans创建Java Project, 项目名称:ExplicitJoin)
News.java(参见12.2)
persistence.xml(参见12.2)
4. 对查询结果集进行分页(Net Beans创建Java Project, 项目名称:Paging)
News.java(参见12.2)
persistence.xml(参见12.2)
5. 使用JPQL子查询和命名查询
#1. 子查询(Net Beans创建Java Project, 项目名称:SubQuery)
News.java(参见12.2)
persistence.xml(参见12.2)
#2. 命名查询(Net Beans创建Java Project, 项目名称:NamedQuery)
News.java(参见12.2)
persistence.xml(参见12.2)
6. JPQL的批量更新和删除
#1. 批量更新(Net Beans创建Java Project, 项目名称:Update)
News.java(参见12.2)
persistence.xml(参见12.2)
#2. 批量删除(Net Beans创建Java Project, 项目名称:Delete)
News.java(参见12.2)
persistence.xml(参见12.2)