This is one simple Hibernate example to insert java entity into MySql database using annotations
Software Used
Hibernate 3
Java 1.7
MySql 5
Eclipse Juno
1. Project Structure
Create a Java project and update Hibernate jars and Mysql driver jar in to build path. (Download Hibernate Jar , MySql Jar)
2. Create an Entity class with Hibernate annotations
package com.pretech;
import java.io.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@Table(name = "studentdetails", catalog = "hibernateschema")
public class Student implements Serializable {
private long id;
private String name;
private String standard;
@Column(name = "standard", unique = true, nullable = false, length = 20)
public String getStandard() {
return standard;
}
public void setStandard(String standard) {
this.standard = standard;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
public long getId() {
return id;
}
@Column(name = "name", unique = true, nullable = false, length = 10)
public String getName() {
return name;
}
public void setId(long string) {
id = string;
}
public void setName(String string) {
name = string;
}
public String toString() {
return name;
}
}
import java.io.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@Table(name = "studentdetails", catalog = "hibernateschema")
public class Student implements Serializable {
private long id;
private String name;
private String standard;
@Column(name = "standard", unique = true, nullable = false, length = 20)
public String getStandard() {
return standard;
}
public void setStandard(String standard) {
this.standard = standard;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
public long getId() {
return id;
}
@Column(name = "name", unique = true, nullable = false, length = 10)
public String getName() {
return name;
}
public void setId(long string) {
id = string;
}
public void setName(String string) {
name = string;
}
public String toString() {
return name;
}
}
3. Create Hibernate Configuration file (hibernate.cfg.xml)
Note: here we are adding the Mapping class instead of mapping xml file
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="studentFactory">
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernateschema
</property>
<property name="connection.username">
root
</property>
<property name="connection.password">
root
</property>
<property name="connection.pool_size">5</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.pretech.Student"></mapping>
</session-factory>
</hibernate-configuration>
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="studentFactory">
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernateschema
</property>
<property name="connection.username">
root
</property>
<property name="connection.password">
root
</property>
<property name="connection.pool_size">5</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.pretech.Student"></mapping>
</session-factory>
</hibernate-configuration>
4. Create a main program to save Student entity
package com.pretech;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class StudentMain {
private static SessionFactory sessionFactory;
public static void main(String args[]) throws Exception {
try {
sessionFactory = new Configuration().configure("hibernate.cfg.xml")
.buildSessionFactory();
} catch (Exception e) {
System.out.println(e.getMessage());
}
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Student student = new Student();
student.setName("Santhosh");
student.setStandard("9th Standard");
session.save(student);
tx.commit();
System.out.println("Updated to Student details table");
if (session != null)
session.close();
}
}
import org.hibernate.*;
import org.hibernate.cfg.*;
public class StudentMain {
private static SessionFactory sessionFactory;
public static void main(String args[]) throws Exception {
try {
sessionFactory = new Configuration().configure("hibernate.cfg.xml")
.buildSessionFactory();
} catch (Exception e) {
System.out.println(e.getMessage());
}
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Student student = new Student();
student.setName("Santhosh");
student.setStandard("9th Standard");
session.save(student);
tx.commit();
System.out.println("Updated to Student details table");
if (session != null)
session.close();
}
}
5.Output
No comments:
Post a Comment