Announcement Announcement Module
Collapse
No announcement yet.
Hibernate + Spring : database get cleared after inserting Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


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

      regards
      marco

      Comment

      Working...
      X