eclipse + JBoss 5 + EJB3开发指南(12):使用命名查询执行JPQL

本文为原创,如需转载,请注明作者和出处,谢谢!

上一篇: eclipse + JBoss 5 + EJB3开发指南(11):实体Bean的连接策略(JOINED Strategy)

在EJB3中可以使用EntityManager对象的createQuery方法来执行JPQL(类似于Hibernate中的HQL),这非常简单。但使用createQuery方法处理JPQL时,在每次执行JPQL的过程中系统都需要对JPQL进行分析,这在一定程度上降低了系统运行时的性能。为此,EJB3提供了命名查询的概念。命名查询有些类型于数据库中的存储过程,在提交的过程中就已经被编译处理了。因此,在执行效率上要高一些。
我们可以使用@NamedQuery注释来定义命名查询。这个注释可以放在任何一个实体Bean的上方。但为了便于管理,最好放在相关的实体Bean的上方。如下面的代码所示:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package entity;

import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name
= " t_customers " )
@NamedQuery(name
="MyQuery",query="selectcfromCustomercwhereid=:id" )
public class Customer
{
private int id;
private Stringname;
private Refereereferee;
private Collection < Order > orders;
private Collection < Address > addresses;

@OneToOne(cascade
= CascadeType.ALL)
@PrimaryKeyJoinColumn
public RefereegetReferee()
{
return referee;
}

public void setReferee(Refereereferee)
{
this .referee = referee;
}

@ManyToMany(cascade
= CascadeType.PERSIST,fetch = FetchType.LAZY)
@JoinTable(name
= " t_customers_addresses " ,
joinColumns
= @JoinColumn(name = " customer_id " ,referencedColumnName = " id " ),
inverseJoinColumns
= @JoinColumn(name = " address_id " ,referencedColumnName = " id " ))
public Collection < Address > getAddresses()
{
return addresses;
}

public void setAddresses(Collection < Address > addresses)
{
this .addresses = addresses;
}

@OneToMany(mappedBy
= " customer " ,cascade = CascadeType.ALL)
public Collection < Order > getOrders()
{
return orders;
}

public void setOrders(Collection < Order > orders)
{
this .orders = orders;
}

@Id
@GeneratedValue(strategy
= GenerationType.IDENTITY)
public int getId()
{
return id;
}

public void setId( int id)
{
this .id = id;
}

public StringgetName()
{
return name;
}

public void setName(Stringname)
{
this .name = name;
}
}

代码中的命名查询使用了命名参数。我们可以使用下面的代码来执行该JPQL:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> private CustomerqueryCustomer( int id)
{
return (Customer)em.createNamedQuery( " MyQuery " ).setParameter( " id " , 23 )
.getSingleResult();
}

在Session Bean中调用queryCustomer就可以通过命名查询获得相应的Customer对象了。

下一篇: eclipse + JBoss 5 + EJB3开发指南(13):在Servlet中访问应用程序管制EntityManager对象

国内最棒的Google Android技术社区(eoeandroid),欢迎访问!

《银河系列原创教程》发布

《Java Web开发速学宝典》出版,欢迎定购

你可能感兴趣的:(eclipse,bean,android,jboss,出版)