- Published on
Setting up Hibernate in a Java Project
- Authors
- Name
- Gary Huynh
- @huynhthienthach
Ahoy, ye code-hoarding buccaneers! Today, we be charting the course to the land of Hibernate
. We’ll be getting ourselves set up with this here ORM framework
that even Blackbeard would have given his other eye for!
Now, gather 'round the mast and listen close as we conjure up Hibernate
in our Java
project, aye? The ol' sea shanty goes like this:
- Set sail with the right ship (or in our case, dependencies): Add the
Hibernate
andJDBC driver
dependencies to ye olde build file. If yer usingMaven
, 'tis how ye do it:
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.15.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
- Draw yer map (create a Hibernate config file):
Hibernate
needs to know how to talk to yerdatabase
. So, ye'll be needing ahibernate.cfg.xml
file. Keep it in yerclasspath
.
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/piratedb</property>
<property name="hibernate.connection.username">yourUser</property>
<property name="hibernate.connection.password">yourPass</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.yourpackage.Pirate"/>
</session-factory>
</hibernate-configuration>
- Initiate the voyage (Initialize the SessionFactory): Create a
HibernateUtil
class to set up theSessionFactory
and provide access to it:
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) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
And just like that, we're ready to sail, mateys! Now, ye can save or retrieve yer pirates, um, objects
from yer treasure chest (database
) like this:
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(new Pirate());
session.getTransaction().commit();
session.close();
Ain't that a smooth sail, eh? That's the power of Hibernate
for ye! But remember, the sea of code is as treacherous as it is bountiful. Keep a weather eye on the horizon for potential performance issues
and ye'll do just fine!
So, hoist the mainstay, secure the cannons, and may the winds of clean code always fill yer sails!