Composed ID : Primary Key « Hibernate « Java Tutorial






File: AccAcc.java

import java.io.*;

public class AccAcc implements Serializable { 
  private String id; 
  private int accountnum; 
  private String acctype; 
  private String name;

  public AccAcc() {
  }

  public AccAcc(int i, String t, String n) {
    accountnum = i;
    acctype = t;
    name = n;
  }

  public void setId(String s) {
    id = s;
  }

  public String getId() {
    return id;
  }

  public void setAccountnum(int i) {
    accountnum = i;
  }

  public int getAccountnum() {
    return accountnum;
  }  

  public void setAcctype(String s) {
    acctype = s;
  }

  public String getAcctype() {
    return acctype;
  }

  public void setname(String s) {
    name = s;
  }

  public String getName() {
    return name;
  }

    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (!this.getClass().equals(obj.getClass())) return false;
        
        AccAcc obj2 = (AccAcc)obj;

        if (this.id.equals(obj2.getId()) &&
            this.accountnum == obj2.getAccountnum() &&
            this.acctype.equals(obj2.getAcctype()) &&
            this.name.equals(obj2.getName())) {
            return true;
        }
        
    return false;
    }

    public int hashCode() {      
        int tmp = 0;
        // Method 1-Concatenate the strings
        tmp = (id + accountnum + name + acctype).hashCode();

        return tmp;
    }
}

File: AccAccTest.java

import java.io.*;
import java.util.*;

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

public class AccAccTest {

  public static void main(String [] args) {

    try {
      Session session = HibernateUtil.currentSession(); 

      AccAcc acc = new AccAcc(1, "Personal", "John");

      session.save(acc);
      session.flush();

      AccAcc acc2 = new AccAcc();
      acc2.setId(1+"Personal"+"John");
      session.load(acc2, acc2.getId());

      session.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

class HibernateUtil {

    public static final SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static final ThreadLocal session = new ThreadLocal();

    public static Session currentSession() throws HibernateException {
        Session s = (Session) session.get();
        if (s == null) {
            s = sessionFactory.openSession();
            session.set(s);
        }
        return s;
    }

    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        if (s != null)
            s.close();
        session.set(null);
    }
}

File: AccAcc.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping 
  PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" 
  "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="AccAcc" 
   table="accacc">
  <composite-id name="id"
        class="string">
    <key-property name="accountnum"/> 
    <key-property name="acctype"/> 
    <key-property name="name"/>
  </composite-id>
</class>
</hibernate-mapping>

File: hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
  <session-factory>
     <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://127.0.0.1/test</property>
        <property name="connection.username">oost</property>
        <property name="connection.password">oost</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

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

        <!-- Drop and re-create the database schema on startup 
        <property name="hbm2ddl.auto">create</property>-->

        <mapping resource="User.hbm.xml"/>
  </session-factory>
</hibernate-configuration>
  Download:  HibernateComposedID.zip( 4,376 k)








21.21.Primary Key
21.21.1.Use Long AS ID
21.21.2.Use Int As ID which starts from 0
21.21.3.UUID HEX as Primary Key
21.21.4.Composed ID