You can easily create hibernate application with Netbeans.
- First create database. I use command line with following command.
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Masudul Haque>mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.8 MySQL Community Server (GPL)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database library;
Query OK, 1 row affected (0.04 sec)
mysql> create user cuet@localhost;
Query OK, 0 rows affected (0.20 sec)
mysql> grant all on library.* to cuet@localhost identified by 'cuet';
Query OK, 0 rows affected (0.04 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.09 sec)
mysql>
- In my netbeans IDE I create a project named 'library_management'
- Create a hibernate configuration by following way.
- Then I show the my Mysql database connection by clicking next.
- After verify my connection is success I click finish. With this attempt hibernate.cfg.xml file will be created. Following is my hibernate.cfg.xml file.
<?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>
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/library?zeroDateTimeBehavior=convertToNull</property>
- <property name="hibernate.connection.username">cuet</property>
- <property name="hibernate.connection.password">cuet</property>
- <mapping class="edu.cuet.library.model.Student"/>
- </session-factory>
- </hibernate-configuration>
- 6. To perform any operation is hibernate you should have Hibernate SessionFactory class. Using netbeans you can easily generate SessionFactory class. From project you should click new to other and under hibernate section you should select HibernateUtil.java then click finish. Following is the HibernateUtil.java file
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.cuet.library.util;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
/**
* Hibernate Utility class with a convenient method to get Session Factory
* object.
*
* @author Masudul Haque
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Creating POJO class:
- Following is my domain class.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.cuet.library.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
*
* @author Masudul Haque
*/
@Entity
public class Student {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
@Column(name="student_id",length=7)
private String studentId;
@Column(name="first_name",length=50)
private String firstName;
@Column(name="last_name",length=50)
private String lastName;
@Temporal(TemporalType.DATE)
private Date birthDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
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;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
}
DB Export:
- It is really easy to export database export from POJO class. For that you need to map domain class to hibernate.cfg.xml on following way.
- Write a application file with named DBExport with following code.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.cuet.library.database;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
*
* @author Masudul Haque
*/
public class DBExport {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
AnnotationConfiguration conf= new AnnotationConfiguration();
conf.configure();
SchemaExport export=new SchemaExport(conf);
export.execute(true, true, false, true);
}
}
- After that run the application and database structure will be generated from POJO class.
No comments:
Post a Comment