-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: Changes not seen at the database
PostPosted: Mon Dec 15, 2003 7:16 pm 
Newbie

Joined: Mon Dec 15, 2003 7:03 pm
Posts: 12
I have tryied every combination of Session.flush(), Transaction.commit(), Session.connection.commit() and I sitll don't see changes on the database. Objects are loading just fine.

Here is my lates attempt at inserting a new object:

Code:
            Session session = sessionFactory.openSession();
            txn = session.beginTransaction();
            setPrimaryKey(DBCounter.getNextID("inventory_item"));
            session.saveOrUpdate(this); //this is the object to be saved
            session.flush();
            session.connection().commit();
            txn.commit();
            session.close();



Here are the lot statements at of saveOrUpdate():
2003-12-15 15:12:39,915 DEBUG main net.sf.hibernate.engine.Cascades - unsaved-value: -1
2003-12-15 15:12:39,916 DEBUG main net.sf.hibernate.impl.SessionImpl - saveOrUpdate() previously saved instance with id: 48
2003-12-15 15:12:39,923 DEBUG main net.sf.hibernate.impl.SessionImpl - updating [inventory.InventoryItem#48]



Any ideas as to why nothing is being saved?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2003 8:35 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I think it is because <id> is improperly configured. Something wrong with unsaved-value. Could you show your mapping?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2003 9:06 pm 
Newbie

Joined: Mon Dec 15, 2003 7:03 pm
Posts: 12
Code:
<hibernate-mapping>
    <class
        name="inventory.InventoryItem"
        table="inventory_item">

        <id name="ID"
            column="inventory_item_id"
            type="long"
            unsaved-value="-1">
            <generator class="assigned"/>
        </id>

        <property
            name="partNo"
            type="java.lang.String"
            update="true"
            insert="true"
            column="part_no"
        />

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
        />

      <!-- .... code does continue let me know if you wish to see everything -->



Top
 Profile  
 
 Post subject: Here is my unit test ( slightly different approach now)
PostPosted: Mon Dec 15, 2003 9:16 pm 
Newbie

Joined: Mon Dec 15, 2003 7:03 pm
Posts: 12
Code:
public class InventoryItemTest extends TestCase implements TestConstants {


    Session session;

    public InventoryItemTest(String name)
    {
        super(name);
    }

    public void setUp() throws Exception
    {
        try {
            SessionFactory sessionFactory =
                    HibernateSesssionFactoryManager.getFactory();
            session = sessionFactory.openSession();
        } catch (Exception e) {
            Logger.error(getClass(), e);
        }
    }

    public void tearDown() throws Exception
    {
        session.close();
    }


    public void testWriteNew() throws Exception {
        InventoryItem item = new InventoryItem();
        item.setSystemID(TEST_SYSTEM_ID);
        item.setCategory("Carpentry");
        item.setName("Hammer");
        session.save(item, new Long(DBCounter.getNextID("inventory_item")));
        session.connection().commit();
        session.flush();
    }

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2003 9:44 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Take a look at http://www.hibernate.org/116.html#A17

(Not exactly your problem but describes your problem too) :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2003 11:23 pm 
Newbie

Joined: Mon Dec 15, 2003 7:03 pm
Posts: 12
Thanks, but I think my problem is the opposite I have no data going in. Just so you know saveOrUpdate() is just one of the things I have tried. I have also tried using save(obj) , save(obj, id) and update() on a loaded object. Let me also point out that no exceptions are thrown. No errors are logged.

Right now I am trying using my own IdentifierGenerator, but I have also tried hilo, increment, and assigned. With the exception of increment, all of these seem to generate the correct id. So at this point I think the problem is not the generator, but I have not discarded the possibility.

For some reason it simply doesn't trigger the insertion ( btw I have the same problem on updating a previously loaded object).

Here is the mapping file I have now:

Code:
<hibernate-mapping>
    <class
        name="inventory.InventoryItem"
        table="inventory_item">

        <id name="ID"
            column="inventory_item_id"
            type="long"
            unsaved-value="-1">
            <generator class="database.IdGenerator">
                <param name="table">inventory_item</param>
            </generator>

        </id>

        <property
            name="partNo"
            type="java.lang.String"
            update="true"
            insert="true"
            column="part_no"
        />

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
        />

        ....


Here is my unit test
Code:
public class InventoryItemTest extends TestCase implements TestConstants {


    Session session;

    public InventoryItemTest(String name)
    {
        super(name);
    }

    public void setUp() throws Exception
    {
        try {
            SessionFactory sessionFactory =
                    HibernateSesssionFactoryManager.getFactory();
            session = sessionFactory.openSession();
        } catch (Exception e) {
            Logger.error(getClass(), e);
        }
    }

    public void tearDown() throws Exception
    {
        session.close();
    }


    public void testWriteNew() throws Exception {
        InventoryItem item = new InventoryItem();
        item.setSystemID(TEST_SYSTEM_ID);
        item.setCategory("Carpentry");
        item.setName("Hammer");
        session.save(item);
        session.connection().commit();
        session.flush();
    }

}


Here are some relevant log stuff (I think):
Code:
2003-12-15 18:57:07,279 DEBUG main net.sf.hibernate.impl.SessionImpl - opened session
2003-12-15 18:57:07,545 DEBUG main net.sf.hibernate.impl.SessionImpl - saving [inventory.InventoryItem#73]
2003-12-15 18:57:07,550 DEBUG main net.sf.hibernate.connection.DriverManagerConnectionProvider - total checked-out connections: 0
2003-12-15 18:57:07,553 DEBUG main net.sf.hibernate.connection.DriverManagerConnectionProvider - using pooled JDBC connection, pool size: 0
2003-12-15 18:57:07,557 DEBUG main net.sf.hibernate.impl.SessionImpl - flushing session
2003-12-15 18:57:07,560 DEBUG main net.sf.hibernate.impl.SessionImpl - Flushing entities and processing referenced collections
2003-12-15 18:57:07,563 DEBUG main net.sf.hibernate.impl.SessionImpl - Processing unreferenced collections
2003-12-15 18:57:07,565 DEBUG main net.sf.hibernate.impl.SessionImpl - Scheduling collection removes/(re)creates/updates
2003-12-15 18:57:07,569 DEBUG main net.sf.hibernate.impl.SessionImpl - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
2003-12-15 18:57:07,572 DEBUG main net.sf.hibernate.impl.SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2003-12-15 18:57:07,575 DEBUG main net.sf.hibernate.impl.SessionImpl - executing flush
2003-12-15 18:57:07,577 DEBUG main net.sf.hibernate.impl.SessionImpl - post flush
2003-12-15 18:57:07,580 DEBUG main net.sf.hibernate.impl.SessionImpl - closing session
2003-12-15 18:57:07,582 DEBUG main net.sf.hibernate.impl.SessionImpl - disconnecting session
2003-12-15 18:57:07,586 DEBUG main net.sf.hibernate.connection.DriverManagerConnectionProvider - returning connection to pool, pool size: 1
2003-12-15 18:57:07,590 DEBUG main net.sf.hibernate.impl.SessionImpl - transaction completion







Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2003 11:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
2003-12-15 18:57:07,569 DEBUG main net.sf.hibernate.impl.SessionImpl - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
2003-12-15 18:57:07,572 DEBUG main net.sf.hibernate.impl.SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections


Well, whatever you are doing, the session has no objects associated with it when you flush().

Are you implementing Lifecycle?

By the way, the correct sequence is of course:

Code:
session.save(item);
session.flush();
session.connection().commit();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2003 2:14 pm 
Newbie

Joined: Mon Dec 15, 2003 7:03 pm
Posts: 12
and I guess that is exactly my question.... how come?


I am saving the object using save(). The generator whichever I use does seem to work. Loading works by the way. I just fixed the sequence of save(), flush() and commit(), thank you. And still no cigar.

Oh yes, I am implementing Lifecycle...and yes I just RFM on Lifecycle... but I still don't see why that would matter.... HELP!?


Here is the class:


Code:
public class InventoryItem implements Lifecycle {
   
    private long id = -1;
    private String partNo  = "";
    private String name = "";


  ....


Here is the mapping:


Code:
<hibernate-mapping>
    <class
        name="inventory.InventoryItem"
        table="inventory_item">

        <id name="ID"
            column="inventory_item_id"
            type="long"
            unsaved-value="-1">
            <generator class="database.IdGenerator">
                <param name="table">inventory_item</param>
            </generator>

        </id>

        <property
            name="partNo"
            type="java.lang.String"
            update="true"
            insert="true"
            column="part_no"
        />

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
        />
       ....


Here is the TestCase

Code:
public class InventoryItemTest extends TestCase implements TestConstants {


    Session session;

    public InventoryItemTest(String name)
    {
        super(name);
    }

    public void setUp() throws Exception
    {
        try {
            SessionFactory sessionFactory =
                    HibernateSesssionFactoryManager.getFactory();
            session = sessionFactory.openSession();
        } catch (Exception e) {
            Logger.error(getClass(), e);
        }
    }

    public void tearDown() throws Exception
    {
        session.close();
    }


[color=green]   public void testWriteNew() throws Exception {
        InventoryItem item = new InventoryItem();
        item.setSystemID(TEST_SYSTEM_ID);
        item.setCategory("Carpentry");
        item.setName("Hammer");
        session.save(item);
        session.flush();
        session.connection().commit();
    }[/color]

}



Here is what is logged starting with the save():

2003-12-16 09:39:46,078 DEBUG main net.sf.hibernate.impl.SessionImpl - opened session
2003-12-16 09:39:47,596 DEBUG main net.sf.hibernate.impl.SessionImpl - saving [inventory.InventoryItem#74]
2003-12-16 09:39:47,803 DEBUG main net.sf.hibernate.impl.SessionImpl - flushing session
2003-12-16 09:39:47,806 DEBUG main net.sf.hibernate.impl.SessionImpl - Flushing entities and processing referenced collections
2003-12-16 09:39:47,821 DEBUG main net.sf.hibernate.impl.SessionImpl - Processing unreferenced collections
2003-12-16 09:39:47,824 DEBUG main net.sf.hibernate.impl.SessionImpl - Scheduling collection removes/(re)creates/updates
2003-12-16 09:39:47,827 DEBUG main net.sf.hibernate.impl.SessionImpl - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
2003-12-16 09:39:47,830 DEBUG main net.sf.hibernate.impl.SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2003-12-16 09:39:47,833 DEBUG main net.sf.hibernate.impl.SessionImpl - executing flush
2003-12-16 09:39:47,835 DEBUG main net.sf.hibernate.impl.SessionImpl - post flush
2003-12-16 09:39:47,837 DEBUG main net.sf.hibernate.connection.DriverManagerConnectionProvider - total checked-out connections: 0
2003-12-16 09:39:47,840 DEBUG main net.sf.hibernate.connection.DriverManagerConnectionProvider - using pooled JDBC connection, pool size: 0
2003-12-16 09:39:47,844 DEBUG main net.sf.hibernate.impl.SessionImpl - closing session
2003-12-16 09:39:47,846 DEBUG main net.sf.hibernate.impl.SessionImpl - disconnecting session
2003-12-16 09:39:47,850 DEBUG main net.sf.hibernate.connection.DriverManagerConnectionProvider - returning connection to pool, pool size: 1
2003-12-16 09:39:47,853 DEBUG main net.sf.hibernate.impl.SessionImpl - transaction completion


[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2003 2:21 pm 
Newbie

Joined: Mon Dec 15, 2003 7:03 pm
Posts: 12
OK... now we are getting somewhere.

I am no longer implementing Lifecycle and now it saves to db. I still don't know why so if anybody knows the answer that would be great. Since I do need it for the legacy app I am dealing with.


Thanks for the tip Gavin!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2003 2:31 pm 
Newbie

Joined: Mon Dec 15, 2003 7:03 pm
Posts: 12
Ok... maybe I should work on my reading skills...


I was returning false on Lifecycle methods.


I hope somebody else can learn from that mistake.


Cool.... I think that solves it. Case closed.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.