Java hibernate xml mapping simple hello world getting started example


Getting started basic hibernate example to insert a record to Mysql database

This example contains

1.       Hibernate configuration file (hibernate.cfg.xml)

2.       Hibernate mapping file (Student.hbm.xml)

3.       POJO class (Student.java)

4.       Main Program (AddStudent.java)


Hibernate Configuration file

Hibernate.cfg.xml

 <!DOCTYPE hibernate-configuration PUBLIC 
	"-//Hibernate/Hibernate Configuration DTD//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory name="student1Factory">
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</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.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 resource="Student.hbm.xml" />
	</session-factory>
</hibernate-configuration>


Hibernate Mapping

Student.hbm.xml

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.candidjava.hibernate.sample.Student" table="users">
		<id name="id" column="sid">
			<generator class="increment" />
		</id>
		<property name="name" column="sname" not-null="true" />
		<property name="degree" column="degree" />
		<property name="phone" column="mobile" unique="true" />
		<property name="email" column="priEmail" />

	</class>

</hibernate-mapping>


POJO Class

Student.java

package com.candidjava.hibernate.sample;

import java.io.Serializable;

public class Student implements Serializable {
	private long id;
	private String name;
	private String degree;
	private String phone;
	private String email;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDegree() {
		return degree;
	}

	public void setDegree(String degree) {
		this.degree = degree;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
}

Main Class

AddStudent.java

package com.candidjava.hibernate.sample;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class AddStudent {
	public static void main(String args[]) throws Exception {

		String name = "Candidjava";
		String degree = "MS";
		String phone = "098776544";
		String email = "candid@candidjava.com";

		Student s1 = new Student();
		s1.setDegree(degree);
		s1.setEmail(email);
		s1.setName(name);
		s1.setPhone(phone);

		try {
			SessionFactory sessionFactory = new Configuration().configure(
					"/hibernate.cfg.xml").buildSessionFactory();

			Session s = sessionFactory.openSession();

			Transaction tx = s.beginTransaction();

			s.save(s1);

			tx.commit();
			s.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			System.err.println("Initial SessionFactory creation failed." + e);
		}
		System.out.println("Added to Database");
	}// end of method
}// end of class


Download Example

Sample xml based war

Sample xml based zip



Comments


©candidjava.com