1

I have order and item entity classes in my program. In order class there is a Set of Item. When I insert an order the order details must be inserted to Order table. And items in the Set must be inserted to Items table which has a foreign key to order_id in Order table. Therefore the query that i want is :

insert into ordertb (cName) values (?);
insert into item (id, order_id, qty) values(?,?,?);
insert into item (id, order_id, qty) values(?,?,?);
insert into item (id, order_id, qty) values(?,?,?);
// until all the items are inserted 

But hibernate generates the following query :

Hibernate: insert into ordertb (cName) values (?)
Hibernate: update item set qty=?, order_id=? where id=?
Hibernate: update item set qty=?, order_id=? where id=?
Hibernate: update item set qty=?, order_id=? where id=?
Hibernate: update item set order_id=? where id=?
Hibernate: update item set order_id=? where id=?
Hibernate: update item set order_id=? where id=?

Here is my Mapping file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="Order" table="ordertb">
    <meta attribute="class-description">
        This class contains the Order detail. 
    </meta>
    <id name="order" type="int" column="order_id">
        <generator class="native"/>
    </id>
    <property name="cName" column="cName" type="string"/>
    <set name="items" table="item" fetch="select" cascade="all">
        <key>
            <column name="order_id" not-null="true"></column>
        </key>
        <one-to-many class="Item"/>
    </set>
    </class>
</hibernate-mapping>

TestComp.java where i call the method

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Deepan
 */
public class TestComp {

    private static SessionFactory factory; 

    public static void main(String args[]){
        try{
         factory = new Configuration().configure().buildSessionFactory();
      }catch (Throwable ex) { 
         System.err.println("Failed to create sessionFactory object." + ex);
         System.out.println("Error");
         throw new ExceptionInInitializerError(ex); 

      }
      TestComp ME = new TestComp();

      Order od=new Order();
        Set<Item> items=new HashSet<Item>();

      Item i1=new Item(1, 5, od);
      items.add(i1);
      Item i2=new Item(2, 20, od);
      items.add(i2);
      Item i3=new Item(3, 15, od);
      items.add(i3);

      od.setOrder(200);
      od.setcName("Deepan");
      od.setItems(items);
      /* Add few employee records in database */


      Integer car1 = ME.addOrder(od);
        System.out.println(od.getOrder()+" : "+od.getcName()+" : "+od.getItems());
      //Integer car2 = ME.addOrder(6, "Benz", "gtr");
      //Integer car3 = ME.addOrder(7, "Audi", "sdf");
    }

     public Integer addOrder(Order order) {
        Session session = factory.openSession();
        Transaction tx = null;
        Integer carVIN = null;
        try {
            tx = session.beginTransaction();
            carVIN = (Integer) session.save(order);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return carVIN;
    }


}

What is wrong? If you need any additional information just mention in the comment. As I am quite new to hibernate please give me a detailed answer. Thanks in advance.

3
  • Add please code snippet where you're persisting your object Commented Aug 2, 2014 at 14:15
  • I have added the code to the question Commented Aug 2, 2014 at 14:39
  • Items contain Item objects that contain Order Objects that contain Items? something is wrong with your data graph. Commented Aug 2, 2014 at 18:26

1 Answer 1

1

get i1, i2, i3 as parameters into the method. Then add the following code after carVIN = (Integer) session.save(order);

session.save(i1);
session.save(i2);
session.save(i3);

hope it helps

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that worked and update queries are changed as insert queries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.