Java Reference
In-Depth Information
If you have multiple @NamedQuery annotations to apply to an entity, they can be provided
as an array of values of the @NamedQueries annotation.
Named Native Queries (SQL)
EJB 3 also allows the database's native query language (usually a dialect of SQL) to be used in
place of EJB QL. You risk losing portability here if you use a database-specific feature, but as
long as you use reasonably generic SQL, you should be OK. The @NamedNativeQuery annotation
is declared in almost exactly the same manner as the @NamedQuery annotation. The following
block of code shows a simple example of the use of a named native query.
@NamedNativeQuery(
name="nativeFindAuthorNames",
query="select name from author"
)
Multiple @NamedNativeQuery annotations can be grouped with the @NamedNativeQueries
annotation.
n Note Hibernate does not currently fully support named native queries.
Configuring the Annotated Classes
Once you have an annotated class, you will need to provide the class to your application's
Hibernate configuration, just as if it were an XML mapping. With annotations, you can use
either the declarative configuration in the hibernate.cfg.xml XML configuration document,
or you can programmatically add annotated classes to Hibernate's org.hibernate.cfg.
AnnotationConfiguration object. Your application may use both annotated entities and
XML mapped entities in the same configuration.
To provide declarative mapping, we use a normal hibernate.cfg.xml XML configuration
file and add the annotated classes to the mapping using the mapping element (see Listing 6-32).
Notice that we have specified the name of the annotated class as a mapping.
Listing 6-32. A Hibernate XML Configuration File with an Annotated Class
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">
org.hsqldb.jdbcDriver
</property>
Search WWH ::




Custom Search