- 浏览: 147463 次
- 来自: ...
文章分类
最新评论
-
fisher:
真心感谢楼主! 解决了困扰我几天的大问题啊!
EntityManagerFactory -
悲剧了:
太棒了,我们项目正在用这个
struts2 convention-plugin -
nforce_com:
...
jpa继承关系详解 -
guanchuangsheng:
精辟~~
总算明白了·~
桥接模式和适配器模式的区别 -
lping2:
强,写得太全面了
EntityManagerFactory
6 Query
6.1 JPQL Queries
6.1.1Query Basics
Java代码
public Query createQuery(String jpql);
public Query createQuery(String jpql); EntityManager上的这个方法用来根据给定的JPQL创建Query。
Java代码
public List getResultList();
public List getResultList(); Query上的这个方法用来得到query的执行结果,以下是个简单的例子:
Java代码
EntityManager em = entityManagerFactory.createEntityManager();
Query q = em.createQuery("SELECT x FROM Magazine x");
List<Magazine> results = (List<Magazine>) q.getResultList();
em.close();
EntityManager em = entityManagerFactory.createEntityManager();
Query q = em.createQuery("SELECT x FROM Magazine x");
List<Magazine> results = (List<Magazine>) q.getResultList();
em.close(); JPQL query的from子句中定义了query内部命名空间。可以将任意的标识符赋值给entities,之后就可以在query的任意位置上通过标识符引用entities。 在from子句中,关键字as是可选的。例如以下两个JPQL等效:Sql代码
SELECT x FROM Magazine x
SELECT x FROM Magazine AS x
SELECT x FROM Magazine x
SELECT x FROM Magazine AS x 当查询entities的时候,关键字object也是可选的,例如select x 和 SELECT OBJECT(x) 是等效的。JPQL中的关键字不是大小写敏感的,而entity、identifier和member names是大小写敏感的。以下是一些JPQL的例子:
Sql代码
SELECT x FROM Magazine x WHERE x.TITLE = 'JDJ'
SELECT x FROM Magazine x WHERE x.title = 'JDJ' OR x.title = 'JavaPro'
SELECT x FROM Magazine x WHERE x.price > 3.00 AND x.price <= 5.00
SELECT x FROM Magazine x WHERE x.price <> 3.00
SELECT x FROM Magazine x WHERE (x.price > 3.00 AND x.price <= 5.00) OR x.price = 7.00
SELECT x FROM Magazine x WHERE x.price > 3.00 AND (x.price <= 5.00 OR x.price = 7.00)
SELECT x FROM Magazine x WHERE x.price >= 3.00 AND x.price <= 5.00
SELECT x FROM Magazine x WHERE x.price BETWEEN 3.00 AND 5.00
SELECT x FROM Magazine x WHERE x.title LIKE 'J%'
SELECT x FROM Magazine x WHERE x.title LIKE 'J__'
SELECT x FROM Magazine x WHERE x.title IN ('JDJ', 'JavaPro', 'IT Insider')
SELECT x FROM Magazine x WHERE x.articles is empty
SELECT x FROM Magazine x WHERE x.publisher is null
SELECT x FROM Magazine x WHERE NOT(x.price = 10.0)
SELECT x FROM Magazine x WHERE x.TITLE = 'JDJ'
SELECT x FROM Magazine x WHERE x.title = 'JDJ' OR x.title = 'JavaPro'
SELECT x FROM Magazine x WHERE x.price > 3.00 AND x.price <= 5.00
SELECT x FROM Magazine x WHERE x.price <> 3.00
SELECT x FROM Magazine x WHERE (x.price > 3.00 AND x.price <= 5.00) OR x.price = 7.00
SELECT x FROM Magazine x WHERE x.price > 3.00 AND (x.price <= 5.00 OR x.price = 7.00)
SELECT x FROM Magazine x WHERE x.price >= 3.00 AND x.price <= 5.00
SELECT x FROM Magazine x WHERE x.price BETWEEN 3.00 AND 5.00
SELECT x FROM Magazine x WHERE x.title LIKE 'J%'
SELECT x FROM Magazine x WHERE x.title LIKE 'J__'
SELECT x FROM Magazine x WHERE x.title IN ('JDJ', 'JavaPro', 'IT Insider')
SELECT x FROM Magazine x WHERE x.articles is empty
SELECT x FROM Magazine x WHERE x.publisher is null
SELECT x FROM Magazine x WHERE NOT(x.price = 10.0)
6.1.2 Relation Traversal
可以通过类似Java的语法来遍历对象间的关系。例如Magazine中有个Publisher类型的属性publisher,那么可以通过以下方式编写JPQL query:
Sql代码
SELECT x FROM Magazine x WHERE x.publisher.name = 'Random House'
SELECT x FROM Magazine x WHERE x.publisher.name = 'Random House' 以上的遍历中假设关系不是null。在SQL术语中类似于inner join。如果你希望包含为null 的关系,那么可以如下指定: Sql代码
SELECT x FROM Magazine x WHERE x.publisher.name = 'Random House' or x.publisher is null
SELECT x FROM Magazine x WHERE x.publisher.name = 'Random House' or x.publisher is null 也可以在query中遍历集合字段,但是必须首先在from子句中定义遍历相关的identification variable,例如: Sql代码
SELECT x FROM Magazine x, IN(x.articles) y WHERE y.authorName = 'John Doe'
SELECT x FROM Magazine x, IN(x.articles) y WHERE y.authorName = 'John Doe' IN() 类型的语法可以使用inner join关键词,例如以下两个JPQL等效: Sql代码
SELECT x FROM Magazine x, IN(x.articles) y WHERE y.authorName = 'John Doe'
SELECT x FROM Magazine x inner join x.articles y WHERE y.authorName = 'John Doe'
SELECT x FROM Magazine x, IN(x.articles) y WHERE y.authorName = 'John Doe'
SELECT x FROM Magazine x inner join x.articles y WHERE y.authorName = 'John Doe'
6.1.3 Fetch Joins
JPQL query中可以指定一个或多个join fetch来指定哪些字段被pre-fetched,以5.6.1中的Publisher和Magazine为例:
Java代码
em = entityManagerFactory.createEntityManager();
Query q2 = em.createQuery("SELECT x FROM Publisher x WHERE x.id = 1");
List<Publisher> r2 = (List<Publisher>)q2.getResultList();
em.close();
for(Iterator<Publisher> iter = r2.iterator(); iter.hasNext(); ) {
System.out.println(iter.next().toString());
}
em = entityManagerFactory.createEntityManager();
Query q2 = em.createQuery("SELECT x FROM Publisher x WHERE x.id = 1");
List<Publisher> r2 = (List<Publisher>)q2.getResultList();
em.close();
for(Iterator<Publisher> iter = r2.iterator(); iter.hasNext(); ) {
System.out.println(iter.next().toString());
} 以上代码执行后的输出是:
id: 1, name: publisher1, grade: null, magazines[]
Java代码
em = entityManagerFactory.createEntityManager();
Query q3 = em.createQuery("SELECT x FROM Publisher x join fetch x.grade join fetch x.magazines WHERE x.id = 1");
List<Publisher> r3 = (List<Publisher>)q3.getResultList();
em.close();
for(Iterator<Publisher> iter = r3.iterator(); iter.hasNext(); ) {
System.out.println(iter.next().toString());
}
em = entityManagerFactory.createEntityManager();
Query q3 = em.createQuery("SELECT x FROM Publisher x join fetch x.grade join fetch x.magazines WHERE x.id = 1");
List<Publisher> r3 = (List<Publisher>)q3.getResultList();
em.close();
for(Iterator<Publisher> iter = r3.iterator(); iter.hasNext(); ) {
System.out.println(iter.next().toString());
} 以上代码执行后的输出是:
id: 1, name: publisher1, grade: excellent, magazines[isbn: isbn1, title: title1; isbn: isbn1, title: title1; isbn: isbn2, title: title2; isbn: isbn2, title: title2]
6.1.4 JPQL Functions
JPQL 支持一系列预定义的标量函数,例如:
Sql代码
SELECT x FROM Magazine x WHERE CONCAT(x.title, 's') = 'JDJs'
SELECT x FROM Magazine x WHERE SUBSTRING(x.title, 1, 1) = 'J'
SELECT x FROM Magazine x WHERE TRIM(BOTH 'J' FROM x.title) = 'D'
SELECT x FROM Magazine x WHERE LOWER(x.title) = 'jdj'
SELECT x FROM Magazine x WHERE UPPER(x.title) = 'JAVAPRO'
SELECT x FROM Magazine x WHERE LENGTH(x.title) = 3
SELECT x FROM Magazine x WHERE LOCATE('D', x.title) = 2
SELECT x FROM Magazine x WHERE ABS(x.price) >= 5.00
SELECT x FROM Magazine x WHERE SQRT(x.price) >= 1.00
SELECT x FROM Magazine x WHERE MOD(x.price, 10) = 0
SELECT x FROM Magazine x WHERE CONCAT(x.title, 's') = 'JDJs'
SELECT x FROM Magazine x WHERE SUBSTRING(x.title, 1, 1) = 'J'
SELECT x FROM Magazine x WHERE TRIM(BOTH 'J' FROM x.title) = 'D'
SELECT x FROM Magazine x WHERE LOWER(x.title) = 'jdj'
SELECT x FROM Magazine x WHERE UPPER(x.title) = 'JAVAPRO'
SELECT x FROM Magazine x WHERE LENGTH(x.title) = 3
SELECT x FROM Magazine x WHERE LOCATE('D', x.title) = 2
SELECT x FROM Magazine x WHERE ABS(x.price) >= 5.00
SELECT x FROM Magazine x WHERE SQRT(x.price) >= 1.00
SELECT x FROM Magazine x WHERE MOD(x.price, 10) = 0
6.1.5 Polymorphic Queries
JPQL from子句中的entity class不仅包含这个类本身,而且还包含这个类及其子类。以下是个关于polymorphic query的简单例子:
Java代码
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="Class", discriminatorType=DiscriminatorType.STRING)
public abstract class BankingAccount {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected int id;
}
@Entity
@DiscriminatorValue("CC")
public class CreditCard extends BankingAccount {
@Basic
private BigInteger limitAmount;
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("credit card, limit amount: ").append(limitAmount);
return sb.toString();
}
}
@Entity
@DiscriminatorValue("DC")
public class DebitCard extends BankingAccount {
@Basic
private BigInteger balanceAmount;
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("debit card, balance amount: ").append(balanceAmount);
return sb.toString();
}
}
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="Class", discriminatorType=DiscriminatorType.STRING)
public abstract class BankingAccount {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected int id;
}
@Entity
@DiscriminatorValue("CC")
public class CreditCard extends BankingAccount {
@Basic
private BigInteger limitAmount;
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("credit card, limit amount: ").append(limitAmount);
return sb.toString();
}
}
@Entity
@DiscriminatorValue("DC")
public class DebitCard extends BankingAccount {
@Basic
private BigInteger balanceAmount;
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("debit card, balance amount: ").append(balanceAmount);
return sb.toString();
}
} 假设目前数据库中的数据如下:
Sql代码
select * from bankingaccount;
+----+-------+-------------+---------------+
| id | Class | limitAmount | balanceAmount |
+----+-------+-------------+---------------+
| 5 | CC | 1000 | NULL |
| 6 | DC | NULL | 2000 |
+----+-------+-------------+---------------+
select * from bankingaccount;
+----+-------+-------------+---------------+
| id | Class | limitAmount | balanceAmount |
+----+-------+-------------+---------------+
| 5 | CC | 1000 | NULL |
| 6 | DC | NULL | 2000 |
+----+-------+-------------+---------------+
Java代码
EntityManager em = entityManagerFactory.createEntityManager();
Query q1 = em.createQuery("SELECT x FROM BankingAccount x");
List<BankingAccount> r1 = (List<BankingAccount>)q1.getResultList();
for(Iterator<BankingAccount> iter = r1.iterator(); iter.hasNext(); ) {
System.out.println(iter.next().toString());
}
em.close();
EntityManager em = entityManagerFactory.createEntityManager();
Query q1 = em.createQuery("SELECT x FROM BankingAccount x");
List<BankingAccount> r1 = (List<BankingAccount>)q1.getResultList();
for(Iterator<BankingAccount> iter = r1.iterator(); iter.hasNext(); ) {
System.out.println(iter.next().toString());
}
em.close(); 以上代码的执行结果如下:
credit card, limit amount: 1000
debit card, balance amount: 2000
6.1.6 Query Parameters
JPQL支持两种类型的query参数:named parameters 或 positional parameters。在一个JPQL query中不能同时使用两种类型的参数。以下是两个简单的例子:
Java代码
Query q = em.createQuery("SELECT x FROM Magazine x WHERE x.title = ?1 and x.price > ?2");
q.setParameter(1, "JDJ").setParameter(2, 5.0);
List<Magazine> results = (List<Magazine>) q.getResultList();
Query q = em.createQuery("SELECT x FROM Magazine x WHERE x.title = :titleParam and x.price > :priceParam");
q.setParameter("titleParam", "JDJ").setParameter("priceParam", 5.0);
List<Magazine> results = (List<Magazine>) q.getResultList();
Query q = em.createQuery("SELECT x FROM Magazine x WHERE x.title = ?1 and x.price > ?2");
q.setParameter(1, "JDJ").setParameter(2, 5.0);
List<Magazine> results = (List<Magazine>) q.getResultList();
Query q = em.createQuery("SELECT x FROM Magazine x WHERE x.title = :titleParam and x.price > :priceParam");
q.setParameter("titleParam", "JDJ").setParameter("priceParam", 5.0);
List<Magazine> results = (List<Magazine>) q.getResultList();
6.1.7 Query Hints
JPQL支持通过name/value对来指定query hints。例如:
Java代码
Query q = em.createQuery("select m from Magazine m where ... ");
q.setHint("openjpa.hint.OptimizeResultCount", new Integer(2));
q.setHint("openjpa.FetchPlan.ReadLockMode","WRITE");
List r = q.getResultList();
Query q = em.createQuery("select m from Magazine m where ... ");
q.setHint("openjpa.hint.OptimizeResultCount", new Integer(2));
q.setHint("openjpa.FetchPlan.ReadLockMode","WRITE");
List r = q.getResultList();
6.1.8 Ordering
JPQL支持根据一个或者多个字段对查询结果进行排序,例如:
Sql代码
SELECT x FROM Magazine x order by x.title asc, x.price desc
SELECT x FROM Magazine x order by x.title asc, x.price desc
6.1.9 Aggregates
JPQL支持min、max、avg和count等聚集函数(也被称为列函数)。以下是一些例子:
Java代码
Query q = em.createQuery("SELECT AVG(x.price) FROM Magazine x");
Query q = em.createQuery("SELECT SUM(DISTINCT x.price) FROM Magazine x");
Query q = em.createQuery("SELECT MAX(x.price) FROM Magazine x WHERE x.title = 'JDJ'");
Query q = em.createQuery("SELECT AVG(x.price) FROM Magazine x");
Query q = em.createQuery("SELECT SUM(DISTINCT x.price) FROM Magazine x");
Query q = em.createQuery("SELECT MAX(x.price) FROM Magazine x WHERE x.title = 'JDJ'");
6.1.10 Named Queries
JPQL支持通过NamedQuery 和NamedQueries 这两个annotation来声明命名query。以下是一个例子:
Java代码
@Entity
@NamedQueries({
@NamedQuery(name="magsOverPrice",
query="SELECT x FROM Magazine x WHERE x.price > ?1"),
@NamedQuery(name="magsByTitle",
query="SELECT x FROM Magazine x WHERE x.title = :titleParam")
})
public class Magazine {
...
}
Query q = em.createNamedQuery("magsOverPrice");
q.setParameter(1, 5.0f);
List<Magazine> results = (List<Magazine>) q.getResultList();
Query q = em.createNamedQuery("magsByTitle");
q.setParameter("titleParam", "JDJ");
List<Magazine> results = (List<Magazine>) q.getResultList();
@Entity
@NamedQueries({
@NamedQuery(name="magsOverPrice",
query="SELECT x FROM Magazine x WHERE x.price > ?1"),
@NamedQuery(name="magsByTitle",
query="SELECT x FROM Magazine x WHERE x.title = :titleParam")
})
public class Magazine {
...
}
Query q = em.createNamedQuery("magsOverPrice");
q.setParameter(1, 5.0f);
List<Magazine> results = (List<Magazine>) q.getResultList();
Query q = em.createNamedQuery("magsByTitle");
q.setParameter("titleParam", "JDJ");
List<Magazine> results = (List<Magazine>) q.getResultList();
6.1.11 Delete By Query
Query可以用来高效地删除对象(不必将每个对象查询之后再进行删除,而是直接进行批量删除)。以下是个简单的例子:
Java代码
Query q = em.createQuery("DELETE FROM Subscription s WHERE s.subscriptionDate < :today");
q.setParameter("today", new Date());
int deleted = q.executeUpdate();
Query q = em.createQuery("DELETE FROM Subscription s WHERE s.subscriptionDate < :today");
q.setParameter("today", new Date());
int deleted = q.executeUpdate(); executeUpdate方法的返回值是删除对象的个数。
6.1.12 Update By Query
跟delete类似,Query也可以用来高效地更新对象。以下是个简单的例子:
Java代码
Query q = em.createQuery("UPDATE Subscription s SET s.paid = :paid WHERE s.subscriptionDate < :today");
q.setParameter("today", new Date());
q.setParameter("paid", true);
int updated = q.executeUpdate();
Query q = em.createQuery("UPDATE Subscription s SET s.paid = :paid WHERE s.subscriptionDate < :today");
q.setParameter("today", new Date());
q.setParameter("paid", true);
int updated = q.executeUpdate();
6.1.13 Subquery
目前JPA规范支持在WHERE子句和HAVING子句中使用子查询,未来可能会支持FROM子句中使用子查询。以下是一些例子:
Sql代码
SELECT DISTINCT auth FROM Author auth WHERE EXISTS (SELECT spouseAuthor FROM Author spouseAuthor WHERE spouseAuthor = auth.spouse)
SELECT auth FROM Author auth WHERE auth.salary >= ALL(SELECT a.salary FROM Author a WHERE a.magazine = auth.magazine)
SELECT goodPublisher FROM Publisher goodPublisher WHERE goodPublisher.revenue < (SELECT AVG(p.revenue) FROM Publisher p)
SELECT mag FROM Magazine mag WHERE (SELECT COUNT(art) FROM mag.articles art) > 10
SELECT DISTINCT auth FROM Author auth WHERE EXISTS (SELECT spouseAuthor FROM Author spouseAuthor WHERE spouseAuthor = auth.spouse)
SELECT auth FROM Author auth WHERE auth.salary >= ALL(SELECT a.salary FROM Author a WHERE a.magazine = auth.magazine)
SELECT goodPublisher FROM Publisher goodPublisher WHERE goodPublisher.revenue < (SELECT AVG(p.revenue) FROM Publisher p)
SELECT mag FROM Magazine mag WHERE (SELECT COUNT(art) FROM mag.articles art) > 10
6.1.14 JPQL Constructor Expressions
在SELECT子句中可以通过使用constructor来返回一个或多个实例。指定的类不必是entity class,例如:
Sql代码
SELECT NEW com.company.PublisherInfo(pub.id, pub.revenue, mag.price) FROM Publisher pub JOIN pub.magazines mag WHERE mag.price > 5.00
SELECT NEW com.company.PublisherInfo(pub.id, pub.revenue, mag.price) FROM Publisher pub JOIN pub.magazines mag WHERE mag.price > 5.00
6.2 SQL Queries
JPA支持通过Query接口的以下两个方法使用SQL查询:
Java代码
public Query createNativeQuery(String sqlString, Class resultClass);
public Query createNativeQuery(String sqlString, String resultSetMapping);
public Query createNativeQuery(String sqlString, Class resultClass);
public Query createNativeQuery(String sqlString, String resultSetMapping); OpenJPA也支持使用存储过程。OpenJPA假定任何不以SELECT开头的SQL为存储过程的调用。
在指定resultClass的情况下,sqlString必须查询指定resultClass的 primary key 列、 discriminator 列 (如果存在) 和version column (如果存在)。JPA使用以上三个列的信息来判断object identity、区分查询子类和检查并发修改。 以下是个简单的例子:
Java代码
Query query = em.createNativeQuery("SELECT isbn, title, price, vers FROM Magazine WHERE price > 5 AND price < 10", Magazine.class);
List<Magazine> results = (List<Magazine>) query.getResultList();
for (Magazine mag : results)
processMagazine(mag);
Query query = em.createNativeQuery("SELECT isbn, title, price, vers FROM Magazine WHERE price > 5 AND price < 10", Magazine.class);
List<Magazine> results = (List<Magazine>) query.getResultList();
for (Magazine mag : results)
processMagazine(mag);
在指定resultSetMapping的情况下,resultSetMapping引用一个定义好的SqlResultSetMapping,例如:
Java代码
@Entity
@SqlResultSetMapping(name="srsm1",
entities={@EntityResult(entityClass=Publisher.class),
@EntityResult(entityClass=Magazine.class)}
)
public class Publisher {
…
}
EntityManager em = entityManagerFactory.createEntityManager();
Query q13 = em.createNativeQuery("SELECT p.id, p.name, m.isbn, m.title FROM Publisher AS p, Magazine as m WHERE p.id = 1 AND p.id = m.publisherId", "srsm1");
List r13 = q13.getResultList();
em.close();
for(Iterator iter = r13.iterator(); iter.hasNext(); ) {
Object objects[] = (Object[])iter.next();
System.out.println("publisher: " + objects[0] + ", magazine: " + objects[1]);
}
@Entity
@SqlResultSetMapping(name="srsm1",
entities={@EntityResult(entityClass=Publisher.class),
@EntityResult(entityClass=Magazine.class)}
)
public class Publisher {
…
}
EntityManager em = entityManagerFactory.createEntityManager();
Query q13 = em.createNativeQuery("SELECT p.id, p.name, m.isbn, m.title FROM Publisher AS p, Magazine as m WHERE p.id = 1 AND p.id = m.publisherId", "srsm1");
List r13 = q13.getResultList();
em.close();
for(Iterator iter = r13.iterator(); iter.hasNext(); ) {
Object objects[] = (Object[])iter.next();
System.out.println("publisher: " + objects[0] + ", magazine: " + objects[1]);
}
当查询结果不仅包含entity,而且包含value type的时候,可以在SqlResultSetMapping中指定@ColumnResult,例如:
Java代码
@Entity
@SqlResultSetMapping(name="srsm2",
entities={@EntityResult(entityClass=Publisher.class)},
columns={@ColumnResult(name="count")}
)
public class Publisher {
…
}
EntityManager em = entityManagerFactory.createEntityManager();
Query q14 = em.createNativeQuery("SELECT p.id, count(*) AS count FROM Publisher AS p LEFT JOIN Magazine as m ON p.id = m.publisherId GROUP BY p.id", "srsm2");
List r14 = q14.getResultList();
em.close();
for(Iterator iter = r14.iterator(); iter.hasNext(); ) {
Object objects[] = (Object[])iter.next();
System.out.println("publisher: " + objects[0] + ", count: " + objects[1]);
}
6.1 JPQL Queries
6.1.1Query Basics
Java代码
public Query createQuery(String jpql);
public Query createQuery(String jpql); EntityManager上的这个方法用来根据给定的JPQL创建Query。
Java代码
public List getResultList();
public List getResultList(); Query上的这个方法用来得到query的执行结果,以下是个简单的例子:
Java代码
EntityManager em = entityManagerFactory.createEntityManager();
Query q = em.createQuery("SELECT x FROM Magazine x");
List<Magazine> results = (List<Magazine>) q.getResultList();
em.close();
EntityManager em = entityManagerFactory.createEntityManager();
Query q = em.createQuery("SELECT x FROM Magazine x");
List<Magazine> results = (List<Magazine>) q.getResultList();
em.close(); JPQL query的from子句中定义了query内部命名空间。可以将任意的标识符赋值给entities,之后就可以在query的任意位置上通过标识符引用entities。 在from子句中,关键字as是可选的。例如以下两个JPQL等效:Sql代码
SELECT x FROM Magazine x
SELECT x FROM Magazine AS x
SELECT x FROM Magazine x
SELECT x FROM Magazine AS x 当查询entities的时候,关键字object也是可选的,例如select x 和 SELECT OBJECT(x) 是等效的。JPQL中的关键字不是大小写敏感的,而entity、identifier和member names是大小写敏感的。以下是一些JPQL的例子:
Sql代码
SELECT x FROM Magazine x WHERE x.TITLE = 'JDJ'
SELECT x FROM Magazine x WHERE x.title = 'JDJ' OR x.title = 'JavaPro'
SELECT x FROM Magazine x WHERE x.price > 3.00 AND x.price <= 5.00
SELECT x FROM Magazine x WHERE x.price <> 3.00
SELECT x FROM Magazine x WHERE (x.price > 3.00 AND x.price <= 5.00) OR x.price = 7.00
SELECT x FROM Magazine x WHERE x.price > 3.00 AND (x.price <= 5.00 OR x.price = 7.00)
SELECT x FROM Magazine x WHERE x.price >= 3.00 AND x.price <= 5.00
SELECT x FROM Magazine x WHERE x.price BETWEEN 3.00 AND 5.00
SELECT x FROM Magazine x WHERE x.title LIKE 'J%'
SELECT x FROM Magazine x WHERE x.title LIKE 'J__'
SELECT x FROM Magazine x WHERE x.title IN ('JDJ', 'JavaPro', 'IT Insider')
SELECT x FROM Magazine x WHERE x.articles is empty
SELECT x FROM Magazine x WHERE x.publisher is null
SELECT x FROM Magazine x WHERE NOT(x.price = 10.0)
SELECT x FROM Magazine x WHERE x.TITLE = 'JDJ'
SELECT x FROM Magazine x WHERE x.title = 'JDJ' OR x.title = 'JavaPro'
SELECT x FROM Magazine x WHERE x.price > 3.00 AND x.price <= 5.00
SELECT x FROM Magazine x WHERE x.price <> 3.00
SELECT x FROM Magazine x WHERE (x.price > 3.00 AND x.price <= 5.00) OR x.price = 7.00
SELECT x FROM Magazine x WHERE x.price > 3.00 AND (x.price <= 5.00 OR x.price = 7.00)
SELECT x FROM Magazine x WHERE x.price >= 3.00 AND x.price <= 5.00
SELECT x FROM Magazine x WHERE x.price BETWEEN 3.00 AND 5.00
SELECT x FROM Magazine x WHERE x.title LIKE 'J%'
SELECT x FROM Magazine x WHERE x.title LIKE 'J__'
SELECT x FROM Magazine x WHERE x.title IN ('JDJ', 'JavaPro', 'IT Insider')
SELECT x FROM Magazine x WHERE x.articles is empty
SELECT x FROM Magazine x WHERE x.publisher is null
SELECT x FROM Magazine x WHERE NOT(x.price = 10.0)
6.1.2 Relation Traversal
可以通过类似Java的语法来遍历对象间的关系。例如Magazine中有个Publisher类型的属性publisher,那么可以通过以下方式编写JPQL query:
Sql代码
SELECT x FROM Magazine x WHERE x.publisher.name = 'Random House'
SELECT x FROM Magazine x WHERE x.publisher.name = 'Random House' 以上的遍历中假设关系不是null。在SQL术语中类似于inner join。如果你希望包含为null 的关系,那么可以如下指定: Sql代码
SELECT x FROM Magazine x WHERE x.publisher.name = 'Random House' or x.publisher is null
SELECT x FROM Magazine x WHERE x.publisher.name = 'Random House' or x.publisher is null 也可以在query中遍历集合字段,但是必须首先在from子句中定义遍历相关的identification variable,例如: Sql代码
SELECT x FROM Magazine x, IN(x.articles) y WHERE y.authorName = 'John Doe'
SELECT x FROM Magazine x, IN(x.articles) y WHERE y.authorName = 'John Doe' IN() 类型的语法可以使用inner join关键词,例如以下两个JPQL等效: Sql代码
SELECT x FROM Magazine x, IN(x.articles) y WHERE y.authorName = 'John Doe'
SELECT x FROM Magazine x inner join x.articles y WHERE y.authorName = 'John Doe'
SELECT x FROM Magazine x, IN(x.articles) y WHERE y.authorName = 'John Doe'
SELECT x FROM Magazine x inner join x.articles y WHERE y.authorName = 'John Doe'
6.1.3 Fetch Joins
JPQL query中可以指定一个或多个join fetch来指定哪些字段被pre-fetched,以5.6.1中的Publisher和Magazine为例:
Java代码
em = entityManagerFactory.createEntityManager();
Query q2 = em.createQuery("SELECT x FROM Publisher x WHERE x.id = 1");
List<Publisher> r2 = (List<Publisher>)q2.getResultList();
em.close();
for(Iterator<Publisher> iter = r2.iterator(); iter.hasNext(); ) {
System.out.println(iter.next().toString());
}
em = entityManagerFactory.createEntityManager();
Query q2 = em.createQuery("SELECT x FROM Publisher x WHERE x.id = 1");
List<Publisher> r2 = (List<Publisher>)q2.getResultList();
em.close();
for(Iterator<Publisher> iter = r2.iterator(); iter.hasNext(); ) {
System.out.println(iter.next().toString());
} 以上代码执行后的输出是:
id: 1, name: publisher1, grade: null, magazines[]
Java代码
em = entityManagerFactory.createEntityManager();
Query q3 = em.createQuery("SELECT x FROM Publisher x join fetch x.grade join fetch x.magazines WHERE x.id = 1");
List<Publisher> r3 = (List<Publisher>)q3.getResultList();
em.close();
for(Iterator<Publisher> iter = r3.iterator(); iter.hasNext(); ) {
System.out.println(iter.next().toString());
}
em = entityManagerFactory.createEntityManager();
Query q3 = em.createQuery("SELECT x FROM Publisher x join fetch x.grade join fetch x.magazines WHERE x.id = 1");
List<Publisher> r3 = (List<Publisher>)q3.getResultList();
em.close();
for(Iterator<Publisher> iter = r3.iterator(); iter.hasNext(); ) {
System.out.println(iter.next().toString());
} 以上代码执行后的输出是:
id: 1, name: publisher1, grade: excellent, magazines[isbn: isbn1, title: title1; isbn: isbn1, title: title1; isbn: isbn2, title: title2; isbn: isbn2, title: title2]
6.1.4 JPQL Functions
JPQL 支持一系列预定义的标量函数,例如:
Sql代码
SELECT x FROM Magazine x WHERE CONCAT(x.title, 's') = 'JDJs'
SELECT x FROM Magazine x WHERE SUBSTRING(x.title, 1, 1) = 'J'
SELECT x FROM Magazine x WHERE TRIM(BOTH 'J' FROM x.title) = 'D'
SELECT x FROM Magazine x WHERE LOWER(x.title) = 'jdj'
SELECT x FROM Magazine x WHERE UPPER(x.title) = 'JAVAPRO'
SELECT x FROM Magazine x WHERE LENGTH(x.title) = 3
SELECT x FROM Magazine x WHERE LOCATE('D', x.title) = 2
SELECT x FROM Magazine x WHERE ABS(x.price) >= 5.00
SELECT x FROM Magazine x WHERE SQRT(x.price) >= 1.00
SELECT x FROM Magazine x WHERE MOD(x.price, 10) = 0
SELECT x FROM Magazine x WHERE CONCAT(x.title, 's') = 'JDJs'
SELECT x FROM Magazine x WHERE SUBSTRING(x.title, 1, 1) = 'J'
SELECT x FROM Magazine x WHERE TRIM(BOTH 'J' FROM x.title) = 'D'
SELECT x FROM Magazine x WHERE LOWER(x.title) = 'jdj'
SELECT x FROM Magazine x WHERE UPPER(x.title) = 'JAVAPRO'
SELECT x FROM Magazine x WHERE LENGTH(x.title) = 3
SELECT x FROM Magazine x WHERE LOCATE('D', x.title) = 2
SELECT x FROM Magazine x WHERE ABS(x.price) >= 5.00
SELECT x FROM Magazine x WHERE SQRT(x.price) >= 1.00
SELECT x FROM Magazine x WHERE MOD(x.price, 10) = 0
6.1.5 Polymorphic Queries
JPQL from子句中的entity class不仅包含这个类本身,而且还包含这个类及其子类。以下是个关于polymorphic query的简单例子:
Java代码
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="Class", discriminatorType=DiscriminatorType.STRING)
public abstract class BankingAccount {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected int id;
}
@Entity
@DiscriminatorValue("CC")
public class CreditCard extends BankingAccount {
@Basic
private BigInteger limitAmount;
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("credit card, limit amount: ").append(limitAmount);
return sb.toString();
}
}
@Entity
@DiscriminatorValue("DC")
public class DebitCard extends BankingAccount {
@Basic
private BigInteger balanceAmount;
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("debit card, balance amount: ").append(balanceAmount);
return sb.toString();
}
}
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="Class", discriminatorType=DiscriminatorType.STRING)
public abstract class BankingAccount {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected int id;
}
@Entity
@DiscriminatorValue("CC")
public class CreditCard extends BankingAccount {
@Basic
private BigInteger limitAmount;
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("credit card, limit amount: ").append(limitAmount);
return sb.toString();
}
}
@Entity
@DiscriminatorValue("DC")
public class DebitCard extends BankingAccount {
@Basic
private BigInteger balanceAmount;
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("debit card, balance amount: ").append(balanceAmount);
return sb.toString();
}
} 假设目前数据库中的数据如下:
Sql代码
select * from bankingaccount;
+----+-------+-------------+---------------+
| id | Class | limitAmount | balanceAmount |
+----+-------+-------------+---------------+
| 5 | CC | 1000 | NULL |
| 6 | DC | NULL | 2000 |
+----+-------+-------------+---------------+
select * from bankingaccount;
+----+-------+-------------+---------------+
| id | Class | limitAmount | balanceAmount |
+----+-------+-------------+---------------+
| 5 | CC | 1000 | NULL |
| 6 | DC | NULL | 2000 |
+----+-------+-------------+---------------+
Java代码
EntityManager em = entityManagerFactory.createEntityManager();
Query q1 = em.createQuery("SELECT x FROM BankingAccount x");
List<BankingAccount> r1 = (List<BankingAccount>)q1.getResultList();
for(Iterator<BankingAccount> iter = r1.iterator(); iter.hasNext(); ) {
System.out.println(iter.next().toString());
}
em.close();
EntityManager em = entityManagerFactory.createEntityManager();
Query q1 = em.createQuery("SELECT x FROM BankingAccount x");
List<BankingAccount> r1 = (List<BankingAccount>)q1.getResultList();
for(Iterator<BankingAccount> iter = r1.iterator(); iter.hasNext(); ) {
System.out.println(iter.next().toString());
}
em.close(); 以上代码的执行结果如下:
credit card, limit amount: 1000
debit card, balance amount: 2000
6.1.6 Query Parameters
JPQL支持两种类型的query参数:named parameters 或 positional parameters。在一个JPQL query中不能同时使用两种类型的参数。以下是两个简单的例子:
Java代码
Query q = em.createQuery("SELECT x FROM Magazine x WHERE x.title = ?1 and x.price > ?2");
q.setParameter(1, "JDJ").setParameter(2, 5.0);
List<Magazine> results = (List<Magazine>) q.getResultList();
Query q = em.createQuery("SELECT x FROM Magazine x WHERE x.title = :titleParam and x.price > :priceParam");
q.setParameter("titleParam", "JDJ").setParameter("priceParam", 5.0);
List<Magazine> results = (List<Magazine>) q.getResultList();
Query q = em.createQuery("SELECT x FROM Magazine x WHERE x.title = ?1 and x.price > ?2");
q.setParameter(1, "JDJ").setParameter(2, 5.0);
List<Magazine> results = (List<Magazine>) q.getResultList();
Query q = em.createQuery("SELECT x FROM Magazine x WHERE x.title = :titleParam and x.price > :priceParam");
q.setParameter("titleParam", "JDJ").setParameter("priceParam", 5.0);
List<Magazine> results = (List<Magazine>) q.getResultList();
6.1.7 Query Hints
JPQL支持通过name/value对来指定query hints。例如:
Java代码
Query q = em.createQuery("select m from Magazine m where ... ");
q.setHint("openjpa.hint.OptimizeResultCount", new Integer(2));
q.setHint("openjpa.FetchPlan.ReadLockMode","WRITE");
List r = q.getResultList();
Query q = em.createQuery("select m from Magazine m where ... ");
q.setHint("openjpa.hint.OptimizeResultCount", new Integer(2));
q.setHint("openjpa.FetchPlan.ReadLockMode","WRITE");
List r = q.getResultList();
6.1.8 Ordering
JPQL支持根据一个或者多个字段对查询结果进行排序,例如:
Sql代码
SELECT x FROM Magazine x order by x.title asc, x.price desc
SELECT x FROM Magazine x order by x.title asc, x.price desc
6.1.9 Aggregates
JPQL支持min、max、avg和count等聚集函数(也被称为列函数)。以下是一些例子:
Java代码
Query q = em.createQuery("SELECT AVG(x.price) FROM Magazine x");
Query q = em.createQuery("SELECT SUM(DISTINCT x.price) FROM Magazine x");
Query q = em.createQuery("SELECT MAX(x.price) FROM Magazine x WHERE x.title = 'JDJ'");
Query q = em.createQuery("SELECT AVG(x.price) FROM Magazine x");
Query q = em.createQuery("SELECT SUM(DISTINCT x.price) FROM Magazine x");
Query q = em.createQuery("SELECT MAX(x.price) FROM Magazine x WHERE x.title = 'JDJ'");
6.1.10 Named Queries
JPQL支持通过NamedQuery 和NamedQueries 这两个annotation来声明命名query。以下是一个例子:
Java代码
@Entity
@NamedQueries({
@NamedQuery(name="magsOverPrice",
query="SELECT x FROM Magazine x WHERE x.price > ?1"),
@NamedQuery(name="magsByTitle",
query="SELECT x FROM Magazine x WHERE x.title = :titleParam")
})
public class Magazine {
...
}
Query q = em.createNamedQuery("magsOverPrice");
q.setParameter(1, 5.0f);
List<Magazine> results = (List<Magazine>) q.getResultList();
Query q = em.createNamedQuery("magsByTitle");
q.setParameter("titleParam", "JDJ");
List<Magazine> results = (List<Magazine>) q.getResultList();
@Entity
@NamedQueries({
@NamedQuery(name="magsOverPrice",
query="SELECT x FROM Magazine x WHERE x.price > ?1"),
@NamedQuery(name="magsByTitle",
query="SELECT x FROM Magazine x WHERE x.title = :titleParam")
})
public class Magazine {
...
}
Query q = em.createNamedQuery("magsOverPrice");
q.setParameter(1, 5.0f);
List<Magazine> results = (List<Magazine>) q.getResultList();
Query q = em.createNamedQuery("magsByTitle");
q.setParameter("titleParam", "JDJ");
List<Magazine> results = (List<Magazine>) q.getResultList();
6.1.11 Delete By Query
Query可以用来高效地删除对象(不必将每个对象查询之后再进行删除,而是直接进行批量删除)。以下是个简单的例子:
Java代码
Query q = em.createQuery("DELETE FROM Subscription s WHERE s.subscriptionDate < :today");
q.setParameter("today", new Date());
int deleted = q.executeUpdate();
Query q = em.createQuery("DELETE FROM Subscription s WHERE s.subscriptionDate < :today");
q.setParameter("today", new Date());
int deleted = q.executeUpdate(); executeUpdate方法的返回值是删除对象的个数。
6.1.12 Update By Query
跟delete类似,Query也可以用来高效地更新对象。以下是个简单的例子:
Java代码
Query q = em.createQuery("UPDATE Subscription s SET s.paid = :paid WHERE s.subscriptionDate < :today");
q.setParameter("today", new Date());
q.setParameter("paid", true);
int updated = q.executeUpdate();
Query q = em.createQuery("UPDATE Subscription s SET s.paid = :paid WHERE s.subscriptionDate < :today");
q.setParameter("today", new Date());
q.setParameter("paid", true);
int updated = q.executeUpdate();
6.1.13 Subquery
目前JPA规范支持在WHERE子句和HAVING子句中使用子查询,未来可能会支持FROM子句中使用子查询。以下是一些例子:
Sql代码
SELECT DISTINCT auth FROM Author auth WHERE EXISTS (SELECT spouseAuthor FROM Author spouseAuthor WHERE spouseAuthor = auth.spouse)
SELECT auth FROM Author auth WHERE auth.salary >= ALL(SELECT a.salary FROM Author a WHERE a.magazine = auth.magazine)
SELECT goodPublisher FROM Publisher goodPublisher WHERE goodPublisher.revenue < (SELECT AVG(p.revenue) FROM Publisher p)
SELECT mag FROM Magazine mag WHERE (SELECT COUNT(art) FROM mag.articles art) > 10
SELECT DISTINCT auth FROM Author auth WHERE EXISTS (SELECT spouseAuthor FROM Author spouseAuthor WHERE spouseAuthor = auth.spouse)
SELECT auth FROM Author auth WHERE auth.salary >= ALL(SELECT a.salary FROM Author a WHERE a.magazine = auth.magazine)
SELECT goodPublisher FROM Publisher goodPublisher WHERE goodPublisher.revenue < (SELECT AVG(p.revenue) FROM Publisher p)
SELECT mag FROM Magazine mag WHERE (SELECT COUNT(art) FROM mag.articles art) > 10
6.1.14 JPQL Constructor Expressions
在SELECT子句中可以通过使用constructor来返回一个或多个实例。指定的类不必是entity class,例如:
Sql代码
SELECT NEW com.company.PublisherInfo(pub.id, pub.revenue, mag.price) FROM Publisher pub JOIN pub.magazines mag WHERE mag.price > 5.00
SELECT NEW com.company.PublisherInfo(pub.id, pub.revenue, mag.price) FROM Publisher pub JOIN pub.magazines mag WHERE mag.price > 5.00
6.2 SQL Queries
JPA支持通过Query接口的以下两个方法使用SQL查询:
Java代码
public Query createNativeQuery(String sqlString, Class resultClass);
public Query createNativeQuery(String sqlString, String resultSetMapping);
public Query createNativeQuery(String sqlString, Class resultClass);
public Query createNativeQuery(String sqlString, String resultSetMapping); OpenJPA也支持使用存储过程。OpenJPA假定任何不以SELECT开头的SQL为存储过程的调用。
在指定resultClass的情况下,sqlString必须查询指定resultClass的 primary key 列、 discriminator 列 (如果存在) 和version column (如果存在)。JPA使用以上三个列的信息来判断object identity、区分查询子类和检查并发修改。 以下是个简单的例子:
Java代码
Query query = em.createNativeQuery("SELECT isbn, title, price, vers FROM Magazine WHERE price > 5 AND price < 10", Magazine.class);
List<Magazine> results = (List<Magazine>) query.getResultList();
for (Magazine mag : results)
processMagazine(mag);
Query query = em.createNativeQuery("SELECT isbn, title, price, vers FROM Magazine WHERE price > 5 AND price < 10", Magazine.class);
List<Magazine> results = (List<Magazine>) query.getResultList();
for (Magazine mag : results)
processMagazine(mag);
在指定resultSetMapping的情况下,resultSetMapping引用一个定义好的SqlResultSetMapping,例如:
Java代码
@Entity
@SqlResultSetMapping(name="srsm1",
entities={@EntityResult(entityClass=Publisher.class),
@EntityResult(entityClass=Magazine.class)}
)
public class Publisher {
…
}
EntityManager em = entityManagerFactory.createEntityManager();
Query q13 = em.createNativeQuery("SELECT p.id, p.name, m.isbn, m.title FROM Publisher AS p, Magazine as m WHERE p.id = 1 AND p.id = m.publisherId", "srsm1");
List r13 = q13.getResultList();
em.close();
for(Iterator iter = r13.iterator(); iter.hasNext(); ) {
Object objects[] = (Object[])iter.next();
System.out.println("publisher: " + objects[0] + ", magazine: " + objects[1]);
}
@Entity
@SqlResultSetMapping(name="srsm1",
entities={@EntityResult(entityClass=Publisher.class),
@EntityResult(entityClass=Magazine.class)}
)
public class Publisher {
…
}
EntityManager em = entityManagerFactory.createEntityManager();
Query q13 = em.createNativeQuery("SELECT p.id, p.name, m.isbn, m.title FROM Publisher AS p, Magazine as m WHERE p.id = 1 AND p.id = m.publisherId", "srsm1");
List r13 = q13.getResultList();
em.close();
for(Iterator iter = r13.iterator(); iter.hasNext(); ) {
Object objects[] = (Object[])iter.next();
System.out.println("publisher: " + objects[0] + ", magazine: " + objects[1]);
}
当查询结果不仅包含entity,而且包含value type的时候,可以在SqlResultSetMapping中指定@ColumnResult,例如:
Java代码
@Entity
@SqlResultSetMapping(name="srsm2",
entities={@EntityResult(entityClass=Publisher.class)},
columns={@ColumnResult(name="count")}
)
public class Publisher {
…
}
EntityManager em = entityManagerFactory.createEntityManager();
Query q14 = em.createNativeQuery("SELECT p.id, count(*) AS count FROM Publisher AS p LEFT JOIN Magazine as m ON p.id = m.publisherId GROUP BY p.id", "srsm2");
List r14 = q14.getResultList();
em.close();
for(Iterator iter = r14.iterator(); iter.hasNext(); ) {
Object objects[] = (Object[])iter.next();
System.out.println("publisher: " + objects[0] + ", count: " + objects[1]);
}
发表评论
-
EJB3 QL查询
2008-07-17 15:57 1548EJB3 QL查询 EJB3的查询语言是一种和SQL非常类似的 ... -
Table
2008-07-08 12:18 2022Table Table用来定义entity主表的name,ca ... -
Overview
2008-07-07 19:33 9471 Overview Apache OpenJPA是J ... -
Metadata
2008-07-07 19:32 13443 Metadata 通过javax.p ... -
EntityManagerFactory
2008-07-07 19:30 95574 EntityManagerFactory 4.1 Over ... -
Miscellaneous Features
2008-07-07 19:29 134410 Miscellaneous Features 10.1 ... -
Object Locking
2008-07-07 19:27 13518 Object Locking 8.1 Configurin ... -
jpa继承关系详解
2008-07-07 19:07 82337 Inheritance 对象使用引用以便关联到其它 ... -
JNDI简介
2008-05-29 17:03 1710JNDI简介 & 简单示例 ... -
ejb2 和 ejb3的区别
2008-03-31 13:39 4249EJB 3 和EJB 2.1 的区别 从整个EJB 规范的 ...
相关推荐
在SAP系统中,"QUERY报表制作和传输"是一个关键任务,主要涉及到SAP Query工具的使用。SAP Query是SAP R/3系统中的一种报告工具,它允许用户无编程地创建自定义报告,特别适合于那些需要定期生成但不需要复杂逻辑...
SAP ABAP Query 是 SAP 系统中一种用于创建自定义报表的强大工具,尤其适合那些对 SQL 不太熟悉或者没有数据库直接访问权限的用户。它提供了丰富的功能,使得开发人员可以构建复杂的数据查询,而无需编写大量的 ABAP...
SAP Query 报表是SAP系统中一种用于生成自定义报表的重要工具,它允许用户根据特定需求定制数据报告,从而提高数据分析和决策制定的效率。本操作手册旨在为SAP业务顾问和运维顾问提供关于如何分配和配置SAP Query...
"SAP Query 传输与实现" 在 SAP 系统中,Query 是一种强大且灵活的报表工具,能够满足各种业务需求。但是,在将 Query 上传到生产机后,如何实现上传和使用 Query,是一个需要注意的问题。本文将详细介绍 Query 的...
**Power Query 中文 2020最新文档** Power Query是一种强大的数据预处理工具,由微软开发,旨在简化数据获取、清洗和转换的过程。它在Excel和Power BI Desktop中都有集成,极大地提升了用户处理和分析数据的能力。...
"PL/SQL ActiveQueryBuilder" 是一个强大的数据库查询构建器,设计用于替代传统的SQL查询构建工具。这个工具的主要目的是提供一种直观、用户友好的界面,帮助开发者和数据库管理员更轻松地构建复杂的SQL查询,同时...
Power Query是Excel中的一款强大数据查询和准备工具,它允许用户轻松地从各种数据源导入、清洗和转换数据。在Excel环境中,Power Query通常被称为“获取和转换”或M语言(用于公式和脚本的查询语言)。这款插件极大...
在SAP系统中,SAP Query是一个强大的工具,允许用户自定义报告以满足特定的数据查询需求。它提供了灵活的报表设计功能,使非编程背景的用户也能创建和修改查询。在某些情况下,我们可能希望在查询结果上添加交互性,...
【Power Query 入门手册】是一本针对Excel报表自动化工具PowerQuery(简称PQ)的初学者指南。Power Query是Microsoft Excel中一个强大的数据预处理工具,它允许用户轻松地清洗、转换和整合数据,为数据分析工作提供...
**Elasticsearch插件Delete-by-Query详解** 在Elasticsearch中,删除操作通常是针对单个文档进行的,但有时我们需要删除满足特定条件的一大批文档。这时,`Delete-by-Query`插件就显得尤为重要。它允许我们通过一个...
Oracle EXP Query参数转义符的各个操作系统通用解决方法 Oracle EXP_Query参数转义符是指在使用Oracle EXP工具导出数据时,query参数中的特殊字符需要进行转义,以避免引发错误。在不同的操作系统平台上,EXP_...
这里,`$request->query`是一个`QueryBag`对象,它提供了`get()`和`getInt()`等方法来获取query参数。`get()`方法默认返回字符串,而`getInt()`则会尝试将值转换为整数,如果没有提供键,或者键不存在,它会返回指定...
Altium Designer是电子设计自动化软件,广泛应用于电路设计领域,其中的Query Language是其强大的数据过滤和编辑系统,可让用户通过特定的查询语句获取软件中的一组特定对象。本教材对Altium Designer中使用的Query ...
CriteriaQuery<YourEntityName> criteriaQuery = builder.createQuery(YourEntityName.class); Root<YourEntityName> root = criteriaQuery.from(YourEntityName.class); TypedQuery<YourEntityName> typedQuery ...
MySQL Query Browser是一款强大的数据库管理工具,专为MySQL数据库设计,提供了直观的图形用户界面,使得数据库操作变得简单易行。本教程将详细讲解其主要功能和使用方法。 1. **安装与启动** - 安装MySQL Query ...
### SAP BW 更改 Query 查询变量属性 #### 一、引言 在SAP BW (Business Warehouse)环境中,经常需要对已经定义并使用的BEx (Business Explorer)查询中的变量进行修改,尤其是在需要更改变量的“Processing by”...
《Excel PowerQuery M函数 视频教程配套笔记及M函数大全》是一份全面介绍Excel PowerQuery中M函数的宝贵资源,适用于数据分析师、业务人员以及任何希望通过Excel进行高效数据处理的用户。本教程旨在帮助学习者掌握...
《PowerQuery:提升数据处理效率的强大工具》 在当今数据密集型的社会中,高效的数据处理能力成为了企业乃至个人提升竞争力的关键。PowerQuery,这个由Microsoft开发的强大的数据查询和清洗工具,为Excel用户提供了...
"Advanced Query Tools" 是一款专为数据库查询优化设计的高级工具,主要面向IT专业人士和数据库管理员。这款软件提供了丰富的功能,旨在帮助用户更高效、精确地执行复杂的SQL查询,提高数据库管理和开发的效率。 在...