Hi,
i'm a beginner with hibernate, trying to make my first example.
So I have written the following code:
Code:
package hibernatesample;
import java.io.Serializable;
import java.util.Date;
import junit.framework.TestCase;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* Einfacher Test, der CRUD mit Hibernate demonstriert.
*/
public class CRUDTest extends TestCase implements Serializable{
private SessionFactory _sessionFactory;
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() + 2 * 24 * 60 * 60 * 1000);
private long _id;
/**
* Wir starten eine SessionFactory und erzeugen schon mal einen Termin.
*/
protected void setUp() throws Exception {
super.setUp();
Configuration configuration =
new Configuration().configure();
configuration.setProperty("hibernate.connection.url", "jdbc:h2:file://home/tony/H2/Termin;DB_CLOSE_DELAY=-1");
SchemaExport export =
new SchemaExport(configuration);
export.create(false, true);
_sessionFactory = configuration.buildSessionFactory();
_id = erzeugeTermin(TITEL, BESCHREIBUNG, ORT, ZEIT_PUNKT);
System.out.println("Id: ");
System.out.println(_id);
testLoad();
}
private long erzeugeTermin(
String titel, String beschreibung,
String ort, Date zeitPunkt) {
Termin termin = new Termin();
termin.setTitel(titel);
termin.setBeschreibung(beschreibung);
termin.setOrt(ort);
termin.setZeitPunkt(zeitPunkt);
Session session = null;
Transaction transaction = null;
try {
session = _sessionFactory.openSession();
transaction =
session.beginTransaction();
session.save(termin);
transaction.commit();
}
catch (HibernateException e) {
if (transaction != null) {
transaction.rollback();
throw e;
}
}
finally {
if (session != null) {
session.close();
}
}
return termin.getId();
}
public void testLoad(){
Session session2 = null;
try{
session2 = _sessionFactory.openSession();
Termin termin2 = (Termin) session2.load(Termin.class, _id);
assertEquals(ZEIT_PUNKT, termin2.getZeitPunkt());
assertEquals(TITEL, termin2.getZeitPunkt());
assertEquals(BESCHREIBUNG, termin2.getZeitPunkt());
assertEquals(ORT, termin2.getZeitPunkt());
System.out.print("Test: " + termin2.toString());
}
finally{
if (session2 != null && session2.isConnected()){
session2.close();
}
}
}
}
I got one failure and don't know why it pops up.
The failure sounds like: The method load(Class, Serializable) in the type Session is not applicable for the arguments (Class, long). This failure notice referenced to the session.load in the method testLoad.
Has anybody one idea, why this failure notice pops up?
Thanx