Hallo Alle
Ich bin noch ganz frisch was Hibernate betrifft und wollte mal eine kleine Testanwendung ausprobieren, die im Prinzip nichts anders macht als eine kleine Tabelle zu befüllen, was auch einwandfrei funktioniert, und die Daten dann wieder ausließt, was leider nicht funktioniert (siehe Exception).
Das Beispiel Programm hab ich aus der "Hibernate Reference Documentation 3.1.1".
Hat vielleicht jemand eine Idee, warum mein List nicht funktioniert, bin im Moment leider etwas ratlos.
Hibernate version:
3
Mapping documents:
Code:
<hibernate-mapping>
<class name="events.Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="native" />
</id>
<property name="date" type="timestamp" column="EVENT_DATE" />
<property name="title" />
</class>
</hibernate-mapping>
Code:Code:
package events;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import util.HibernateUtil;
public class EventManager {
public static void main(String[] args) {
EventManager mgr = new EventManager();
if (args[0].equals("store")) {
mgr.createAndStoreEvent("My Event", new Date());
}
else if (args[0].equals("list")) {
List events = mgr.listEvents();
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 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();
}
private List listEvents() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result = session.createQuery("from Event e").list();
session.getTransaction().commit();
return result;
}
}
Full stack trace of any exception that occurs:Code:
[java] Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
[java] at $Proxy0.createQuery(Unknown Source)
[java] at events.EventManager.listEvents(Unknown Source)
[java] at events.EventManager.main(Unknown Source)
[java] Caused by: java.lang.reflect.InvocationTargetException
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[java] at java.lang.reflect.Method.invoke(Method.java:585)
[java] at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
[java] ... 3 more
[java] Caused by: java.lang.NoClassDefFoundError: antlr/ANTLRException
[java] at org.hibernate.hql.ast.ASTQueryTranslatorFactory.createQueryTranslator(ASTQueryTranslatorFactory.java:35)
[java] at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:74)
[java] at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
[java] at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
[java] at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
[java] at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
[java] at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
[java] ... 8 more
[java] Java Result: 1
Ant File:Code:
<project name="hibernate-tutorial" default="compile">
<property name="sourcedir" value="${basedir}/src" />
<property name="targetdir" value="${basedir}/bin" />
<property name="librarydir" value="${basedir}/lib" />
<path id="libraries">
<fileset dir="${librarydir}">
<include name="*.jar" />
</fileset>
</path>
<target name="clean">
<delete dir="${targetdir}" />
<mkdir dir="${targetdir}" />
</target>
<target name="compile" depends="clean, copy-resources">
<javac srcdir="${sourcedir}" destdir="${targetdir}" classpathref="libraries" />
</target>
<target name="copy-resources">
<copy todir="${targetdir}">
<fileset dir="${sourcedir}">
<exclude name="**/*.java" />
</fileset>
</copy>
</target>
<target name="run" depends="compile">
<java fork="true" classname="events.EventManager" classpathref="libraries">
<classpath path="${targetdir}" />
<arg value="${action}" />
</java>
</target>
</project>
Name and version of the database you are using:
HSQLDB
mfG und vielen Dank
Corpse