(From my experience) check the version, then add this into pom:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.1.Final</version>
</dependency>
Another sample is given from tutorial.
Otherwise, manual library addition instruction provided.
need an id property (if need to use full feature set, and is recommended anyway);
setter method should be private
hibernate can access private / protected method anyway
default (no-arg) constructor
Basic structure as below
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.tutorial.domain">
[...]
</hibernate-mapping>
DTD is included in hibernate-core.jar; [...] goes all mappings of all persistent entity classes
Maven script for launch HSQLDB and to clean database is provided.
Configuration can be done with either
hibernate.properties
hibernate.cfg.xml, or
programmatic setup
Example:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>
First needs to obtain org.hibernate.SessionFactory
thread-safe,
global object
from which to obtain Session instance
Session
represent a single thread of work (a single atomic piece of work)
one-to-one with database transaction (a bit simplified)
To obtain and use SessionFactory
A utility class HibernateUtil is provided as sample showing how to start-up
if a name is given to SessionFactory, hibernate will register it with JNDI
other container provided mechanism can be used
Configure hibernate logging: put log4j.properties in /etc of hibernate distribution to src together with hibernate.cfg.xml
Working on session
SessionFactory.getCurrentSession();
this can be called as many times as wish anywhere
always returns the "current" unit of work
this behaviour (relate to thread) is configured in configuration xml with
<property name="current_session_context_class">thread</property>
note: "thread" is not suitable for production use, see further discussion
first call created a Session and bind to current thread (if so configured)
session.beginTransaction();
do some work
session.save(new entity);
session.createQuery().list();
session.getTransaction.commit();
unbind Session from thread (if so configured)
SessionFactory.close()
various mapping samples