Composed ID : Primary Key « Hibernate « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Hibernate » Primary Key 
21.21.4.Composed ID

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 == nullreturn 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 = (Sessionsession.get();
        if (s == null) {
            s = sessionFactory.openSession();
            session.set(s);
        }
        return s;
    }

    public static void closeSession() throws HibernateException {
        Session s = (Sessionsession.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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.