Knowee
Questions
Features
Study Tools

Write the simple program in Hibernate

Question

Write the simple program in Hibernate

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple program in Hibernate. This program will save a new Employee object to the database.

First, we need to create a Hibernate configuration file (hibernate.cfg.xml):

<!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>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</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="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <mapping class="com.example.Employee"/>
    </session-factory>
</hibernate-configuration>

Next, we need to create a POJO class (Employee.java):

package com.example;

public class Employee {
    private int id;
    private String firstName;
    private String lastName;

    // getters and setters
}

Then, we need to create a Hibernate mapping file (Employee.hbm.xml):

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.example.Employee" table="EMPLOYEE">
        <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"/>
    </class>
</hibernate-mapping>

Finally, we can write a main program to save a new Employee object to the database (Main.java):

package com.example;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Main {
    public static void main(String[] args) {
        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml");

        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.openSession();

        Employee emp = new Employee();
        emp.setFirstName("John");
        emp.setLastName("Doe");

        session.beginTransaction();
        session.save(emp);
        session.getTransaction().commit();

        session.close();
        factory.close();
    }
}

Please replace the database connection details in the configuration file with your actual database connection details. Also, make sure that the Hibernate and JDBC libraries are included in your project classpath.

This problem has been solved

Similar Questions

What is Hibernate in Java?An ORM frameworkA databaseA programming languageA version control system

Write the database operations as Insert, delete, update, search operation in Hibernate

how to write java application

how to write java appliaction

Which method in Hibernate is used to save an entity to the database?save()commit()persist()insert()

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.