• RubyonRails
  • JavaScript
  • Servlet
  • JSP
  • Struts
  • Spring
  • PHP
  • jQuery
  • SQL Tutorial

  • <

    Hibernate Component Mapping


    A Component mapping is a mapping for a class having a reference to another class as a member variable. We have seen such mapping while having two tables and using element in the mapping file. Now we will use element in the mapping file and a single table would be used to keep the attributes contained inside the class variable.

    Example


    sql file

    
    CREATE TABLE `customer` (
      `CUST_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `CUST_NAME` varchar(10) NOT NULL,
      `AGE` int(10) unsigned NOT NULL,
      `ADDRESS1` varchar(255) NOT NULL,
      `ADDRESS2` varchar(255) NOT NULL,
      `ADDRESS3` varchar(255) NOT NULL,
      `CREATED_DATE` date NOT NULL,
      `CREATED_BY` varchar(10) NOT NULL,
      PRIMARY KEY (`CUST_ID`) USING BTREE
    );
    

    Model Class


    Customer.java

    
    package com.tutorial.customer;
    
    import java.util.Date;
    
    public class Customer implements java.io.Serializable {
    
    	private Integer custId;
    	private String custName;
    	private int age;
    	private Address address;
    	private Date createdDate;
    	private String createdBy;
    
    	//setters and getters
    }
    


    Address.java

    
    package com.tutorial.customer;
    
    public class Address implements java.io.Serializable {
    
    	private String address1;
    	private String address2;
    	private String address3;
    
    	//setters and getters
    }
    
    

    Component Mapping


    Customer.hbm.xml

    
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping>
    	<class name="com.mkyong.customer.Customer" table="customer"
    		catalog="mkyongdb">
    
    		<id name="custId" type="java.lang.Integer">
    			<column name="CUST_ID" />
    			<generator class="identity" />
    		</id>
    		<property name="custName" type="string">
    			<column name="CUST_NAME" length="10" not-null="true" />
    		</property>
    		<property name="age" type="int">
    			<column name="AGE" not-null="true" />
    		</property>
    
    		<component name="Address" class="com.tutorial.customer.Address">
    			<property name="address1" type="string">
    				<column name="ADDRESS1" not-null="true" />
    			</property>
    			<property name="address2" type="string">
    				<column name="ADDRESS2" not-null="true" />
    			</property>
    			<property name="address3" type="string">
    				<column name="ADDRESS3" not-null="true" />
    			</property>
    		</component>
    
    		<property name="createdDate" type="date">
    			<column name="CREATED_DATE" length="10" not-null="true" />
    		</property>
    		<property name="createdBy" type="string">
    			<column name="CREATED_BY" length="10" not-null="true" />
    		</property>
    	</class>
    </hibernate-mapping>
    
    
    
    
    

    debug it

    
    package com.tutorial;
    
    import java.util.Date;
    import org.hibernate.Session;
    import com.mkyong.customer.Address;
    import com.mkyong.customer.Customer;
    import com.mkyong.util.HibernateUtil;
    
    public class App {
    	public static void main(String[] args) {
    
    	System.out.println("Hibernate component mapping");
    	Session session = HibernateUtil.getSessionFactory().openSession();
    
    	session.beginTransaction();
    
    	Address address = new Address();
    	address.setAddress1("Address 1");
    	address.setAddress2("Address 2");
    	address.setAddress3("Address 3");
    
            Customer cust = new Customer();
            cust.setCustName("prady");
            cust.setAge(30);
            cust.setAddress(address);
            cust.setCreatedDate(new Date());
            cust.setCreatedBy("system");
    
            session.save(cust);
    
    	session.getTransaction().commit();
    	System.out.println("Done");
    	}
    }
    



    Output:
    Hibernate component mapping
    Hibernate:
    insert
    into
    tutorialdb.customer
    (CUST_NAME, AGE, ADDRESS1, ADDRESS2, ADDRESS3, CREATED_DATE, CREATED_BY)
    values
    (?, ?, ?, ?, ?, ?, ?)
    Done













    © copyright 2017-2021 Completedone pvt ltd.