Java

Thursday, 18 October 2012

hibernate component xml mapping


Ex 9:
//working with components


     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    HIBERNATE-ANNOTATIONS     SPRING-IOC       SPRING –AOP-SCHEMABASED   

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY

     SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT.... 






Customer.java



package madhav;

public class Customer {
private String name;
private int cid;
private Address permenentAddress;
private Address tempAddress;
public String getName() {
      return name;
}
public void setName(String name) {
      this.name = name;
}
public int getCid() {
      return cid;
}
public void setCid(int cid) {
      this.cid = cid;
}
public Address getPermenentAddress() {
      return permenentAddress;
}
public void setPermenentAddress(Address permenentAddress) {
      this.permenentAddress = permenentAddress;
}
public Address getTempAddress() {
      return tempAddress;
}
public void setTempAddress(Address tempAddress) {
      this.tempAddress = tempAddress;
}



}


Address.java



package madhav;

public class Address {
private int hno;
private String city;
private String state;
public int getHno() {
      return hno;
}
public void setHno(int hno) {
      this.hno = hno;
}
public String getCity() {
      return city;
}
public void setCity(String city) {
      this.city = city;
}
public String getState() {
      return state;
}
public void setState(String state) {
      this.state = state;
}


}


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="madhav.Customer" table="customer">
   <id name="cid" type="int" column="cid" >
   <generator class="increment"/>
  </id>

  <property name="name">
   <column name="name" />
  </property>
            <component name="permenentAddress" class="madhav.Address">
                  <property name="hno" type="int">
                        <column name="p_hno"/>
                  </property>
                  <property name="city" type="string">
                        <column name="p_city"/>
                  </property>
                  <property name="state" type="string">
                        <column name="p_state" />
                  </property>
            </component>
            <component name="tempAddress" class="madhav.Address">
                  <property name="hno" type="int">
                        <column name="t_hno"/>
                  </property>
                  <property name="city" type="string">
                        <column name="t_city"/>
                  </property>
                  <property name="state" type="string">
                        <column name="t_state" />
                  </property>
            </component>
             
 </class>
</hibernate-mapping>


Hibernate.cfg.xml




<?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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
        <property name="connection.username">system</property>
        <property name="connection.password">softech</property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <mapping resource="customer.hbm.xml"/>
    </session-factory>

</hibernate-configuration>
     
                                           
TestCase.java


import madhav.*;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import org.hibernate.cfg.*;
import org.hibernate.*;

import org.apache.log4j.*;
public class TestCase {

                                                 /**
                                                  * @param args
                                                  */
                                                 SessionFactory factory;
                                                
                                                 public void createCustomer(Customer d)
                                                 {
                                                  Session session=null;
                                                  Transaction tx=null;
                                                  try
                                                  {
                                                                 
                                                                 session =factory.openSession();
                                                                 
                                                                 

tx=session.beginTransaction();
                                                                 
                                                                 System.out.println("break1 in cd");
                                                                 session.save(d);
                                                                 
                                                                 tx.commit();
                                                                 System.out.println("record inserted");
                                                  }
                                                  catch(HibernateException e)
                                                  {
                                                               
                                                 e.printStackTrace();
                                                                 
                                                  }
                                                 }
                                                 public static void main(String[] args) {
                                                                // TODO Auto-generated method stub

                                                  System.out.println("first");
                                                                TestCase t=new TestCase();
                                                                Configuration cfg=new Configuration();
                                                                cfg.configure();
                                                                //cfg.addResource("employee.hbm.xml");
                                                                //cfg.addResource("personaldetails.hbm.xml");
                                                                System.out.println("break 2");
                                                               

t.factory=cfg.buildSessionFactory();
Address permenent=new Address();
permenent.setHno(23);
permenent.setCity("hyd");
permenent.setState("ap");

Address temp=new Address();
temp.setHno(3);
temp.setCity("prakasam");
temp.setState("ap");

Customer c=new Customer();
c.setCid(101);
c.setName("madhv");
c.setPermenentAddress(permenent);
c.setTempAddress(temp);


                                                                t.createCustomer(c);
                                                               

System.out.println("execution over");


                                                 }

}


Output -console:







No comments:

Post a Comment