Hi.Everyone.
I am a new one for Our Hibernate.
I encountered one question which maybe relate jdk version.I don't confirm.
I am reading Hibernate Reference V3.2.2.
create one entity class, one hibernate configure file,a hibernate assist class and one ant script.
when I run build.xml ,the error occurred.What is wrong with me?
Code:
The Entity Class :
package events;
import java.util.Date;
public class Event {
private Long id;
private String title;
private Date date;
public Event(){}
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
the manager:
Code:
package events;
import org.hibernate.Session;
import java.util.Date;
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());
}
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();
}
}
hibernate.cfg.xml:
Code:
<?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>
<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>
HibernateUtil.java
Code:
package util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configure = new Configuration().configure();
sessionFactory = configure
.buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed. "
+ ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
ant build.xml :
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}" source="1.5" destdir="${targetdir}" debug="true" 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>
the error.....:
Code:
Buildfile: D:\developDir\workspace\MyEclipse6.0\MyHibernateReferenceCode\build.xml
clean:
[delete] Deleting directory D:\developDir\workspace\MyEclipse6.0\MyHibernateReferenceCode\bin
[mkdir] Created dir: D:\developDir\workspace\MyEclipse6.0\MyHibernateReferenceCode\bin
copy-resources:
[copy] Copying 3 files to D:\developDir\workspace\MyEclipse6.0\MyHibernateReferenceCode\bin
[copy] Copied 2 empty directories to 1 empty directory under D:\developDir\workspace\MyEclipse6.0\MyHibernateReferenceCode\bin
compile:
[javac] Compiling 3 source files to D:\developDir\workspace\MyEclipse6.0\MyHibernateReferenceCode\bin
run:
[java] log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
[java] log4j:WARN Please initialize the log4j system properly.
[java] Initial SessionFactory creation failed. java.lang.UnsupportedClassVersionError: Bad version number in .class file
[java] Exception in thread "main" java.lang.ExceptionInInitializerError
[java] at util.HibernateUtil.<clinit>(HibernateUtil.java:17)
[java] at events.EventManager.main(EventManager.java:11)
[java] Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class file
[java] at java.lang.ClassLoader.defineClass1(Native Method)
[java] at java.lang.ClassLoader.defineClass(Unknown Source)
[java] at java.security.SecureClassLoader.defineClass(Unknown Source)
[java] at java.net.URLClassLoader.defineClass(Unknown Source)
[java] at java.net.URLClassLoader.access$100(Unknown Source)
[java] at java.net.URLClassLoader$1.run(Unknown Source)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(Unknown Source)
[java] at java.lang.ClassLoader.loadClass(Unknown Source)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
[java] at java.lang.ClassLoader.loadClass(Unknown Source)
[java] at java.lang.ClassLoader.loadClassInternal(Unknown Source)
[java] at java.lang.Class.forName0(Native Method)
[java] at java.lang.Class.forName(Unknown Source)
[java] at org.hibernate.connection.DriverManagerConnectionProvider.configure(DriverManagerConnectionProvider.java:57)
[java] at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
[java] at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:56)
[java] at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:397)
[java] at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62)
[java] at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1933)
[java] at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1216)
[java] at util.HibernateUtil.<clinit>(HibernateUtil.java:11)
[java] ... 1 more
[java] Java Result: 1
BUILD SUCCESSFUL
Total time: 6 seconds
Thank you for your thinking ..