Results 1 to 3 of 3

Thread: Hibernate + Spring : database get cleared after inserting

  1. #1
    Join Date
    Sep 2004
    Location
    London
    Posts
    311

    Default Hibernate + Spring : database get cleared after inserting

    Hi all,
    i am using Hibernate together with Spring and i am trying to insert values into my databasae.
    my Entry.hbm.xml is as follows

    <?xml version="1.0"?>

    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

    <hibernate-mapping>
    <class
    name="com.myapp.hibernate.HibernateEntry"
    table="entries"
    dynamic-update="false"
    dynamic-insert="false"
    >

    <id
    name="id"
    column="id"
    type="int"
    unsaved-value="0"
    >
    <generator class="increment">
    </generator>
    </id>

    <property
    name="date"
    type="java.util.Date"
    update="true"
    insert="true"
    column="date"
    />

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

    <property
    name="type"
    type="int"
    update="true"
    insert="true"
    column="type"
    />

    <property
    name="amount"
    type="double"
    update="true"
    insert="true"
    column="amount"
    />

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

    <!--
    To add non XDoclet property mappings, create a file named
    hibernate-properties-HibernateEntry.xml
    containing the additional properties and place it in your merge dir.
    -->

    </class>

    <query name="TypeQuery"><![CDATA[
    from com.myapp.hibernate.HibernateEntry e WHERE e.type = ? and e.user = ? order by e.date asc
    ]]></query>
    <query name="DateQuery"><![CDATA[
    from com.myapp.hibernate.HibernateEntry e WHERE e.user = ? and e.date >= ? and e.date <=? order by e.date asc
    ]]></query>
    <query name="DateTypeQuery"><![CDATA[
    from com.myapp.hibernate.HibernateEntry e WHERE e.type = ? and e.user = ? and e.date >= ? and e.date <=? order by e.date asc
    ]]></query>

    </hibernate-mapping>

    i am using following code (thru Spring)for inserting values into database..

    public void insert(Entry data) throws PersistenceException {
    try {
    _log.error("INSERTING ENTRY...:" + data.getDescription());

    getHibernateTemplate().saveOrUpdate(data);
    } catch(Exception e) {
    _log.error("Exception in creating HibernateEntry\n" + e);

    throw new PersistenceException(e);
    }
    }


    in my test i am calling this method, and whenever this gets called all the 'hibernate related tables' are emptied..
    i cannot figure out why....

    below is the code of my test class

    public class BaseHibernateTest extends MenagerieTest {

    private AbstractApplicationContext context;

    private Log log = LogFactory.getLog(getClass());

    private static Map applicationContextCache = new HashMap();

    protected PersistenceManager manager;

    protected final ApplicationContext getContext() {
    return context;
    }


    protected static ApplicationContext appContext = null;

    static {
    appContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
    }

    public void setUp() {
    try {
    //appContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
    manager = (PersistenceManager)appContext.getBean("persistenc eManager");
    } catch(Exception e) {
    System.err.println("EXCEPTION IN SETUP\n" + e);
    }
    }

    public void tearDown() {
    try {
    super.tearDown();
    } catch(Exception e) {
    System.err.println("Exception in BaseHibernateTest.tearDown()\n" + e);
    }
    }

    public void testCreateEntry() {
    try {
    Entry entry = getEntry();
    manager.insert(entry);
    System.err.println("Entry created..");

    Object[] paramValues = new Object[]{new Integer("100"),"TestUser"};
    Collection coll = manager.query("TypeQuery", paramValues);
    assertEquals("Testing results size",coll.size(),1);
    Iterator i = coll.iterator();
    while( i.hasNext()) {
    Entry found = (Entry)i.next();
    System.err.println("FOUND ENTRY:" + found);
    manager.delete(found);
    }
    System.err.println("************* TEST SUCCESSFUL!!!!");

    } catch(Exception e) {
    System.err.println("Exception in testCreateEntry!\n" + e);
    fail();
    }
    }

    private Entry getEntry() {
    Entry entry = manager.getEmptyEntry();
    entry.setDescription("TestExpense");
    Date date = new Date();
    System.err.println("a today date is"+date);
    entry.setDate(new Date());
    entry.setType(100);
    entry.setAmount(2.0);
    entry.setUser("TestUser");
    return entry;
    }

    }



    anyone can help?

    thanx in advance and regards
    marco

  2. #2
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    Just at a quick glance, could your problem be your LocalSessionFactoryBean declaration having an incorrect hibernate.hbm2ddl.auto setting? Allowed values are:

    create = create the database tables from scratch each time LocalSessionFactoryBean loads

    create-drop = as with create, but drop all database tables when LocalSessionFactoryBean terminates

    update = create tables as needed, but otherwise don't drop tables

    Typically I use "create" when making schema changes or wanting to clear out all my test data. Then "update" the rest of the time, so my seed data is preserved between application restarts.

  3. #3
    Join Date
    Sep 2004
    Location
    London
    Posts
    311

    Default

    Hello,
    thank you!!! i guess that is the reason.... i copied some samples from the net where hibernate.hbm2ddl settings is create......

    regards
    marco

Similar Threads

  1. Replies: 1
    Last Post: Sep 21st, 2005, 04:37 AM
  2. Replies: 2
    Last Post: Mar 10th, 2005, 02:38 PM
  3. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM
  4. Replies: 6
    Last Post: Oct 13th, 2004, 08:36 AM
  5. Replies: 7
    Last Post: Aug 21st, 2004, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •