First create a hibernate.cfg.xml file. Let me explain about the values given in this file first.
- In this example I have used Oracle as data base. Hence I have provided the driver_class and url related to Oracle db.
- Provide the username and password to access the databse.
- A connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required. For this example let's give the value as 1.
- Dialect: This gives information about the language that hibernate needs to talk to the database i.e., Dialect allows Hibernate to generate SQL optimized for a particular relational database.
- The second level cache is responsible for caching objects across sessions. When this is turned on, objects will first be searched in the cache and if they are not found, a database query will be fired. For this particular example let's us not set the cache.
- Show_sql is set to true, which means that hibernate will print all the sql queries it generates.
- hbm2ddl.auto is set to create, which means hibernate will drop table if it is already existing and create a new table every time. If this field is set to update then hibernate will check for the table and table will be created if it does not exist.
- For every bean class that we create we need to give its details here in the mappings attribute.
<?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">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property> <property name="connection.username">hr</property> <property name="connection.password">hr</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.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">create</property> <!-- Names the annotated entity class --> <mapping class="com.ram.dao.UserDetails"/> </session-factory> </hibernate-configuration>
Create a UserDetails bean class. This bean class has been declared as an entity using the @Entity annotation.
A table with the name "userdetails" will be created in the database. @Id annotation is given on top of userId, this
means that, userId will be decalred as a primary key when "userdetails" table is created.
File: com.ram.dao.UserDetails.java
package com.ram.dao; 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 UserDetails object and assign some values to it. In order to store these values into the database we
need a session object. Session objects are created by a SessionFactory class. Once a session object has been created
we need to begin a transaction. We can save as many objects as we want in this transaction. In our case we just have
one model object. We have to then save the object using the save method.
File: com.ram.hibernate.UserDetails.java
package com.ram.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.ram.dao.UserDetails; public class HibernateUser { public static void main(String[] args) { UserDetails userDetails = new UserDetails(); userDetails.setUserId(1); userDetails.setUserName("Ram"); SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); session.save(userDetails); session.getTransaction().commit(); } }
Execute HibernateUser.java and you get the below output. In console we get the SQL query that hibernate
generates for us. "userdetails" is the table that has been created based on the bean class name.
No comments:
Post a Comment