Component Mapping in Hibernate 4

Hôm nay mình xin giới thiệu với các bạn . Về việc thiết lập component mapping từ database to Object java in Hibernate như thế nào.

Source code : Downlaod


Student table của chúng ta.


Cấu trúc thư mục như sau :



package thaihoanghai.hibernate.bean;

import javax.persistence.Column;
import javax.persistence.Embeddable;
 * @Embeddable this annotation is used to specify the Address class will be used
 * as a component. The address class can't have a primary key of it's own.
 * It uses the enclosing class primary key

public class Address {
 @Column(name="address_street", nullable=false, length=100)
 private String street;
 @Column(name="address_city", nullable=false, length=100)
 private String city;
 @Column(name="address_state", nullable=false, length=100)
 private String state;
 @Column(name="address_zipcode", nullable=false, length=100)
 private String zipcode;

 public Address() {

 public Address(String street, String city, String state, String zipcode) {
 this.street = street; = city;
 this.state = state;
 this.zipcode = zipcode;
 public String getStreet() {
 return street;
 public void setStreet(String street) {
 this.street = street;
 public String getCity() {
 return city;
 public void setCity(String city) { = city;
 public String getState() {
 return state;
 public void setState(String state) {
 this.state = state;
 public String getZipcode() {
 return zipcode;
 public void setZipcode(String zipcode) {
 this.zipcode = zipcode;


package thaihoanghai.hibernate.bean;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "student")
public class Student {
 private int studentId;

 @Column(name="student_name", nullable=false, length=100)
 private String studentName;

 * @Embedded this annotation is used to specify the Address entity
 * stored in student table

 private Address studentAddress;

 public Student() {

 public Student(String studentName, Address studentAddress) {
 this.studentName = studentName;
 this.studentAddress = studentAddress;
 public int getStudentId() {
 return studentId;
 public void setStudentId(int studentId) {
 this.studentId = studentId;
 public String getStudentName() {
 return studentName;
 public void setStudentName(String studentName) {
 this.studentName = studentName;
 public Address getStudentAddress() {
 return studentAddress;
 public void setStudentAddress(Address studentAddress) {
 this.studentAddress = studentAddress;



package thaihoanghai.hibernate.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import thaihoanghai.hibernate.bean.Student;
import thaihoanghai.hibernate.util.HibernateUtil;

public class StudentDAO {

public boolean addStudent(Student student){
 boolean flag = true;
 Session session = HibernateUtil.getSeesionfactory().openSession();
 Transaction trns = null;
 try {
 trns = session.beginTransaction();;
 } catch (Exception e) {
 flag = false;
 // TODO: handle exception
 return flag;

Class StudentService.kava

package thaihoanghai.hibernate.service;

import thaihoanghai.hibernate.bean.Student;
import thaihoanghai.hibernate.dao.StudentDAO;

public class StudentService {
 StudentDAO service = new StudentDAO();

 public boolean addStudent(Student std){
 return service.addStudent(std);


package thaihoanghai.hibernate.util;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

 * Hibernate Util used to configure Hibernate's Session Factoru and retrieve it as Singleton
 * @author kobee
public class HibernateUtil {
 private static final SessionFactory seesionFactory;
 private static ServiceRegistry serviceRegistry;

 try {
 Configuration configuration = new Configuration();
 serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
 seesionFactory = configuration.buildSessionFactory(serviceRegistry);

 } catch (HibernateException e) {
 System.err.println("Initial SessionFactory creation failed. " + e);
 throw new ExceptionInInitializerError(e);


public static SessionFactory getSeesionfactory() {
 return seesionFactory;


Class Hibernate.config.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
 <property name="hibernate.connection.url">jdbc:mysql://localhost/dbTest</property>
 <property name="hibernate.connection.username">root</property>
 <property name="hibernate.connection.password">123456</property>
 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
 <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

 <!-- JDBC connecition pool (use the built-in) -->
 <property name="hibernate.connection.pool_size">1</property>
 <!-- Enable Hibernate's automatic session context management -->
 <property name="hibernate.current_session_context_class">thread</property>

 <!-- Echo all executed SQL to stdout -->
 <property name="show_sql">true</property>
 <property name="format_sql">false</property>
 <property name="">Validate</property>

 <!-- Mapping to Resource -->
 <mapping class="thaihoanghai.hibernate.bean.Student"/>
 <mapping class="thaihoanghai.hibernate.bean.Address"/>



package project;

import thaihoanghai.hibernate.bean.Address;
import thaihoanghai.hibernate.bean.Student;
import thaihoanghai.hibernate.service.StudentService;
public class Test {
 public static void main(String[] args) {

 // Add student into database
 StudentService service = new StudentService();
 Address address = new Address("Thống Nhất", "Hồ Chí Minh", "State - Not", "123456");
 Student student = new Student("Kobee", address);

Kết quả sau khi run :


Chúc các bạn thành công .


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: