Studyon Minte9.com
Hibernate




Example (config)



1) src/testexample/Employee.java (POJO)

	%java
		package testexample;

		public class Employee {
		    
		    private int id;
		    private String firstName;
		    private String lastName;
		    private int salary;
		    
		    public Employee() {}
		    public Employee(String fname, String lname, int salary) {
			this.firstName = fname;
			this.lastName = lname;
			this.salary = salary;
		    }
		    public int getId() {
			return id;
		    }
		    public void setId(int id) {
			this.id = id;
		    }
		    public String getFirstName() {
			return firstName;
		    }
		    public void setFirstName(String fname) {
			this.firstName = fname;
		    }
		    public String getLastName() {
			return lastName;
		    }
		    public void setLastName(String lname) {
			this.lastName = lname;
		    }
		    public int getSalary() {
			return salary;
		    }
		    public void setLastName(int salary) {
			this.salary = salary;
		    }
		}


2) src/Employee.hbm.xml (Mapping file)

	<?xml version="1.0" encoding="utf-8"?>
	<!DOCTYPE hibernate-mapping PUBLIC
		"-//Hibernate/Hibernate Mapping DTD//EN"
		"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

	<hibernate-mapping>
	  <class name="testexample.Employee" table="employee">
	    <meta attribute="class-description">
	      This class contains the employee detail
	    </meta>
	    <id name="id" type="int" column="id">
	      <generator class="native"/>
	    </id>
	    <property name="firstName" column="first_name" type="string"/>
	    <property name="lastName" column="last_name" type="string"/>
	    <property name="salary" column="salary" type="int"/>
	  </class>
	</hibernate-mapping>


3) src/hibernate.cfg.xml (Configuration file)

	<?xml version="1.0" encoding="utf-8"?>
	<!DOCTYPE hibernate-configuration PUBLIC 
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
		"http://www.hibernate.org/dtd/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://myserver/test</property>
	     <property name="hibernate.connection.username">root</property>
	     <property name="hibernate.connection.password"></property>

		 <property name="hibernate.show_sql">false</property>
		 <property name="hibernate.generate_statistics">false</property>
		 <property name="hibernate.use_sql_comments">false</property>

	     <mapping resource="Employee.hbm.xml"/>

	   </session-factory>
	</hibernate-configuration>


http://www.tutorialspoint.com/hibernate/hibernate_configuration.htm
http://www.tutorialspoint.com/hibernate/hibernate_persistent_classes.htm