- JDBC Database configuration
- The Model Object
- Service method to create the model object
- Database Design
- DAO method to use saving the model object to the database using SQL queries.
- JDBC Database Configuration -> Hibernate Configuration
- The Model Object -> Using Annotation
- Service method to create the model object -> Using the Hibernate API
- Database Design -> Not Needed
- DAO method to use saving the model object to the database using SQL queries -> Not needed
Step 1: File Name -> hibernate.cfg.xml in src folder of the application
<!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD 3.0//EN”
“http://hibernate.sourceforge.net/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/hibernateDB</property>
<property name=”connection.username”>username</property>
<property name=”connection.password”>password</property>
<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.NoCacheProvider</property>
<!– Show all executed SQL to stdout –>
<property name=”show_sql”>true</property>
<!– Drop and re-create the database schema on startup –>
<property name=”hbm2ddl.auto”>create</property>
<mapping class=”com.sdnext.hibernate.tutorial.dto.UserDetails”/>
</session-factory>
</hibernate-configuration>
Here below property configure driver of the specific database
<property name=”connection.driver_class”>com.mysql.jdbc.Driver</property>
<property name=”connection.url”>jdbc:mysql://localhost:3306/hibernateDB</property>
<property name=”connection.username”>username</property>
<property name=”connection.password”>password</property>
<property name=”dialect”>org.hibernate.dialect.MySQLDialect</property>
The below line configured the model class name UserDetails its object we want save on the database hibernateDB
<mapping class=”com.sdnext.hibernate.tutorial.dto.UserDetails”/>
Step 2: UserDetails.java
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class UserDetails
{
@Id
private int userId;
private String userName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
- Create a session factory
- create a session from the session factory
- Use the session to save model objects
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import com.sdnext.hibernate.tutorial.dto.UserDetails;
public class HibernateTestDemo {
public static void main(String[] args)
{
//Create the model object
UserDetails user = new UserDetails();
user.setUserId(1);
user.setUserName(“Dinesh Rajput”);
// Create Session Factory Object using Annotation Configuration
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
//Create Session object from session factory object
Session session = sessionFactory.openSession();
session.beginTransaction();
//Use the session to save model objects
session.save(user);
session.getTransaction().commit();
session.close();
}
}
Next Chapter we will write hibernate application using mapping file for the model class
<<Previous Chapter 3<< >>Next Chapter5>>
Can i need to create a table in DB already or not?
Hibernate Automatically creates for you… u dont need to
here while creating session factory we can not pass “hibernate.cnf.xml ” in configure() method….it’s possible or we don’t need it
table will automatically created with the model class name if not created already.
Hi Dinesh, the pink colour on the coding part seems dull-looking not readable. You may change the respective colour if you wish 🙂