facebook

Hibernate *DAO.save()

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #257813 Reply

    gavin9
    Member

    Greetings,
    I’m new to Hibernate. I have read the MyEclipse Hibernate Quickstart and have made a test application following the tutorial. I can retrieve entities fine, as shown in the tutorial, however I am unable to save records.

    Advertisement ad = new Advertisement("title", "desc", java.util.Calendar.getInstance().getTime());
    dao.save(ad);
    

    As far as the DAO is concerned, the object is being saved:

    14:23:29,090 DEBUG AbstractSaveEventListener:106 - generated identifier: 3, using strategy: org.hibernate.id.IncrementGenerator
    14:23:29,090 DEBUG AbstractSaveEventListener:139 - saving [hbtest.Advertisement#3]
    14:23:29,106 DEBUG AdvertisementDAO:31 - save successful
    

    But when I browse the database there is only 2 records there. Can someone point out what I’m missing?

    TIA

    #257845 Reply

    Are you in a transaction? Do you commit it?
    If you wrote a Spring hibernate junit test, then they automatically roll-back the transaction after the test case completes. Which is actually a neat feature, as you can set up your DB table by deleting all rows, then inserting only the rows you want, run your tests, and at the end of the test, it’s as if you never did anything.

    #261125 Reply

    I had the same confusion and I really like the feature. I have a related issue now that I’ve come to rely on the jUnit tests of my DAO layer. I have used the jUnit tool to create a TestSuite and included all of the appropriate tests in the suite. Each one of these tests runs successfully when right-clicking >> run as >> JUnit Test. The creation of the TestSuite also completes as expected. Now, how do I run it under the framework so that the data for the save() methods don’t commit? This feature works when running the test individually. However, the only way that I can get the TestSuite to run is to add a main method:

    
    public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
    }
    

    In order to run it, i have to run “As A Java Application because it isn’t recognized as a JUnit runnable test (because there are no test methods). I guess I expected to see a “run as” option like “JUnit Test Suite” or something. If I run as a java app it stores data to the database (successfully executing all 15 tests). However, I would like it to avoid the commit() at the database level. How can this be done?

    Travis

Viewing 3 posts - 1 through 3 (of 3 total)
Reply To: Hibernate *DAO.save()

You must be logged in to post in the forum log in