Programming Tutorials

Step by Step Hibernate - Your First Hibernate Application

By: Felix in Hibernate Tutorials on 2023-05-08  

Here are the steps to create a simple Hibernate application:

  1. Create a new Java project in your IDE of choice, and add the Hibernate libraries to your classpath. You can download the latest version of Hibernate from the official website.

  2. Create a new Hibernate configuration file (hibernate.cfg.xml) in the root of your project, and set up the database connection details. For example:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
            "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
      <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydatabase</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
      </session-factory>
    </hibernate-configuration>

    Replace the database connection details with your own.

  3. Create a new Java class to represent a table in your database. This class should have a no-argument constructor and getter and setter methods for each property. For example:
    @Entity
    @Table(name = "users")
    public class User {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
        @Column(name = "username")
        private String username;
    
        @Column(name = "email")
        private String email;
    
        // no-argument constructor
        public User() {}
    
        // getter and setter methods
        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }
        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
        public String getEmail() { return email; }
        public void setEmail(String email) { this.email = email; }
    }
    
  4. Create a new Hibernate session factory object, using the configuration file you created in step 2. For example:
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory sessionFactory = cfg.buildSessionFactory();
    
  5. Open a new Hibernate session using the session factory. For example:
    Session session = sessionFactory.openSession();
    
  6. Create a new transaction using the session. For example:
    Transaction tx = session.beginTransaction();
    
  7. Create a new instance of the User class you created in step 3, and set its properties. For example:
    User user = new User();
    user.setUsername("john_doe");
    user.setEmail("[email protected]");
    
  8. Save the new object to the database using the session. For example:
    session.save(user);
    
  9. Commit the transaction using the session. For example:
    tx.commit();
    
  10. Close the session using the session factory. For example:
    sessionFactory.close();
    

That's it! You've created a simple Hibernate application that saves a new user object to the database. You can use similar steps to retrieve, update, and delete objects from the database using Hibernate.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Hibernate )

Latest Articles (in Hibernate)