双方向の1対のマルチ外部ジョイン(外部キー)


まず、2つのBeanクラス、Customerユーザークラス、Order受注クラスで、Order受注に外部キーが追加され、Customerのフィールドが参照されます.
Customerクラス
//  
public  class Customer {

    //  ID
    private Integer customerId;
    //   
    private String customerName;

    /** * 1:       ,       ,  Hibernate    *      ,    Hibernate       ,   Java SE      *      * 2:        ,        */
    private Set<Order> orders = new HashSet<Order>();




    public Set<Order> getOrders() {
        return orders;
    }

    public void setOrders(Set<Order> orders) {
        this.orders = orders;
    }

    public Customer() {
        super();
    }

    public Customer(Integer customerId, String customerName) {
        super();
        this.customerId = customerId;
        this.customerName = customerName;
    }

    public Integer getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Integer customerId) {
        this.customerId = customerId;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    @Override
    public String toString() {
        return "Customer [customerId=" + customerId + ", customerName="
                + customerName + "]";
    }
}

Orderクラス
//   
public class Order {
    //  ID
    private Integer orderId;
    //   
    private String orderName;
    //  ,  Customer
    private Customer customer;

    public Order() {
        super();
    }
    public Order(Integer orderId, String orderName, Customer customer) {
        super();
        this.orderId = orderId;
        this.orderName = orderName;
        this.customer = customer;
    }
    public Integer getOrderId() {
        return orderId;
    }
    public void setOrderId(Integer orderId) {
        this.orderId = orderId;
    }
    public String getOrderName() {
        return orderName;
    }
    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }
    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    @Override
    public String toString() {
        return "Order [orderId=" + orderId + ", orderName=" + orderName
                + ", customer=" + customer + "]";
    }


}

次にXMLプロファイル
Customer.hbm.xml
<?xml version="1.0"?> 
     <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="entity2">

    <class name="Customer" table="customer">

        <!-- id:     ,name:       -->
        <id name="customerId" type="java.lang.Integer">
            <!-- name:       -->
            <column name="customer_id"/>
            <!--          ,native:           -->
            <generator class="native"/>
        </id>

        <!-- property     -->
        <property name="customerName" type="java.lang.String">
            <column name="customer_name"/>
        </property>

        <!-- table: Order      key column: Order         inverse:true,Customer    Order          UPDATE   -->
        <set name="orders" table="orders" inverse="true">
            <key column="customer_id"></key>
            <one-to-many class="Order"/>
        </set>

    </class>
</hibernate-mapping>

Order.hbm.xml
<?xml version="1.0"?> 
     <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="entity2">
    <class name="Order" table="orders">

        <!-- id:     ,name:       -->
        <id name="orderId" type="java.lang.Integer">
            <!-- name:       -->
            <column name="order_id"/>
            <!--          ,native:           -->
            <generator class="native"/>
        </id>

        <!-- property     -->
        <property name="orderName" type="java.lang.String">
            <column name="order_name"/>
        </property>

        <!--            -->
        <many-to-one name="customer" class="Customer" column="customer_id"></many-to-one>

    </class>
</hibernate-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="connection.username">root</property>
        <property name="connection.password">1234</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>

        <!--   Hibernate      -->
        <!-- Hibernate          -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!--              SQL -->
        <property name="show_sql">true</property>

        <!--    SQL      -->
        <property name="format_sql">true</property>

        <!--              -->
        <property name="hbm2ddl.auto">update</property>

        <!--         /     ,  Bean xml   -->
        <!--  :   .  / -->
        <!-- <mapping resource="entity1/Customer.hbm.xml"/> <mapping resource="entity1/Order.hbm.xml"/> -->

        <mapping resource="entity2/Customer.hbm.xml"/>
        <mapping resource="entity2/Order.hbm.xml"/>

    </session-factory>
</hibernate-configuration> 

HibernateTestテストクラス
public class HibernateTest {
    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;

    @Before
    public void init() {
        // 1.    SessionFactory   
        sessionFactory = null;
            // 1.  Configuration  :  Hibernate                
            // configure();      ,      hibernate.cfg.xml
            Configuration configuration = new Configuration().configure();

            /** * new Configuration       * *     hibernate.properties     * 1:Configuration configuration = new Configuration(); * *       ,      hibernate.cfg.xml * 2:Configuration configuration = new Configuration().configure(); * *       ,    xml,    File * 3:Configuration configuration = new Configuration().configure(file); */

            // 2.    ServiceRegistry  
            // Hibernate                       
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
                .applySettings(configuration.getProperties())
                .buildServiceRegistry();
            // 3.    SessionFactory
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        // 2.    Session  
        session = sessionFactory.openSession();

        // 3.    
        transaction = session.beginTransaction();
    }

    @After
    public void distroy() {
        // 5.    
        transaction.commit();

        // 6.  Session  
        session.close();

        // 7.  SessionFactory  
        sessionFactory.close();
    }

    @Test 
    public void test1(){

        Customer customer = new Customer();
        customer.setCustomerName("1");

        Order order1 = new Order();
        Order order2 = new Order();

        order1.setOrderName("1");
        order2.setOrderName("2");

        //      
        order1.setCustomer(customer);
        order2.setCustomer(customer);

        // customer         Order  
        customer.getOrders().add(order1);
        customer.getOrders().add(order2);

        /** *    Customer,   Order * 3 INSERT,2 UPDATE *   Customer Order       ,   * Order   ,Customer   (UPDATE)Order    * *    Customer     Set     inverse="true" *     Order     ,   3 INSERT */
        session.save(customer);
        session.save(order1);
        session.save(order2);



        /** *    Order,   Customer * 3 INSERT,4 UPDATE *   Customer Order        * *    Customer     Set     inverse="true" *     Order     ,   3 INSERT,2 UPDATE */
        //Order    order1  
        session.save(order1);
        //Order    order2  
        session.save(order2);
        //Customer    order1,order2  
        session.save(customer);
    }

    // 
    @Test
    public void test2(){
        /** *  customer  orders List         *      orders List  ,    SQL  Order   */
        Customer customer = (Customer) session.get(Customer.class,1);

        /** *       Hibernate        *                     */
        Set<Order> orders = customer.getOrders();
    }

    // 
    @Test
    public void test3(){
        Customer customer = (Customer) session.get(Customer.class,1);
        /** *      LazyInitializationException   *   customer     orders   *  session.close() *      */
        session.close();
        System.out.println(customer.getOrders().size());
    }


    // 
    @Test
    public void test4(){
        //    customer  orders Set    order
        Customer customer = (Customer) session.get(Customer.class,1);
        //       order     ,  Set    :  
        customer.getOrders().iterator().next().setOrderName("GGG");
    }

    // 
    @Test
    public void test5(){
        /** *             ,        (Customer   ) *   Order(   )    ,      customer */
        Customer customer = (Customer) session.get(Customer.class,1);
        session.delete(customer);
    }
}