Persistence provider in java

Persistence provider in java

Interface implemented by the persistence provider.

It is invoked by the container in Java EE environments and by the Persistence class in Java SE environments to create an EntityManagerFactory .

Since: Java Persistence 1.0

Method Summary
EntityManagerFactory createContainerEntityManagerFactory (PersistenceUnitInfo info, java.util.Map map)
Called by the container when an EntityManagerFactory is to be created.
EntityManagerFactory createEntityManagerFactory (java.lang.String emName, java.util.Map map)
Called by Persistence class when an EntityManagerFactory is to be created.
ProviderUtil getProviderUtil ()
Return the utility interface implemented by the persistence provider.

createEntityManagerFactory

EntityManagerFactory createEntityManagerFactory(java.lang.String emName, java.util.Map map)

Parameters: emName — the name of the persistence unit map — a Map of properties for use by the persistence provider. These properties may be used to override the values of the corresponding elements in the persistence.xml file or specify values for properties not specified in the persistence.xml (and may be null if no properties are specified). Returns: EntityManagerFactory for the persistence unit, or null if the provider is not the right provider

createContainerEntityManagerFactory

EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, java.util.Map map)

Parameters: info — metadata for use by the persistence provider map — a Map of integration-level properties for use by the persistence provider (may be null if no properties are specified). If a Bean Validation provider is present in the classpath, the container must pass the ValidatorFactory instance in the map with the key «javax.persistence.validation.factory» . Returns: EntityManagerFactory for the persistence unit specified by the metadata

Читайте также:  Grey color for html

getProviderUtil

Returns: ProviderUtil interface Since: Java Persistence 2.0

Overview Package Class Tree Deprecated Index Help
PREV CLASS NEXT CLASS FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD
Submit a bug or feature

Copyright © 2009-2011, Oracle Corporation and/or its affiliates. All Rights Reserved. Use is subject to license terms.

Generated on 10-February-2011 12:41

Источник

Chapter 7 Using the Java Persistence API

Sun Java System Application Server support for the Java Persistence API includes all required features described in the Java Persistence Specification. Although officially part of the Enterprise JavaBeans Specification v3.0, also known as JSR 220, the Java Persistence API can also be used with non-EJB components outside the EJB container.

The Java Persistence API provides an object/relational mapping facility to Java developers for managing relational data in Java applications. For basic information about the Java Persistence API, see “Part Four: Persistence” in the Java EE 5 Tutorial.

This chapter contains Application Server specific information on using the Java Persistence API in the following topics:

The default persistence provider in the Application Server is based on Oracle’s TopLink Essentials Java Persistence API implementation. All configuration options in TopLink Essentials are available to applications that use the Application Server’s default persistence provider.

Specifying the Database

The Application Server uses the bundled Java DB (Derby) database by default. If the transaction-type element is omitted or specified as JTA and both the jta-data-source and non-jta-data-source elements are omitted in the persistence.xml file, Java DB is used as a JTA data source. If transaction-type is specified as RESOURCE_LOCAL and both jta-data-source and non-jta-data-source are omitted, Java DB is used as a non-JTA data source.

To use a non-default database, either specify a value for the jta-data-source element, or set the transaction-type element to RESOURCE_LOCAL and specify a value for the non-jta-data-source element.

If you are using the default persistence provider, the provider attempts to automatically detect the database based on the connection metadata. You can specify the optional toplink.platform.class.name property to guarantee that the database is correct. For example:

The following toplink.platform.class.name property values are allowed. Supported platforms have been tested with the Application Server and are found to be Java EE compatible.

//Supported platforms oracle.toplink.essentials.platform.database.DerbyPlatform oracle.toplink.essentials.platform.database.oracle.OraclePlatform oracle.toplink.essentials.platform.database.SQLServerPlatform oracle.toplink.essentials.platform.database.DB2Platform oracle.toplink.essentials.platform.database.SybasePlatform oracle.toplink.essentials.platform.database.CloudscapePlatform oracle.toplink.essentials.platform.database.MySQL4Platform oracle.toplink.essentials.platform.database.PointBasePlatform oracle.toplink.essentials.platform.database.PostgreSQLPlatform //Others available oracle.toplink.essentials.platform.database.InformixPlatform oracle.toplink.essentials.platform.database.TimesTenPlatform oracle.toplink.essentials.platform.database.AttunityPlatform oracle.toplink.essentials.platform.database.HSQLPlatform oracle.toplink.essentials.platform.database.SQLAnyWherePlatform oracle.toplink.essentials.platform.database.DBasePlatform oracle.toplink.essentials.platform.database.DB2MainframePlatform oracle.toplink.essentials.platform.database.AccessPlatform

To use the Java Persistence API outside the EJB container (in Java SE mode), do not specify the jta-data-source or non-jta-data-source elements if the DataSource is not available. Instead, specify the provider element and any additional properties required by the JDBC driver or the database. For example:

   oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider RESOURCE_LOCAL jdbc/MyDB2DB         

For more information about toplink properties, see Additional Database Properties.

For a list of the JDBC drivers currently supported by the Application Server, see the Sun Java System Application Server 9.1 Release Notes . For configurations of supported and other drivers, see Configurations for Specific JDBC Drivers in Sun Java System Application Server 9.1 Administration Guide .

To change the persistence provider, see Changing the Persistence Provider.

Additional Database Properties

If you are using the default persistence provider, you can specify in the persistence.xml file the database properties listed at Persistence Unit Extensions in TopLink JPA Extensions Reference.

For schema generation properties, see Generation Options. For query hints, see Query Hints.

Configuring the Cache

If you are using the default persistence provider, you can configure whether caching occurs, the type of caching, the size of the cache, and whether client sessions share the cache. Caching properties for the default persistence provider are described in detail at Extensions for Caching in TopLink JPA Extensions Reference.

Setting the Logging Level

One of the default persistence provider’s database properties that you can set in the persistence.xml file is toplink.logging.level. For example, setting the logging level to FINE or higher logs all SQL statements. For details about this property, see Extensions for Logging inTopLink JPA Extensions Reference.

You can also set the TopLink Essentials logging level globally in the Application Server in any of the following ways:

asadmin set --user adminuser "server.log-service.module-log-levels.property.oracle\.toplink\.essentials"=FINE
asadmin create-jvm-options --user adminuser -Dtoplink.logging.level=FINE

Setting the logging level to OFF disables TopLink Essentials logging. A logging level set in the persistence.xml file takes precedence over the global logging level.

You can set the logging level for Java Persistence in general using the Admin Console. In the developer profile, select the Application Server component and the Logging tab. In the cluster profile, select the Logger Settings component under the relevant configuration. Select the Log Levels tab. Then set the logging level for Persistence. Setting the logging level to OFF disables Java Persistence logging.

Using Lazy Loading

The default persistence provider treats only OneToOne, ManyToOne, OneToMany, and ManyToMany mappings specially when they are annotated as LAZY. OneToMany and ManyToMany mappings are loaded lazily by default in compliance with the Java Persistence Specification. Other mappings are always loaded eagerly. For OneToOne and ManyToOne mappings, value holder indirection is used. For OneToMany and ManyToMany mappings, transparent indirection is used.

For basic information about lazy loading, see Lazy Loading in TopLink JPA Extensions Reference. For details about indirection, see Indirection in Mapping Concepts.

Primary Key Generation Defaults

In the descriptions of the @GeneratedValue, @SequenceGenerator, and @TableGenerator annotations in the Java Persistence Specification, certain defaults are noted as specific to the persistence provider. The default persistence provider’s primary key generation defaults are listed here.

@GeneratedValue defaults are as follows:

  • Using strategy=AUTO (or no strategy) creates a @TableGenerator named SEQ_GEN with default settings. Specifying a generator has no effect.
  • Using strategy=TABLE without specifying a generator creates a @TableGenerator named SEQ_GEN_TABLE with default settings. Specifying a generator but no @TableGenerator creates and names a @TableGenerator with default settings.
  • Using strategy=IDENTITY or strategy=SEQUENCE produces the same results, which are database-specific.
    • For Oracle databases, not specifying a generator creates a @SequenceGenerator named SEQ_GEN_SEQUENCE with default settings. Specifying a generator but no @SequenceGenerator creates and names a @SequenceGenerator with default settings.
    • For PostgreSQL databases, a SERIAL column named entity-table _ pk-column _SEQ is created.
    • For MySQL databases, an AUTO_INCREMENT column is created.
    • For other supported databases, an IDENTITY column is created.

    The @SequenceGenerator annotation has one default specific to the default provider. The default sequenceName is the specified name.

    @TableGenerator defaults are as follows:

    • The default table is SEQUENCE.
    • The default pkColumnName is SEQ_NAME.
    • The default valueColumnName is SEQ_COUNT.
    • The default pkColumnValue is the specified name, or the default name if no name is specified.

    Automatic Schema Generation

    The automatic schema generation feature of the Application Server defines database tables based on the fields or properties in entities and the relationships between the fields or properties. This insulates developers from many of the database related aspects of development, allowing them to focus on entity development. The resulting schema is usable as-is or can be given to a database administrator for tuning with respect to performance, security, and so on. This section covers the following topics:

    Automatic schema generation is supported on an all-or-none basis: it expects that no tables exist in the database before it is executed. It is not intended to be used as a tool to generate extra tables or constraints.

    Deployment won’t fail if all tables are not created, and undeployment won’t fail if not all tables are dropped. Instead, an error is written to the server log. This is done to allow you to investigate the problem and fix it manually. You should not rely on the partially created database schema to be correct for running the application.

    Annotations

    The following annotations are used in automatic schema generation: @AssociationOverride, @AssociationOverrides, @AttributeOverride, @AttributeOverrides, @Column, @DiscriminatorColumn, @DiscriminatorValue, @Embedded, @EmbeddedId, @GeneratedValue, @Id, @IdClass, @JoinColumn, @JoinColumns, @JoinTable, @Lob, @ManyToMany, @ManyToOne, @OneToMany, @OneToOne, @PrimaryKeyJoinColumn, @PrimaryKeyJoinColumns, @SecondaryTable, @SecondaryTables, @SequenceGenerator, @Table, @TableGenerator, @UniqueConstraint, and @Version. For information about these annotations, see the Java Persistence Specification.

    For @Column annotations, the insertable and updatable elements are not used in automatic schema generation.

    For @OneToMany and @ManyToOne annotations, no ForeignKeyConstraint is created in the resulting DDL files.

    Supported Data Types

    The following table shows mappings of Java types to SQL types when the default persistence provider and automatic schema generation are used.

    Источник

Оцените статью