This is simple hibernate program to create table and insert records.
Software used
- Eclipse
- Mysql
Create simple java project in eclipse "com.vishal.hibernate"
Jar's Used:
1. hibernate jars http://hibernate.org/orm/downloads/
- download latest hibernate and go-to require folder and used all jars
2. mysql-connector-java
- download latest hibernate and go-to require folder and used all jars
2. mysql-connector-java
hibernate.cfg.xml
<?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">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</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 the existing tables and create new one -->
<property name="hbm2ddl.auto">create</property>
<!-- create -> Everytime it will create new table
create-drop -> deleting existing and creating new table same as above
update -> To update
validate -> To validate existing DB table and new, Will update
or change anything in DB table -->
<!-- Mention here all the model classes along with their package name -->
<mapping class="com.vishal.hibernate.Student_Info"/>
</session-factory>
</hibernate-configuration>
Student_Info.java
package com.vishal.hibernate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/*This is the Model Class with Annotations
- which you would be using for storing or retrieving or performing any other database information
*/
@Entity //Tells that class is an entity which hold the data
@Table(name="Student_Information1") //to store this table in database with different name
public class Student_Info
{
@Id //To specify the primary key @GeneratedValue-To automatically generate ID
@GeneratedValue
private int rollNo;
private String name;
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Main.java
package com.vishal.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class Main
{
public static void main(String[] args)
{
Student_Info student=new Student_Info();
student.setName("Raj");
//student.setRollNo(1);
SessionFactory sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(student);
session.getTransaction().commit();
session.close();
//sessionFactory.close();
}
}
Good vishal,
ReplyDeleteKeep it up .....
Thanks Kishor
ReplyDelete