Hibernate version: 3:1
DB: HSQLDB 1.8.0.2
Ich versuche gerade ein beispiel zum laufen zu kriegen, das ein Objekt anlegt und dann in der DB speichert.
Alles scheint gut zu gehen, aber hinterher ist die DB leer.
Hier mein Code:
mapping:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.jazzanowak.hibernate">
<class name="FanClub" table="FANCLUB">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="name" column="NAME"/>
</class>
</hibernate-mapping>
conf:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property>
<property name="connection.url">
jdbc:hsqldb:file:F:/Developing/workspace/LearningHibernate/data/fans
</property>
<property name="hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<mapping resource="FanClub.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Code:
public class Main {
private SessionFactory sessionFactory;
Log logger = LogFactory.getLog(Main.class);
public Main() {
try {
logger.info("Creating session Factory");
Configuration cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
} catch( HibernateException ex ) {
ex.printStackTrace();
System.exit( 5 );
}
}
public void close() {
sessionFactory.close();
}
public static void main(String[] args) throws Exception {
Main main = new Main();
main.addFanClub("Die jans");
main.showFanClubs();
main.close();
}
private void addFanClub(String name) {
Session sess = null;
try {
sess = sessionFactory.openSession();
sess.beginTransaction();
FanClub fanClub = new FanClub();
fanClub.setName(name);
sess.save( fanClub );
sess.getTransaction().commit();
} catch( HibernateException ex ) {
logger.error("Someting failed ",ex);
} finally {
sess.close();
}
}
private void showFanClubs() {
org.hibernate.Session sess = null;
try {
sess = sessionFactory.openSession();
System.out.println("\nFanclubs:");
List kunden = sess.createQuery("from FanClub").list();
for (int i = 0; i < kunden.size(); i++) {
FanClub fanClub = (FanClub) kunden.get(i);
logger.info("FanClub: " + fanClub.toString());
}
} catch (HibernateException ex) {
logger.error("Error while reading from db", ex);
} finally {
sess.close();
}
}
}
Die FanClub Klasse ist ein simples Bean.
Vielen Dank
|