Hibernate entity manager and persistence.xml

Hibernate entity manager and persistence.xml 

javax.persistence.jdbc.driver: The JDBC driver class
H2 – org.h2.Driver
Oracle – oracle.jdbc.driver.OracleDriver
MySql – com.mysql.jdbc.Driver

javax.persistence.jdbc.url: The JDBC URL to the database instance.
H2 – jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE
Oracle – jdbc:oracle:thin:@127.0.0.1:1521:DBNAME
MySql – jdbc:mysql://localhost/DBNAME

javax.persistence.jdbc.user: The database user name

javax.persistence.jdbc.password: The database user password

Other hibernate properties can be used as required like below
<property name=”hibernate.show_sql” value=”true” />

Example:

Source: Employee Manager

Hibernat basic: annotations

Hibernate basic without annotations

@Entity [javax.persistence.Entity]: Specifies that the class is an entity
-name: (Optional) The entity name. Defaults to the unqualified name of the entity class. This name is used to refer to the entity in queries. The name must not be a reserved literal in the Java Persistence query language

@Table [javax.persistence.Table]: Specifies the primary table for the annotated entity. Additional tables may be specified using SecondaryTable or SecondaryTables annotation. If no Table annotation is specified for an entity class, the default values apply.
-name: (Optional) The name of the table
-catalog: (Optional) The catalog of the table
-schema: (Optional) The schema of the table.
-uniqueConstraints: (Optional) Unique constraints that are to be placed on the table.

@Column [javax.persistence.Column]: Specifies the mapped column for a persistent property or field.
-String name: (Optional) The name of the column. Defaults to the property or field name.
-boolean unique: (Optional) Whether the column is a unique key. This is a shortcut for the UniqueConstraint annotation at the table level and is useful for when the unique key constraint corresponds to only a single column. This constraint applies in addition to any constraint entailed by primary key mapping and to constraints specified at the table level.
-boolean nullable: (Optional) Whether the database column is nullable.
-int length: (Optional) The column length. (Applies only if a string-valued column is used.) Default 255
-int precision: (Optional) The precision for a decimal (exact numeric) column. (Applies only if a decimal column is used.) Value must be set by developer if used when generating the DDL for the column.
-int scale: (Optional) The scale for a decimal (exact numeric) column. (Applies only if a decimal column is used.)
-String table: (Optional) The name of the table that contains the column. If absent the column is assumed to be in the primary table.
-String columnDefinition: (Optional) The SQL fragment that is used when generating the DDL for the column. Defaults to the generated SQL to create a column of the inferred type.
-boolean insertable: (Optional) Whether the column is included in SQL INSERT statements generated by the persistence provider.
-boolean updatable: (Optional) Whether the column is included in SQL UPDATE statements generated by the persistence provider.

@GeneratedValue [javax.persistence.GeneratedValue]:  Provides for the specification of generation strategies for the values of primary keys.
-GenerationType strategy: (Optional) The primary key generation strategy that the persistence provider must use to generate the annotated entity primary key.
-String generator: (Optional) The name of the primary key generator to use as specified in the SequenceGenerator or TableGenerator annotation. Default:javax.persistence.GenerationType.AUTO

javax.persistence.GenerationType
-AUTO: Indicates that the persistence provider should pick an appropriate strategy for the particular database.
-IDENTITY: Indicates that the persistence provider must assign primary keys for the entity using a database identity column.
-SEQUENCE: Indicates that the persistence provider must assign primary keys for the entity using a database sequence.
-TABLE: Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.

@Id [javax.persistence.Id]: Specifies the primary key of an entity. The field or property to which the Id annotation is applied should be one of the following types: any Java primitive type; any primitive wrapper type; String; java.util.Date; java.sql.Date; java.math.BigDecimal; java.math.BigInteger. The mapped column for the primary key of the entity is assumed to be the primary key of the primary table. If no Column annotation is specified, the primary key column name is assumed to be the name of the primary key property or field.

@Temporal [javax.persistence.Temporal]: This annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar. It may only be specified for fields or properties of these types.
-TemporalType value: The type used in mapping java.util.Date or java.util.Calendar.
javax.persistence.TemporalType: Type used to indicate a specific mapping of java.util.Date or java.util.Calendar.
DATE: Map as java.sql.Date
TIME: Map as java.sql.Time
TIMESTAMP: Map as java.sql.Timestamp

Example:

Source: Employee Manager

Details of hibernate.cfg.xml

Details of hibernate.cfg.xml

hibernate.connection.driver_class: The JDBC driver class
H2 – org.h2.Driver
Oracle – oracle.jdbc.driver.OracleDriver
MySql – com.mysql.jdbc.Driver

hibernate.connection.url: The JDBC URL to the database instance.
H2 – jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE
Oracle – jdbc:oracle:thin:@127.0.0.1:1521:DBNAME
MySql – jdbc:mysql://localhost/DBNAME

hibernate.dialect: This property makes Hibernate generate the appropriate SQL for the chosen database.
H2 – org.hibernate.dialect.H2Dialect
Oracle – org.hibernate.dialect.OracleDialect
MySql – org.hibernate.dialect.MySQLDialect

hibernate.cache.provider_class: Caching implementation to use
No Cache – org.hibernate.cache.internal.NoCacheProvider
EHCache – org.hibernate.cache.EhCacheProvider
OSCache – org.hibernate.cache.OSCacheProvider
SwarmCache – org.hibernate.cache.SwarmCacheProvider
JBoss TreeCache – org.hibernate.cache.TreeCacheProvider

hibernate.connection.username: The database user name

hibernate.connection.password: The database user password

hibernate.connection.pool_size: Limits the number of connections waiting in the Hibernate database connection pool.

hibernate.show_sql: Echo all executed SQL to stdout

hibernate.hbm2ddl.auto: Automatically validates or exports schema DDL to the database
create: creates the schema, destroying previous data
create-drop: drop the schema at the end of the session
validate: validate the schema, makes no changes to the database
update: update the schema

mapping resource: Resource mapping files

Sample XML