Followers

hb : Composite Key Implementation Using XML 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">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
    <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
    <property name="hibernate.connection.url">jdbc:derby://localhost:1527/hari</property>
    <property name="hibernate.connection.username">app</property>
    <property name="hibernate.connection.password">app</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <mapping resource="product.hbm.xml" />
    
  </session-factory>
</hibernate-configuration>

Product.java
=============

package mypack;

import java.io.Serializable;
public class Product implements Serializable{
    private int productId;
    private String productName;
    private double price;

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

product.hbm.xml
===============

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="mypack.Product" table="PRODUCT">
        <composite-id >
            <key-property name="productId" column="PR_ID"/>
            <key-property name="productName" column="PR_NAME"/>
        </composite-id>
        <property name="price" column="PR_PRICE"/>
    </class>
</hibernate-mapping>


SaveData.java
=============
package mypack;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class SaveData {
    public static void main(String[] args){
    SessionFactory factory = new Configuration().configure().buildSessionFactory();
    Session session = factory.openSession();
    Transaction tx = session.beginTransaction();   
    Product p = new Product();  
    p.setProductId(111);
    p.setProductName("NOKIA");
    p.setPrice(2000);
    session.persist(p);
    tx.commit();
    System.out.println("Data saved successfully...");
    session.close();
    }
}


UpdateOrDelete.java
===================
package mypack;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class UpdateOrDelete {
    public static void main(String[] args){
    SessionFactory factory = new Configuration().configure().buildSessionFactory();
    Session session = factory.openSession();
    Transaction tx = session.beginTransaction();   
    Product p = new Product();  
    p.setProductId(111);
    p.setProductName("NOKIA");
    Product p1 = (Product)session.load(Product.class, p);
   //  System.out.println(p1.getProductId()+" "+p1.getProductName()+" "+p1.getPrice());
    //session.delete(p);
    p1.setPrice(111111.0);
   // p.setPrice(2000);
       session.persist(p1);
      tx.commit();
      System.out.println("Data deleted successfully...");
      session.close();
    }
}

FetchData.java
==============
package mypack;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class FetchData {
    public static void main(String[] args){
    SessionFactory factory = new Configuration().configure().buildSessionFactory();
    Session session = factory.openSession();
    Transaction tx = session.beginTransaction();   
    Product p = new Product();  
    p.setProductId(111);
    p.setProductName("NOKIA");
    Product p1 = (Product)session.load(Product.class, p);
    System.out.println(p1.getProductId()+" "+p1.getProductName()+" "+p1.getPrice());
    
   // p.setPrice(2000);
   // session.persist(p);
   // tx.commit();
    System.out.println("Data retrieval successfully...");
    session.close();
    }
}

No comments:

Post a Comment