I tried out the "Getting Started"-example (with the Event class).
It works fine so far, but my saved Objects are not persistent, they only exist as long as my applications runs - if I start my application a second time, Hibernate creates a new table (after it said "Event - Table not found) like nothing happened before.
Does anyone have an idea what Im doing wrong ?
Im using hsqldb1.8.0.1 with Hibernate 3.1.2.
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:db_myfile</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<hibernate-mapping>
<class name="Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="increment"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
</class>
</hibernate-mapping>
import org.hibernate.Session;
import java.util.*;
public class EventManager {
public static void main(String[] args) {
EventManager mgr = new EventManager();
//if (args[0].equals("store")) {
mgr.createAndStoreEvent("My Event1", new Date());
mgr.createAndStoreEvent("My Event2", new Date());
mgr.createAndStoreEvent("My Event3", new Date());
mgr.createAndStoreEvent("My Event4", new Date());
//}
//else if (args[0].equals("list")) {
List events = mgr.listEvents();
System.out.println("Listgröße:" + events.size());
for (int i = 0; i < events.size(); i++) {
Event theEvent = (Event) events.get(i);
System.out.println("Event: " + theEvent.getTitle() +
" Time: " + theEvent.getDate());
}
//}
//HibernateUtil.getSessionFactory().close();
}
private List listEvents() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result = session.createQuery("from Event").list();
session.getTransaction().commit();
return result;
}
private void createAndStoreEvent(String title, Date theDate) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
session.getTransaction().commit();
}
}
|