I am unable to get records inserted into the database predictably. I've been looking all over for an answer, but can't find one.
I'm running Hibernate 3.2.6 and HSQL 1.8.0.10 in standalone mode.
When I create and commit a transaction, I am not seeing any changes in the database. Not only do I not get the inserts, I also am not getting the create table queries. There is SQL outputted to the console, and it is correct, but it is clearly not being executed. I have found a few workarounds, but none are going to work out in the long run.
I tried running this and didn't get anything:
Code:
public Long save(){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction trans = session.beginTransaction();
Long id = (Long)session.save(this);
trans.commit();
return id;
}
I tried the code below, with the same result:
Code:
public Long save(){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction trans = session.beginTransaction();
Long id = (Long)session.save(this);
session.flush();
session.clear();
trans.commit();
return id;
If I step through either implementation in the debugger, it works fine. This led me to believe it might be some sort of overloading, so I added a Thred.sleep() before the return. I found that the sleep time had to be at least 500ms to get things into the database.
Another way to get things working is to run a select after the insert, like this:
Code:
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
List result = session.createQuery("from team").list();
session.getTransaction().commit();
return result;
This would place things into the database, but it runs a ridiculous amount of insert and delete queries on the database, an example is shown here (from the HSQL log):
Code:
/*C2*/SET SCHEMA PUBLIC
CONNECT USER SA
SET AUTOCOMMIT FALSE
DELETE FROM TEAMS WHERE TEAM_ID=1
INSERT INTO TEAMS VALUES(1,'2008-06-16 12:59:02.094000000','2008-06-16 12:59:24.208000000','COL',NULL)
COMMIT
SET AUTOCOMMIT TRUE
SET AUTOCOMMIT FALSE
DELETE FROM TEAMS WHERE TEAM_ID=1
INSERT INTO TEAMS VALUES(1,'2008-06-16 12:59:02.094000000','2008-06-16 12:59:24.222000000','COL','COL')
COMMIT
SET AUTOCOMMIT TRUE
/*C1*/DELETE FROM TEAMS WHERE TEAM_ID=1
INSERT INTO TEAMS VALUES(1,'2008-06-16 12:59:02.094000000','2008-06-16 12:59:24.222000000','COL','COL')
COMMIT
SET AUTOCOMMIT TRUE
DISCONNECT
/*C2*/SET AUTOCOMMIT FALSE
INSERT INTO TEAMS VALUES(2,'2008-06-16 12:59:02.098000000','2008-06-16 12:59:02.098000000','FRG','FRG')
COMMIT
SET AUTOCOMMIT TRUE
SET AUTOCOMMIT FALSE
/*C3*/SET SCHEMA PUBLIC
CONNECT USER SA
SET AUTOCOMMIT FALSE
DELETE FROM TEAMS WHERE TEAM_ID=1
INSERT INTO TEAMS VALUES(1,'2008-06-16 12:59:02.094000000','2008-06-16 12:59:30.886000000','COL',NULL)
COMMIT
SET AUTOCOMMIT TRUE
SET AUTOCOMMIT FALSE
DELETE FROM TEAMS WHERE TEAM_ID=1
INSERT INTO TEAMS VALUES(1,'2008-06-16 12:59:02.094000000','2008-06-16 12:59:30.901000000','COL','COL')
COMMIT
SET AUTOCOMMIT TRUE
SET AUTOCOMMIT FALSE
DELETE FROM TEAMS WHERE TEAM_ID=2
INSERT INTO TEAMS VALUES(2,'2008-06-16 12:59:02.098000000','2008-06-16 12:59:30.905000000','FRG',NULL)
COMMIT
SET AUTOCOMMIT TRUE
SET AUTOCOMMIT FALSE
DELETE FROM TEAMS WHERE TEAM_ID=2
INSERT INTO TEAMS VALUES(2,'2008-06-16 12:59:02.098000000','2008-06-16 12:59:30.908000000','FRG','FRG')
COMMIT
SET AUTOCOMMIT TRUE
/*C2*/DELETE FROM TEAMS WHERE TEAM_ID=1
INSERT INTO TEAMS VALUES(1,'2008-06-16 12:59:02.094000000','2008-06-16 12:59:30.901000000','COL','COL')
DELETE FROM TEAMS WHERE TEAM_ID=2
INSERT INTO TEAMS VALUES(2,'2008-06-16 12:59:02.098000000','2008-06-16 12:59:30.908000000','FRG','FRG')
COMMIT
SET AUTOCOMMIT TRUE
DISCONNECT
I thought that might be because there was no equals() method implemented on the class, so I added one. Same result.
Why do I need to sleep to get the insert to go through? Why are there so many insert/delete queries run when I'm doing a select? What do I do to fix these problems?
Thanks in advance.
The mapping file for the class in question is below for reference.
Code:
<hibernate-mapping>
<class name="datacontainers.Team" table="TEAMS" >
<id name="id" column="TEAM_ID">
<generator class="native"></generator></id>
<property name="created" type="timestamp" column="date_created"></property>
<property name="modified" type="timestamp" column="date_modified"></property>
<property name="name"></property>
<property name="abbreviation"></property>
</class>
</hibernate-mapping>