Followers

hb : Composite Key Implementation Using Annotations

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 class="mypack.CourseOffering" />
  </session-factory>
</hibernate-configuration>

COKey.java
==========

package mypack;

import java.io.Serializable;
import javax.persistence.Embeddable;
@Embeddable
public class COKey implements Serializable{
    private String courseName;
    private String track;

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public String getTrack() {
        return track;
    }

    public void setTrack(String track) {
        this.track = track;
    }
   
}

CourseOffering.java
===================

package mypack;

import javax.persistence.*;

@Entity
public class CourseOffering {
    @EmbeddedId
    private COKey key;
    private int noOfDays;

    public COKey getKey() {
        return key;
    }

    public void setKey(COKey key) {
        this.key = key;
    }

    public int getNoOfDays() {
        return noOfDays;
    }

    public void setNoOfDays(int noOfDays) {
        this.noOfDays = noOfDays;
    }
}

SaveData.java
=============
package mypack;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class SaveData {
    public static void main(String[] args){
    SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = factory.openSession();
    Transaction tx = session.beginTransaction();
    COKey key = new COKey();
    CourseOffering co = new CourseOffering();
    key.setCourseName("spring");
    key.setTrack("java");
    co.setKey(key);
    co.setNoOfDays(30);
    session.persist(co);
    tx.commit();
    System.out.println("Data saved successfully...");
    session.close();
    
    }
    
}

UpdataOrDelete.java
===================

package mypack;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class UpdataOrDelete {
    public static void main(String[] args){
    SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = factory.openSession();
    Transaction tx = session.beginTransaction();
    COKey key = new COKey();    
    key.setCourseName("spring");
    key.setTrack("java");
    CourseOffering co = (CourseOffering)session.load(CourseOffering.class, key);
    co.setNoOfDays(40);
    tx.commit();
    System.out.println("Data saved 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 AnnotationConfiguration().configure().buildSessionFactory();
    Session session = factory.openSession();
    Transaction tx = session.beginTransaction();
    COKey key = new COKey();  
    key.setCourseName("spring");
    key.setTrack("java");
    CourseOffering co = (CourseOffering)session.load(CourseOffering.class, key);
    System.out.println(co.getKey().getCourseName()+" "+co.getKey().getTrack()+" "+co.getNoOfDays());
    tx.commit();
    System.out.println("Data saved successfully...");
    session.close();
    
    }
    
}

No comments:

Post a Comment