Loading...

hibernate-devel@lists.sourceforge.net

[Prev] Thread [Next]  |  [Prev] Date [Next]

[Hibernate] (newb) Hibernate keeps DELETing and INSERTing unchanged component collection items Ondra Zizka Mon Apr 28 12:01:52 2008

Hi all,

I assume this is the right place to ask, please point me elsewhere if not.

I am learning Hibernate and came to such situation:

I have a 1:N relation (invoice (faktura) and it's items (polozky)) mapped 
like:

       <set name="polozky" table="faktury_polozky" lazy="false" 
access="field" fetch="join">
          <key column="id_faktura"/>
          <composite-element class="fakturace.Polozka">
            <property name="nazev" not-null="true" />
            <property name="mnozstvi" not-null="true"/>
            <property name="jednotka" not-null="true"/>
            <property name="cenaJednotky" not-null="true"/>
          </composite-element>
        </set>

But, 1) When I list() the invoices, upon commit() hibernate DELETEs the 
items' rows from the DB and INSERTs them again with same values. No other 
action is done; getter returns the same collection object as setter 
retrieves; and I set access to "field" anyway. So here's the code:

// LIST
        ...
        else if( "list".equals(args[0]) ){

            List res = session.createQuery("FROM Faktura ORDER BY vystaveno 
ASC").list();
            System.out.println("Nalezeno "+res.size()+" faktur.\n");
            for (int i = 0; i < res.size(); i++) {
                Faktura f = (Faktura) res.get(i);
                System.out.println( f.Vypis() + "\n\n" ); // Vypis() uses 
only getters.
            }
        }

        // COMMIT + CLOSE
        System.out.println("------------------------- COMMIT");
        session.getTransaction().commit();

And the log after commit:

------------------------- COMMIT
Hibernate: delete from faktury_polozky  where  id_faktura=? and nazev=? and 
mnozstvi=? and jednotka=? and cenaJednotky=?
Hibernate: delete from faktury_polozky where id_faktura=?   and nazev=?  and 
mnozstvi=?  and jednotka=? and cenaJednotky=?
Hibernate: insert     into        faktury_polozky        (id_faktura, nazev, 
mnozstvi, jednotka, cenaJednotky)     values        (?, ?, ?, ?, ?)
Hibernate: insert     into        faktury_polozky        (id_faktura, nazev, 
mnozstvi, jednotka, cenaJednotky)     values        (?, ?, ?, ?, ?)
... etc., for every invoice (faktury) there's a delete and insert.

So, what's that I do wrong?


And 2), Hibernate does a select for each invoice to get it's items. I wonder 
whether this is the 1+N problem, because it is not caused by reading 
collection items one by one. I would expect hibernate to fetch all items 
with a SELECT like
   SELECT ... FROM invoice_items WHERE id_invoice IN ( SELECT id FROM 
invoices WHERE ... << the conditions generated from the HQL query >> )
At least that's the way I would do it in MySQL; currently I play with HSQL, 
maybe it's not capable of this.
So is this normal?

Thanks for any useful hints.
Ondra Zizka




-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel