Following are the required files to execute the hibernate application, Here hibernate mapping file is not needed.
Employeeannotation.java
[java]package com.abc;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name= "emp500")
public class Employeeannotation {
@Id
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;
}
} [/java]
The
@Table annotation permits the programmer to determine the points of interest of the table that will be utilized to continue the entity in the database.The @Table comment gives four characteristics, permitting the programmer to supersede the name of the table, its index, and its mapping, and uphold one of a kind limitations on segments in the table.
Set and
Get methods are a pattern of data encapsulation. Instead of accessing class member variables directly, one can define get methods to access these variables, and set methods to modify them. By encapsulating them in this manner, one can have control over the public interface.
hibernate.cfg.xml
[java]<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping class="com.abc.Employeeannotation"/>
</session-factory>
</hibernate-configuration>[/java]
Properties |
Description |
hibernate.connection.driver_class |
The JDBC driver class. |
hibernate.dialect |
This property makes Hibernate generate the suitable SQL for the picked database. |
hibernate.connection.url |
The JDBC URL to the database instance. |
hibernate.connection.username |
The database username. |
hibernate.connection.password |
The database password. |
hibernate.connection.pool_size |
Limits the number of connections waiting in the Hibernate database connection pool. |
hibernate.connection.autocommit |
Allows autocommit mode to be used for the JDBC connection. |
Here Write <mapping class="com.abc.Employeeannotation"/> to identify the file.
persistencestored.java
[java]package com.abc;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class persistencestored {
public static void main(String[] args) {
Session session=new AnnotationConfiguration()
.configure().buildSessionFactory().openSession();
Transaction t=session.beginTransaction();
Employeeannotation e1=new Employeeannotation();
e1.setId(1001);
e1.setFirstName("sonoo");
e1.setLastName("jaiswal");
Employeeannotation e2=new Employeeannotation();
e2.setId(1002);
e2.setFirstName("vimal");
e2.setLastName("jaiswal");
session.persist(e1);
session.persist(e2);
t.commit();
session.close();
System.out.println("successfully saved");
}
} [/java]
The
commit() treats a solitary SQL statement or a gathering of SQL explanations as one logical unit, and if the statement fails, the entire transaction comes up short. To empower manual-transaction support rather than the auto-commit mode that the JDBC driver utilizes uses by default.
Output:
Output will be as follows, The following message will be displayed and table will be created automatically in MySQL.
As inserted records in the coding output will be as follows.