Hi there!
I bought a book about hibernate 3 from dpunkt.verlag and have some trouble to get the first example running. Yesterday at the office it worked with no problems (winXP). Today I am trying to get it running on my mac but it doesn't work and I can't see the error and get no exceptions ... btw, I am using HSQLDB as used in the book
I figured out, that in my testclass, the actual test isn't executed. The setUp() contains this line:
Configuration cfg = new Configuration().configure();
The .configure() is the last line executed. This is the output from log4j:
0 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.1.3
29 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
65 [main] INFO org.hibernate.cfg.Environment - using CGLIB reflection optimizer
76 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
414 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
480 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
1292 [main] DEBUG org.hibernate.util.DTDEntityResolver - trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd]
1297 [main] DEBUG org.hibernate.util.DTDEntityResolver - recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
1322 [main] DEBUG org.hibernate.util.DTDEntityResolver - located [http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd] in classpath
1497 [main] DEBUG org.hibernate.cfg.Configuration - dialect=org.hibernate.dialect.HSQLDialect
1502 [main] DEBUG org.hibernate.cfg.Configuration - connection.driver_class=org.hsqldb.jdbcDriver
1545 [main] DEBUG org.hibernate.cfg.Configuration - connection.username=sa
1547 [main] DEBUG org.hibernate.cfg.Configuration - connection.password=
1549 [main] DEBUG org.hibernate.cfg.Configuration - connection.url=jdbc:hsqldb:file:db/termine
1552 [main] DEBUG org.hibernate.cfg.Configuration - null<-org.dom4j.tree.DefaultAttribute@82b9b5 [Attribute: name resource value "net/sf/hibernatesample/einfach/Termin.hbm.xml"]
1555 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: net/sf/hibernatesample/einfach/Termin.hbm.xml
1587 [main] DEBUG org.hibernate.util.DTDEntityResolver - trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd]
1590 [main] DEBUG org.hibernate.util.DTDEntityResolver - recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
1593 [main] DEBUG org.hibernate.util.DTDEntityResolver - located [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd] in classpath
2088 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: net.sf.hibernatesample.einfach.Termin -> Termin
2177 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: id -> id
Here are all my codes:
The class to be persisted (Termin.java):
package net.sf.hibernatesample.einfach;
import java.util.Date;
public class Termin {
private long _id;
private String _title;
private String _beschreibung;
private String _ort;
private Date _zeitPunkt;
public String getBeschreibung() {
return _beschreibung;
}
public void setBeschreibung(String _beschreibung) {
this._beschreibung = _beschreibung;
}
public long getId() {
return _id;
}
public void setId(long _id) {
this._id = _id;
}
public String getOrt() {
return _ort;
}
public void setOrt(String _ort) {
this._ort = _ort;
}
public String getTitle() {
return _title;
}
public void setTitle(String _title) {
this._title = _title;
}
public Date getZeitPunkt() {
return _zeitPunkt;
}
public void setZeitPunkt(Date punkt) {
_zeitPunkt = punkt;
}
}
The mappingfile Termin.hbm.xml (same package as class):
<?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="net.sf.hibernatesample.einfach">
<class name="Termin">
<id name="id">
<generator class="native" />
</id>
<property name="titel" />
<property name="beschreibung" />
<property name="zeitPunkt" />
<property name="ort" />
</class>
</hibernate-mapping>
The hibernate-config (hibernate.cfg.xml) in root-folder:
<?xml version="1.0" encoding="UTF-8"?>
<!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="connection.url">jdbc:hsqldb:file:db/termine</property>
<mapping resource="net/sf/hibernatesample/einfach/Termin.hbm.xml" />
</session-factory>
</hibernate-configuration>
And the class with the test:
package net.sf.hibernatesample.einfach;
import java.util.Date;
import junit.framework.TestCase;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class CRUDTests extends TestCase {
private static final String ORT = "Hamburg";
private static final String BESCHREIBUNG = "termin";
private static final String TITEL = "titel";
private static final Date ZEIT_PUNKT = new Date(
System.currentTimeMillis() + 172800000);
private SessionFactory _sf;
private long _id;
protected void setUp() throws Exception {
super.setUp();
Configuration cfg = new Configuration().configure(); // ----- Hier ist Ende!!!
SchemaExport export = new SchemaExport(cfg);
export.create(false, true);
_sf = cfg.buildSessionFactory();
_id = erzeugeTermin(TITEL, BESCHREIBUNG, ORT, ZEIT_PUNKT);
}
public void testLoad() {
Session s = null;
try {
s = _sf.openSession();
Termin termin = (Termin) s.load(Termin.class, _id);
assertEquals(TITEL, termin.getTitle());
assertEquals(BESCHREIBUNG, termin.getBeschreibung());
assertEquals(ORT, termin.getOrt());
assertEquals(ZEIT_PUNKT, termin.getZeitPunkt());
} finally {
if (null != s && s.isConnected()) {
s.close();
}
}
}
private long erzeugeTermin(String titel, String beschreibung, String ort,
Date zeitPunkt) {
Termin termin = new Termin();
termin.setTitle(titel);
termin.setBeschreibung(beschreibung);
termin.setOrt(ort);
termin.setZeitPunkt(zeitPunkt);
Session s = null;
Transaction t = null;
try {
s = _sf.openSession();
t = s.beginTransaction();
s.save(termin);
t.commit();
} catch (HibernateException e) {
if (null != t) {
t.rollback();
throw e;
}
} finally {
if (null != s) {
s.close();
}
}
return termin.getId();
}
}
And log4j.properties (root-folder):
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
I don't get it, what is wrong with it? The junit test fails every time and I get no error or exceptions. Can someone help me please?
Greetings, Sascha