to tutorial on Hibernate

advertisement
The Easiest Way To Learn
Hibernate: YouTube Videos and the
Online Tutorial
http://www.theserverside.com/discussions/thread.tss?thread_id=60191
http://www.allapplabs.com/hibernate/getting_started_with_hibernate.htm
http://stackoverflow.com/questions/2880206/java-beginner-wants-to-learn-hibernate
Java Persistence with Hibernate (Christian Bauer and Gavin King)
http://facestutorials.icefaces.org/tutorial/hibernate-tutorial.html#stepstocreate
http://www.allapplabs.com/hibernate/introduction_to_hibernate.htm
http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/
http://www.javatpoint.com/hibernate-architecture
http://www.javatpoint.com/collection-mapping
Installing Hibernate and Creating your first class
•
•
•
•
You need to install maven in eclipse > start eclipse >help > Install new software
You need to create a maven project
You need to add the pom.xml in the project root directory.
You need to add the following directories to the project
src/main/java, src/main/resources and src/main/webapp
•
THE PERSISTENT CLASS - You need to create a JavaBean and place it in the following path:
src/main/java/org/hibernate/tutorial/hibernatetutorial/Event.java
•
You need to create a table in the database called EVENTS with the columns matching with the above class.
•
THE MAPPING FILE - You need to create a mapping file which maps the javabean fields with the columns in the
table created above. Since the class file is Event the mapping file is named as Event.hbm.xml. This file is placed in
the following location: src/main/resources/org/hibernate/tutorial/hibernatetutorial/Event.hbm.xml
•
THE HIBERNATE CONFIGUATION: You need to create a configuration file for the jdbc connectivity and place it in
the following path: src/main/resources /hibernate.cfg.xml
NOTE: The mapping resource in your configuration file should point to the mapping file Event.hbm.xml
•
To compile your project: Right click your project> Run As > Maven Build >
The edit Configuration screen will show up. In the Goals field type “clean install” > Apply > Run
The compiled classes are generated inside the target folder
NOTE:The jar files are automatically downloaded when you build your project and are located in the folder Maven
dependencies
•
Create HibernateUtil.java helper class at the following
location:src/main/java/org/hibernate/tutorial/util/HibernateUtil.java
•
copy log4j.properties from the Hibernate distribution in the etc/ directory to your src directory, next to
hibernate.cfg.xml
Hibernate Architecture
• The Hibernate architecture includes many
objects persistent object, session factory,
session, transaction, transaction factory,
connection factory, etc.
• There are 4 layers in hibernate architecutre:
java application layer, hibernate framework
layer, backhand api layer and database
layer.Let's see the diagram of hibernate
architecture:
Hibernate Architecture
Objects used by Hibernate Framework
Elements of Hibernate Architecture
•
•
•
•
•
•
•
For creating the first hibernate application, we must know the elements of Hibernate architecture.
They are as follows:
SessionFactory
The SessionFactory is a factory of session and client of ConnectionProvider. It holds second level
cache (optional) of data. The org.hibernate.SessionFactory interface provides factory method to get
the object of Session.
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
Session
The session object provides an interface between the application and data stored in the database.
It is a short-lived object and wraps the JDBC connection. It is factory of Transaction, Query and
Criteria. It holds a first-level cache (mandatory) of data. The org.hibernate.Session interface
provides methods to insert, update and delete the object. It also provides factory methods for
Transaction, Query and Criteria.
save() and persist() result in an SQL INSERT, delete() in an SQL DELETE and update() or merge() in an
SQL UPDATE. Changes to persistent instances are detected at flush time and also result in an SQL
UPDATE. saveOrUpdate() and replicate() result in either an INSERT or an UPDATE.
Transaction
The transaction object specifies the atomic unit of work. It is optional. The
org.hibernate.Transaction interface provides methods for transaction management.
ConnectionProvider
It is a factory of JDBC connections. It abstracts the application from DriverManager or DataSource.
It is optional.
TransactionFactory
It is a factory of Transaction. It is optional.
Session Object
•
The main function of the Session is to offer create, read and delete operations for
instances of mapped entity classes. Instances may exist in one of three states:
transient: never persistent, not associated with any Session
persistent: associated with a unique Session
detached: previously persistent, not associated with any Session
•
Transient instances may be made persistent by calling save(), persist() or
saveOrUpdate(). Persistent instances may be made transient by calling delete(). Any
instance returned by a get() or load() method is persistent. Detached instances may be
made persistent by calling update(), saveOrUpdate(), lock() or replicate(). The state of a
transient or detached instance may also be made persistent as a new persistent
instance by calling merge().
•
save() and persist() result in an SQL INSERT, delete() in an SQL DELETE and update() or
merge() in an SQL UPDATE. Changes to persistent instances are detected at flush time
and also result in an SQL UPDATE. saveOrUpdate() and replicate() result in either an
INSERT or an UPDATE.
•
It is not intended that implementors be threadsafe. Instead each thread/transaction
should obtain its own instance from a SessionFactory.
•
A Session instance is serializable if its persistent classes are serializable.
Transaction
• A typical transaction should use the following
idiom:
• Session sess = factory.openSession();
• Transaction tx;
• try { tx = sess.beginTransaction();
• //do some work ...
• tx.commit();
• } catch (Exception e) {
• if (tx!=null) tx.rollback(); throw e; }
• finally { sess.close(); }
SessionFactory,Session,Transaction Objects
• You have to startup Hibernate by building a global
org.hibernate.SessionFactory object and storing it somewhere for
easy access in application code
• The org.hibernate.SessionFactory is a thread-safe global object
that is instantiated once.
• A org.hibernate.SessionFactory is used to obtain
org.hibernate.Session instances
• A org.hibernate.Session represents a single-threaded unit of work.
• A Session is used to get a physical connection with a database.
• The Session object is lightweight and designed to be instantiated
each time an interaction is needed with the database
• The main function of the Session is to offer create, read and delete
operations for instances of mapped entity classes.
HibernateUtil helper class
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
package org.hibernate.tutorial.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
1.1.7. Loading and storing objects
We are now ready to start doing some real work with Hibernate. Let's start by writing an
EventManager class with a main() method:
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
package org.hibernate.tutorial;
import org.hibernate.Session;
import java.util.*;
import org.hibernate.tutorial.domain.Event;
import org.hibernate.tutorial.util.HibernateUtil;
public class EventManager {
public static void main(String[] args) {
EventManager mgr = new EventManager();
if (args[0].equals("store")) {
mgr.createAndStoreEvent("My Event", new Date());
}
HibernateUtil.getSessionFactory().close();
}
private void createAndStoreEvent(String title, Date theDate) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
session.getTransaction().commit();
}
}
Instance of mapped Entity classes may exist in
one of the following three states at a given
point in time:
• transient: A new instance of a a persistent class which is not
associated with a Session and has no representation in the
database and no identifier value is considered transient by
Hibernate.
• persistent: You can make a transient instance persistent by
associating it with a Session. A persistent instance has a
representation in the database, an identifier value and is
associated with a Session.
• detached: Once we close the Hibernate Session, the
persistent instance will become a detached instance.
A typical transaction should use the
following idiom:
• Session session = factory.openSession();
• Transaction tx = null;
• try {
•
tx = session.beginTransaction();
•
// do some work
•
...
•
tx.commit();
• }
• catch (Exception e) {
•
if (tx!=null) tx.rollback();
•
e.printStackTrace();
• }finally {
•
session.close();
• }
NOTE: If the Session throws an exception, the transaction must be rolled back and the
session must be discarded.
Hibernate Persistant Class
•
•
•
The entire concept of Hibernate is to take the values from Java class attributes and
persist them to a database table
A mapping document helps Hibernate in determining how to pull the values from
the classes and map them with table and associated fields
Java classes whose objects or instances will be stored in database tables are called
persistent classes in Hibernate
There are following main rules of persistent classes, however, none of these rules are
hard requirements.
• All Java classes that will be persisted need a default constructor.
• All classes should contain an ID in order to allow easy identification of your objects
within Hibernate and the database. This property maps to the primary key column
of a database table.
• All attributes that will be persisted should be declared private and have getXXX
and setXXX methods defined in the JavaBean style.
• A central feature of Hibernate, proxies, depends upon the persistent class being
either non-final, or the implementation of an interface that declares all public
methods.
• All classes that do not extend or implement some specialized classes and
interfaces required by the EJB framework.
Hibernate Mapping File
•
•
•
•
•
•
•
•
•
•
An Object/relational mappings are usually defined in an XML document.
This mapping file instructs Hibernate how to map the defined class or classes to the database
tables.
There would be one table corresponding to each object you are willing to provide
persistence.
Suppose you are mapping the class Employee having the instance variables: int id, String
firstName, String lastName, int salary with the table Employee having id(primary key),
firstName, lastName, salary
The mapping document is an XML document having <hibernate-mapping> as the root
element which contains all the <class> elements.
The <class> elements are used to define specific mappings from a Java classes to the
database tables. The Java class name is specified using the name attribute of the class
element and the database table name is specified using the table attribute.
The <meta> element is optional element and can be used to create the class description.
The <id> element maps the unique ID attribute in class to the primary key of the database
table. The name attribute of the id element refers to the property in the class and the
column attribute refers to the column in the database table. The type attribute holds the
hibernate mapping type, this mapping types will convert from Java to SQL data type.
The <generator> element within the id element is used to automatically generate the
primary key values. Set the class attribute of the generator element is set to native to let
hibernate pick up either identity, sequence or hilo algorithm to create primary key
depending upon the capabilities of the underlying database.
The <property> element is used to map a Java class property to a column in the database
table. The name attribute of the element refers to the property in the class and the column
attribute refers to the column in the database table. The type attribute holds the hibernate
mapping type, this mapping types will convert from Java to SQL data type.
The Hibernate Mapping File or <classname>.hbm.xml
• <?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernatemapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
• <hibernate-mapping>
• <class name="Employee" table="EMPLOYEE">
• <meta attribute="class-description"> This class contains the
employee detail.</meta>
• <id name="id" type="int" column="id"><generator class="native"/>
</id>
• <property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
<property name="salary" column="salary" type="int"/>
</class></hibernate-mapping>
NOTE:You should save the mapping document in a file with the format
<classname>.hbm.xml.
The hibernate.cfg.xml or hibernate configuration file
•
•
•
•
<?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">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/testdb</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</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/tutorials/hibernatetutorial/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
•
•
•
•
•
•
•
Hibernate Mapping Types
• When you prepare a Hibernate mapping
document, we have seen that you map Java data
types into RDBMS data types.
• The types declared and used in the mapping files
are not Java data types; they are not SQL
database types either. These types are called
Hibernate mapping types, which can translate
from Java to SQL data types and vice versa.
• This chapter lists down all the basic, date and
time, large object, and various other builtin
mapping types.
Hibernate Mapping Types
Primitive types:
Primitive types:
Mapping type
integer
long
short
float
double
big_decimal
character
string
byte
boolean
yes/no
true/false
Java type
int or java.lang.Integer
long or java.lang.Long
short or java.lang.Short
float or java.lang.Float
double or java.lang.Double
java.math.BigDecimal
java.lang.String
java.lang.String
byte or java.lang.Byte
boolean or java.lang.Boolean
boolean or java.lang.Boolean
boolean or java.lang.Boolean
ANSI SQL Type
INTEGER
BIGINT
SMALLINT
FLOAT
DOUBLE
NUMERIC
CHAR(1)
VARCHAR
TINYINT
BIT
CHAR(1) ('Y' or 'N')
CHAR(1) ('T' or 'F')
Hibernate Mapping Types
Date and time types:
Mapping type
date
time
timestamp
calendar
calendar_date
Java type
ANSI SQL Type
java.util.Date or java.sql.Date
DATE
java.util.Date or java.sql.Time
TIME
java.util.Date or java.sql.Timestamp TIMESTAMP
java.util.Calendar
TIMESTAMP
java.util.Calendar
DATE
Hibernate Mapping Types
Binary and large object types:
Mapping type
Java type
binary
byte[]
text
java.lang.String
any Java class that implements
java.io.Serializable
java.sql.Clob
java.sql.Blob
serializable
clob
blob
ANSI SQL Type
VARBINARY (or
BLOB)
CLOB
VARBINARY (or
BLOB)
CLOB
BLOB
Hibernate Mapping Types
JDK-related types:
Mapping type
class
locale
timezone
currency
Java type
java.lang.Class
java.util.Locale
java.util.TimeZone
java.util.Currency
ANSI SQL Type
VARCHAR
VARCHAR
VARCHAR
VARCHAR
Learnings on 01/02/2014
• While constructng the hibernate mapping file
ensure that you use the full qualified class
name(i.e. package.class) instead of just the
class name
• While writing the hibernate configuration file
make sure that you commentou the
“cache.provider_class” property as it throws
exceptions
Create Application Class To Add An Employee(insert)
•
•
•
package org.hibernate.tutorials.hibernatetutorial;
import java.util.List; import java.util.Date;import java.util.Iterator; import org.hibernate.HibernateException; import org.hibernate.Session;
import org.hibernate.Transaction;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
public class ManageEmployee {
private static SessionFactory factory;
public static void main(String[] args) {
try{
factory = new Configuration().configure().buildSessionFactory(); }catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex); }
ManageEmployee ME = new ManageEmployee();
/* Add few employee records in database */
Integer empID1 = ME.addEmployee("Zara", "Ali", 1000);
Integer empID2 = ME.addEmployee("Daisy", "Das", 5000);
Integer empID3 = ME.addEmployee("John", "Paul", 10000);
/* Method to CREATE an employee in the database */
public Integer addEmployee(String fname, String lname, int salary){
Session session = factory.openSession(); T
ransaction tx = null;
Integer employeeID = null;
try{
tx = session.beginTransaction(); Employee employee = new Employee(fname, lname, salary);
employeeID = (Integer) session.save(employee);
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally { session.close(); }
return employeeID; }
Create Application Class To Update An Employee(update)
•
•
•
package org.hibernate.tutorials.hibernatetutorial;
import java.util.List; import java.util.Date;import java.util.Iterator; import org.hibernate.HibernateException;
import org.hibernate.Session; import org.hibernate.Transaction;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;
public class ManageEmployee {
•
private static SessionFactory factory;
•
public static void main(String[] args) {
•
try{
•
factory = new Configuration().configure().buildSessionFactory();
•
}catch (Throwable ex) {
•
System.err.println("Failed to create sessionFactory object." + ex);
•
throw new ExceptionInInitializerError(ex);
•
}
•
ManageEmployee ME = new ManageEmployee();
•
/* Update employee's records */
•
ME.updateEmployee(empID1, 5000);
•
/* Method to UPDATE salary for an employee */
•
public void updateEmployee(Integer EmployeeID, int salary ){
•
Session session = factory.openSession();
•
Transaction tx = null;
•
try{
•
tx = session.beginTransaction();
•
Employee employee =
•
(Employee)session.get(Employee.class, EmployeeID);
•
employee.setSalary( salary );
•
session.update(employee);
•
tx.commit();
•
}catch (HibernateException e) {
•
if (tx!=null) tx.rollback();
•
e.printStackTrace();
•
}finally {
•
session.close(); }
}
Create application to read all employee
•
•
package org.hibernate.tutorials.hibernatetutorial;
import java.util.List; import java.util.Date;import java.util.Iterator; import org.hibernate.HibernateException; import org.hibernate.Session;
import org.hibernate.Transaction;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;
public class ManageEmployee {
private static SessionFactory factory;
public static void main(String[] args) {
try{ factory = new Configuration().configure().buildSessionFactory();
}catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex); }
ManageEmployee ME = new ManageEmployee();
/* List down all the employees */
ME.listEmployees();
/* Method to READ all the employees */
public void listEmployees( ){
Session session = factory.openSession();
Transaction tx = null;
try{ tx = session.beginTransaction();
List employees = session.createQuery("FROM Employee").list();
for (Iterator iterator = employees.iterator(); iterator.hasNext();){
Employee employee = (Employee) iterator.next();
System.out.print("First Name: " + employee.getFirstName());
System.out.print(" Last Name: " + employee.getLastName());
System.out.println(" Salary: " + employee.getSalary());
} tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
} }
Delete an employee from the database
•
•
•
package org.hibernate.tutorials.hibernatetutorial;
import java.util.List; import java.util.Date;import java.util.Iterator; import org.hibernate.HibernateException; import org.hibernate.Session;
import org.hibernate.Transaction;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
public class ManageEmployee {
private static SessionFactory factory;
public static void main(String[] args) {
try{
factory = new Configuration().configure().buildSessionFactory();
}catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
ManageEmployee ME = new ManageEmployee();
/* Delete an employee from the database */
ME.deleteEmployee(empID2);}
/* Method to DELETE an employee from the records */
public void deleteEmployee(Integer EmployeeID){
Session session = factory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
Employee employee =
(Employee)session.get(Employee.class, EmployeeID);
session.delete(employee);
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close(); } } }
Generator classes in Hibernate
•
•
•
•
•
•
•
•
•
•
•
•
•
•
The <generator> subelement of id used to generate the unique identifier for the objects of
persistent class. There are many generator classes defined in the Hibernate Framework.
All the generator classes implements the org.hibernate.id.IdentifierGenerator interface. The
application programmer may create one's own generator classes by implementing the
IdentifierGenerator interface. Hibernate framework provides many built-in generator classes:
assigned
increment
sequence
hilo
native
identity
seqhilo
uuid
guid
select
foreign
sequence-identity
Generator classes in Hibernate
•
•
1) assigned
It is the default generator strategy if there is no <generator> element . In this case, application
assigns the id. For example:
•
•
•
•
•
•
•
<hibernate-mapping>
<class ...>
<id ...>
<generator class="assigned"></generator>
</id>
</class>
</hibernate-mapping>
•
•
2) increment
It generates the unique id only if no other process is inserting data into this table. It generates
short, int or long type identifier. The first generated identifier is 1 normally and incremented as 1.
Syntax:
•
•
•
•
•
•
•
<hibernate-mapping>
<class ...>
<id ...>
<generator class="increment"></generator>
</id>
</class>
</hibernate-mapping>
Generator classes in Hibernate
•
•
•
•
•
3) sequence
It uses the sequence of the database. if there is no sequence defined, it creates a sequence automatically
e.g. in case of Oracle database, it creates a sequence named HIBERNATE_SEQUENCE. In case of Oracle,
DB2, SAP DB, Postgre SQL or McKoi, it uses sequence but it uses generator in interbase. Syntax:
<id ...>
<generator class="sequence"></generator>
</id>
•
•
•
•
•
•
For defining your own sequence, use the param subelement of generator.
<id ...>
<generator class="sequence">
<param name="sequence">your_sequence_name</param>
</generator>
</id>
•
•
•
•
•
4) hilo
It uses high and low algorithm to generate the id of type short, int and long. Syntax:
<id ...>
<generator class="hilo"></generator>
</id>
•
•
•
•
•
5) native
It uses identity, sequence or hilo depending on the database vendor. Syntax:
<id ...>
<generator class="native"></generator>
</id>
Generator classes in Hibernate
• 6) identity
It is used in Sybase, My SQL, MS SQL Server, DB2 and HypersonicSQL to support the id
column. The returned id is of type short, int or long.
• 7) seqhilo
It uses high and low algorithm on the specified sequence name. The returned id is of
type short, int or long.
• 8) uuid
It uses 128-bit UUID algorithm to generate the id. The returned id is of type String,
unique within a network (because IP is used). The UUID is represented in
hexadecimal digits, 32 in length.
• 9) guid
It uses GUID generated by database of type string. It works on MS SQL Server and
MySQL.
• 10) select
It uses the primary key returned by the database trigger.
• 11) foreign
It uses the id of another associated object, mostly used with <one-to-one>
association.
• 12) sequence-identity
It uses a special sequence generation strategy. It is supported in Oracle 10g drivers
only. download the example of assigned generator
SQL Dialects in Hibernate
• For connecting any hibernate application with
the database, you must specify the SQL
dialects. There are many Dialects classes
defined for RDBMS in the
org.hibernate.dialect package. They are as
follows
Hibernate Table Per Hierarchy using xml file
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
By this inheritance strategy, we can map the whole hierarchy by single table only. Here, an extra column (also
known as discriminator column) is created in the table to identify the class.
RDBMS
Oracle (any version)
Oracle9i
Oracle10g
MySQL
MySQL with InnoDB
MySQL with MyISAM
DB2
DB2 AS/400
DB2 OS390
Microsoft SQL Server
Sybase
Sybase Anywhere
PostgreSQL
SAP DB
Informix
HypersonicSQL
Ingres
Progress
Mckoi SQL
Interbase
Pointbase
FrontBase
Firebird
Dialect
org.hibernate.dialect.OracleDialect
org.hibernate.dialect.Oracle9iDialect
org.hibernate.dialect.Oracle10gDialect
org.hibernate.dialect.MySQLDialect
org.hibernate.dialect.MySQLInnoDBDialect
org.hibernate.dialect.MySQLMyISAMDialect
org.hibernate.dialect.DB2Dialect
org.hibernate.dialect.DB2400Dialect
org.hibernate.dialect.DB2390Dialect
org.hibernate.dialect.SQLServerDialect
org.hibernate.dialect.SybaseDialect
org.hibernate.dialect.SybaseAnywhereDialect
org.hibernate.dialect.PostgreSQLDialect
org.hibernate.dialect.SAPDBDialect
org.hibernate.dialect.InformixDialect
org.hibernate.dialect.HSQLDialect
org.hibernate.dialect.IngresDialect
org.hibernate.dialect.ProgressDialect
org.hibernate.dialect.MckoiDialect
org.hibernate.dialect.InterbaseDialect
org.hibernate.dialect.PointbaseDialect
org.hibernate.dialect.FrontbaseDialect
org.hibernate.dialect.FirebirdDialect
Hibernate O/R Mapping
Code Snipped for Hibernate SQL insert
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
public class StoreData {
public static void main(String[] args) {
//creating configuration object
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
//populates the data of the configuration file
//creating seession factory object
SessionFactory factory=cfg.buildSessionFactory();
//creating session object
Session session=factory.openSession();
//creating transaction object
Transaction t=session.beginTransaction();
Employee e1=new Employee();
e1.setId(115);
e1.setFirstName("sonoo");
e1.setLastName("jaiswal");
session.persist(e1);
//persisting the object
t.commit();//transaction is commited
session.close();
System.out.println("successfully saved");
} }
MISC
• Also note that everything in
src/main/resources will be "on the classpath",
i.e., it all is copied to my-webapp/WEBINF/classes when the war is built. So that's a
good place to put your configuration files, for
example, log4j.xml / logback.xml,
How to configure your hibernate maven project to run
on server – to be done when building a web project
• Simply go into the project properties, make the projet
faceted.. tick Dynamic Web Module and you'll get run on
server as an option after that.
• This will enable the "Run on Server" option, however your
problems don't finish there as when you then run it, you'll
get a 404.
• To fix this, go back into project properties, Deployment
assembly.
• Delete the WebContent entry (and you can delete the
folder in the project later too), and make sure you have the
src-main-webapp & src-main-resource folders added.
• Bingo.. run the app and it should be fine.
Download