As mentioned previously, the TREAT operator can also be used within the WHERE clause in order to filter a query
based upon subtype attribute values. In the following example, the query will return all employees that are from the
IT department and have a status of ACTIVE . The assumption is that the ItEmployee entity is a subtype of the Employee
SELECT e FROM Employee
WHERE TREAT(e as ItEmployee).status = "ACTIVE";
Downcasting support adds yet another feature to the scope of JPA, making it even more flexible for developers to
use. This technique will make it easier to obtain values from related entities or subtypes, without the need to issue an
Schema generation refers to the creation of underlying database tables, views, constraints, and other database
artifacts. Prior to the Java EE 7 release, schema generation has only been automated via the use of an IDE such as
Netbeans or Eclipse. However, the EE 7 release takes a step towards breaking this dependency on an IDE by allowing
schema generation to become automated by configuring an appropriate persistence.xml for an application.
Schema generation can be applied directly to the database or it can generate SQL scripts that can be manually
applied to the database (or both), depending upon which options are configured for the application. Schema generation
may occur prior to application deployment, or when an EntityManagerFactory is created as part of the application
deployment and initialization. To perform schema generation, the container may call the PersistenceProvider
generateSchema method separate from and/or prior to the entity manager factory for the persistence unit. The
createContainerEntityManagerFactory call can accept additional information to cause generation of schema
constructs to occur as part of the entity manager factory creation or initialization process. Furthermore, this information
can determine whether the database is manipulated directly or if SQL scripts are created, or both.
■ Schema generation is also available outside of a managed container (eg., application server) in java Se
environments. to perform schema generation in an Se environment, the application may call the Persistence
generateSchema method separately from and/or prior to the creation of the entity manager factory or may pass
information to the createEntityManagerFactory method to cause schema generation to occur as part of the entity
manager factory creation.
Schema generation is determined by the object/relational metadata of the persistence.xml file unless custom
scripts are provided for the generation. The application developer can package scripts as part of the persistence unit
or can supply URLs to the location of the scripts for schema generation. The execution of such scripts can be carried
out by the container itself, or the container may direct the persistence provider to take care of script execution.