Let's create a Hibernate application without the use of IDE.
Download the following application and API to compile and run the application.
1) MySQL server 8.0
2) MySQL 8 Connector
3) Hibernate jars
We need to create the following classes and configuration files to compile and run the application:
1) Create persistence class
A persistence class is having no-argument constructor, and identifier field, getter and setter methods as follows:
public class Employee {
private int id;
private String firstName,lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
2) Create the mapping file for Persistent class
The mapping file name conventionally should be class_name.hbm.xml. The following are the elements of the mapping file.
hibernate-mapping: It is the root element in the mapping file that contains all the mapping elements.
class: It is the sub-element of the hibernate-mapping element. It specifies the Persistent class.
id: It is the subelement of class. It specifies the primary key attribute in the class.
generator: It is the sub-element of id. It is used to generate the primary key. There are many generator classes such as assigned, increment, hilo, sequence, native, etc. We will learn all the generator classes later.
property: It is the sub-element of class that specifies the property name of the Persistent class.
Let's see the mapping file (employee.hbm.xml) for the Employee class:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 5.3//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">
<hibernate-mapping>
<class name="Employee" table="emp1000">
<id name="id">
<generator class="assigned"></generator>
</id>
<property name="firstName"></property>
<property name="lastName"></property>
</class>
</hibernate-mapping>
3) Create the Configuration file
The configuration file contains information about the database and mapping. Conventionally, its name should be hibernate.cfg.xml.
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 5.3//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="connection.url">jdbc:mysql://@localhost:3306/demo</property>
<property name="connection.username">root</property>
<property name="connection.password">pass</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4) Create the class that retrieves or stores the object
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class StoreData {
public static void main(String[] args) {
//Create typesafe ServiceRegistry object
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();
SessionFactory factory = meta.getSessionFactoryBuilder().build();
Session session = factory.openSession();
Transaction t = session.beginTransaction();
Employee e1=new Employee();
e1.setId(101);
e1.setFirstName("Gaurav");
e1.setLastName("Chawla");
session.save(e1);
t.commit();
System.out.println("successfully saved");
factory.close();
session.close();
}
}
Put all of the above in the same folder. Don't forget to set classpath pointing to all the jars of hibernate as well as MySQL connector otherwise you will not able to compile the program.
Download the following application and API to compile and run the application.
1) MySQL server 8.0
2) MySQL 8 Connector
3) Hibernate jars
We need to create the following classes and configuration files to compile and run the application:
- create persistence class
- create a mapping file
- create a configuration file
- Create the class that retrieves or stores the persistent object
1) Create persistence class
A persistence class is having no-argument constructor, and identifier field, getter and setter methods as follows:
public class Employee {
private int id;
private String firstName,lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
2) Create the mapping file for Persistent class
The mapping file name conventionally should be class_name.hbm.xml. The following are the elements of the mapping file.
hibernate-mapping: It is the root element in the mapping file that contains all the mapping elements.
class: It is the sub-element of the hibernate-mapping element. It specifies the Persistent class.
id: It is the subelement of class. It specifies the primary key attribute in the class.
generator: It is the sub-element of id. It is used to generate the primary key. There are many generator classes such as assigned, increment, hilo, sequence, native, etc. We will learn all the generator classes later.
property: It is the sub-element of class that specifies the property name of the Persistent class.
Let's see the mapping file (employee.hbm.xml) for the Employee class:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 5.3//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">
<hibernate-mapping>
<class name="Employee" table="emp1000">
<id name="id">
<generator class="assigned"></generator>
</id>
<property name="firstName"></property>
<property name="lastName"></property>
</class>
</hibernate-mapping>
3) Create the Configuration file
The configuration file contains information about the database and mapping. Conventionally, its name should be hibernate.cfg.xml.
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 5.3//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="connection.url">jdbc:mysql://@localhost:3306/demo</property>
<property name="connection.username">root</property>
<property name="connection.password">pass</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4) Create the class that retrieves or stores the object
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class StoreData {
public static void main(String[] args) {
//Create typesafe ServiceRegistry object
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();
SessionFactory factory = meta.getSessionFactoryBuilder().build();
Session session = factory.openSession();
Transaction t = session.beginTransaction();
Employee e1=new Employee();
e1.setId(101);
e1.setFirstName("Gaurav");
e1.setLastName("Chawla");
session.save(e1);
t.commit();
System.out.println("successfully saved");
factory.close();
session.close();
}
}
Put all of the above in the same folder. Don't forget to set classpath pointing to all the jars of hibernate as well as MySQL connector otherwise you will not able to compile the program.
No comments:
Post a Comment