-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate + Spring : database get cleared after inserting
PostPosted: Tue Oct 12, 2004 2:40 pm 
Beginner
Beginner

Joined: Thu Apr 29, 2004 4:31 am
Posts: 42
Hibernate version:
2.1.6
Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:
MySql 4.1.1 alpha
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

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("persistenceManager");
} 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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 12, 2004 7:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
What exactly does "'hibernate related tables' are emptied" mean? This is typically one of two things: either you have the auto schema generation enabled, or you (i.e., Spring) are not commiting transactions.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.