Map Array For One To Many Map : Map Array « Hibernate « Java






Map Array For One To Many Map

/////////////////////////////////////////////////////////////////////////

import java.util.Date;
import java.util.List;


import org.hibernate.*;
import org.hibernate.criterion.*;

public class SimpleRetrieveTest {
  
  
  public static void main(String[] args) {
    HibernateUtil.setup("create table A ( id int);");
    HibernateUtil.setup("create table B ( id int, aid int);");
    // hibernate code start
    Session session = HibernateUtil.currentSession();
    Transaction tx = session.beginTransaction();


    A a = new A();
    B b1 = new B();
    B b2 = new B();
    a.setBs( new B[] {b1, b2} );

    session.save(a);

        tx.commit();
    HibernateUtil.closeSession();

    HibernateUtil.sessionFactory.close();

    // hibernate code end
    HibernateUtil.checkData("select * from A");
    HibernateUtil.checkData("select * from B");    
  }
}


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

<!-- 

  This mapping demonstrates content-based discrimination for the
  table-per-hierarchy mapping strategy, using a formula
  discriminator.

-->

<hibernate-mapping >
  
  <class name="A" lazy="true">
    
    <id name="id">
      <generator class="increment"/>
    </id>

    <array name="bs" cascade="all" fetch="join"  >
      <key column="aid"/>
      <list-index column="id"/>
      <one-to-many class="B"/>
    </array>

  </class>

  <class name="B" lazy="true">
    <id name="id">
      <generator class="increment"/>
    </id>
  </class>


</hibernate-mapping>





/////////////////////////////////////////////////////////////////////////


/**
 * @author Emmanuel Bernard
 */
public class A {
  private Integer id;
  private B[] bs;

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public B[] getBs() {
    return bs;
  }

  public void setBs(B[] bs) {
    this.bs = bs;
  }
}



/////////////////////////////////////////////////////////////////////////


/**
 * @author Emmanuel Bernard
 */
public class B {
  private Integer id;

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }
}



/////////////////////////////////////////////////////////////////////////


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

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:data/tutorial</property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></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>

        <mapping resource="A.hbm.xml"/>
    </session-factory>

</hibernate-configuration>

/////////////////////////////////////////////////////////////////////////
           
       








HibernateMapArrayForOneToManyMap.zip( 4,578 k)

Related examples in the same category

1.Map Array Retrieve Demo
2.Collection Mapping: Array