JPA - Persistence Properties

Card Puncher Data Processing

About

persistence.properties are database and JPA provider specific.

Configuration

You can configure a properties (such as database connections) using:

Properties file

final Properties persistenceProperties = new Properties();
InputStream is = null;

try {
	is = getClass().getClassLoader().getResourceAsStream("persistence.properties");
	persistenceProperties.load(is);
} finally {
	if (is != null) {
		try {
			is.close();
		} catch (IOException ignored) {
		}
	}
}

entityManagerFactory = Persistence.createEntityManagerFactory("generated", persistenceProperties);

Programmatically

Map props = new HashMap();

props.put(PersistenceUnitProperties.JDBC_USER, "user-name");
props.put(PersistenceUnitProperties.JDBC_PASSWORD, "password");

EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu-name", props); 

JPA Providers

  • javax.persistence.transactionType - Standard JPA PersistenceUnitTransactionType property, “JTA” or “RESOURCE_LOCAL”.
  • javax.persistence.jtaDataSource - Standard JPA JTA DataSource name.
  • javax.persistence.nonJtaDataSource - Standard JPA non-JTA DataSource name.
  • javax.persistence.jdbc.driver“ - Standard JPA 2.0 JDBC driver class name for JSE deployments (was “eclipselink.jdbc.driver” in EclipseLink 1.1)
  • javax.persistence.jdbc.url - Standard JPA 2.0 JDBC URL for JSE deployments (was “eclipselink.jdbc.url” in EclipseLink 1.1).
  • javax.persistence.jdbc.user - Standard JPA 2.0 database user for JSE deployments (was “eclipselink.jdbc.user” in EclipseLink 1.1).
  • javax.persistence.jdbc.password - Standard JPA 2.0 database password for JSE deployments (was “eclipselink.jdbc.password” in EclipseLink 1.1).

See : Class PersistenceUnitProperties for a list of all properties.

Hibernate

Most important Hibernate JDBC properties

  • connection.driver_class, connection.url, connection.username and connection.password property define the JDBC connection information.
  • hibernate.connection.pool_size
hibernate.connection.driver_class=oracle.jdbc.OracleDriver
hibernate.connection.url=jdbc:oracle:thin:@//localhost:1521/orcl.HotITem.local
hibernate.connection.username=test
hibernate.connection.password=test
hibernate.hbm2ddl.auto=create-drop
hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider
hibernate.jdbc.batch_size=0
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

The hbm2ddl.auto property enables automatic generation of database schemas directly into the database.

Note the hibernate.hbm2ddl.auto=create-drop property in the list above instructs Hibernate to initalize the database. This feature is extremely useful for testing as you don't have to take care of the database schema, JPA provider will do it for you. But don't forget to remove this setting from productional configurations or you may loose or corrupt your data if you forget it.

Ter info:

toplink.jdbc.driver=oracle.jdbc.OracleDriver
toplink.jdbc.url=jdbc:oracle:thin:@//localhost:1521/orcl.HotITem.local
toplink.jdbc.user=test
toplink.jdbc.password=test
toplink.ddl-generation=create-tables
toplink.target-database=oracle.toplink.essentials.platform.database.oracle.OraclePlatform
toplink.create-ddl-jdbc-file-name=target/test-database/create.ddl
toplink.drop-ddl-jdbc-file-name=target/test-database/drop.ddl
toplink.logging.level=ALL





Discover More
Card Puncher Data Processing
JPA - Getting Started - Hello World

A simple JPA implementation that insert an “Hello World” string in a HELLO table in a relational database. This example uses the JPA reference implementation: against an Oracle Database. Maven...
Card Puncher Data Processing
JPA - Open JPA

Persistence.Xml Provider: Persistence Properties: Persistence.Xml Dynamically
Card Puncher Data Processing
JPA - persistence.xml

The JPA specification requires the use of a persistence.xml file for deployment. The persistence configuration file must be named “META-INF/persistence.xml” in the persistence archive. This file...
Hyperjaxb Generated Source
Java - HyperJaxb - From Xml (Jaxb) to database (Jpa)

HyperJaxb is a Jaxb plugin that creates classes that are: JAXB-enabled (thanks to the JAXB schema compiler) and JPA-enabled (thanks to the Hyperjaxb3 Jaxb plugin) HyperJaxb permits then to have...
Card Puncher Data Processing
Persistence Unit

the node provider property See chapter 8.1 of the specification



Share this page:
Follow us:
Task Runner