Hi,
I'm building my first Hibernate example refering
http://www.gloegl.de/8.html and
http://www.allapplabs.com/hibernate/getting_started_with_hibernate.htm For a beginner, these two are good i think to develop a simple example.
Once i'll pas thr' errors..will post a link to a tutorial reg. this excercise.
I'm getting following errors:
1)
a) if i use:
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
and
public static final SessionFactory sessionFactory;
then during compile,Error is: variable sessionFactory might have not been initilized.
b) no compile time Error if i use
public static SessionFactory sessionFactory; (omitting final)
c) but when i run:
C:\Project\testHibernate\build\classes>
java src.hibernate.testHibernate
---- Starting Hibernate
Exception in
thread "main" java.lang.NoClassDefFoundError: org/hibernate/Session
at src.hibernate.testHibernate.main(testHibernate.java:10)
2) if i use:
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
net.sf package don't exisit.
Please tell me....if i'm using hibernate3.jar....should i use net.sf??
Thanks...
raminder singh
-------------------------------------------------------
My Project details are:
/testHibernate
|
|-lib
|-ant.jar
|-cglib-2.1.2.jar
|-commons-collections-2.1.1.jar
|-commons-lang.jar
|-commons-logging-1.0.4.jar
|-dom4j-1.6.1.jar
|-hibernate3.jar
|-jdbc2_0-stdext.jar
|-jta.jar
|-log4j-1.2.11.jar
|-mysql-connector-java-3.1.12-bin.jar
|-src
|-hibernate
|-AppLabsUser.hbm.xml
|-AppLabsUser.java
|-HibernateSessionFactory.java
|-testHibernate.java
|-hibernate.cfg.xml
|-log4j.properties
|-build.xml
Files are:
1) AppLabsUser.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="test.AppLabsUser" table="applabsuser">
<id column="USER_ID" name="id" type="java.lang.Long">
<generator class="sequence"/>
</id>
<property column="USER_NAME" length="255" name="userName" not-null="true" type="java.lang.String"/>
<property column="USER_PASSWORD" length="255" name="userPassword" not-null="true" type="java.lang.String"/>
<property column="USER_FIRST_NAME" length="255" name="userFirstName" type="java.lang.String"/>
<property column="USER_LAST_NAME" length="255" name="userLastName" type="java.lang.String"/>
<property column="USER_EMAIL" length="255" name="userEmail" type="java.lang.String"/>
<property column="USER_CREATION_DATE" length="10" name="userCreationDate" type="java.util.Date"/>
<property column="USER_MODIFICATION_DATE" length="10" name="userModificationDate" type="java.util.Date"/>
</class>
</hibernate-mapping>
2) AppLabsUser.java
package src.hibernate;
import java.io.Serializable;
import java.util.Date;
import org.apache.commons.lang.builder.ToStringBuilder;
public class AppLabsUser implements Serializable {
private Long id;
private
String userName;
private String userPassword;
private String userFirstName;
private String userLastName;
private String userEmail;
private Date userCreationDate;
private Date userModificationDate;
/** default constructor */
public AppLabsUser() {
}
/** full constructor */
public AppLabsUser(String userName, String userPassword, String userFirstName, String userLastName, String userEmail, Date
userCreationDate, Date userModificationDate) {
this.userName = userName;
this.userPassword = userPassword;
this.userFirstName = userFirstName;
this.userLastName = userLastName;
this.userEmail = userEmail;
this.userCreationDate = userCreationDate;
this.userModificationDate = userModificationDate;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return this.userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserFirstName() {
return this.userFirstName;
}
public void setUserFirstName(String userFirstName) {
this.userFirstName = userFirstName;
}
public String getUserLastName() {
return this.userLastName;
}
public void setUserLastName(String userLastName) {
this.userLastName = userLastName;
}
public String getUserEmail() {
return this.userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public Date getUserCreationDate() {
return this.userCreationDate;
}
public void setUserCreationDate(Date userCreationDate) {
this.userCreationDate = userCreationDate;
}
public Date getUserModificationDate() {
return this.userModificationDate;
}
public void setUserModificationDate(Date userModificationDate) {
this.userModificationDate = userModificationDate;
}
public String toString() {
return new ToStringBuilder(this)
.append("UserID:", getId())
.append(" / Name:", getUserName())
.toString();
//return "User ID:"+id+" / Name:"+userName;
}
}
3) HibernateSessionFactory.java
package src.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory
{
private static String CONFIG_FILE_LOCATION = "/testHibernate/src/hibernate.cfg.xml;";
public static SessionFactory sessionFactory;
static
{
try
{
// create the SessionFactory from hibernate.cfg.xml
sessionFactory= new Configuration().configure(CONFIG_FILE_LOCATION).buildSessionFactory();
}catch(Throwable ex)
{
System.out.println("Initial SessionFactory Creation Failed...!!!");
}
}
public static Session getHibernateSession() throws HibernateException
{
return sessionFactory.openSession();
}
}
4) testHibernate.java
package src.hibernate;
import org.hibernate.Session;
public class testHibernate
{
public static void main(String args[])
{
System.out.println("---- Starting Hibernate");
Session session = HibernateSessionFactory.getHibernateSession();
System.out.println("---- Hibernate Started..");
System.out.println("----Create User....");
AppLabsUser user01 = new AppLabsUser();
user01.setUserName("raminder.singh");
user01.setUserPassword("IGT");
user01.setUserFirstName("Raminder");
user01.setUserLastName("Singh");
user01.setUserEmail("r.s@igt.com");
System.out.println("----Save User....");
session.save(user01);
System.out.println("---- Closing Down Hibernate");
session.close();
HibernateSessionFactory.sessionFactory.close();
System.out.println("---- Hibernate Closed..");
}
}
5) hibernate.cfg.xml
<!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="show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLMyISAMDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/raminder</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">mysql</property>
<mapping resource="test/hibernate/AppLabsUser.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6) log4j.properties
log4j.rootCategory=INFO, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-5p - %m%n
7) build.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<project name="TestHibernate" default="build" basedir=".">
<property name="dir" value="C:\Project\testHibernate"/>
<property name="home" value="${dir}"/>
<property name="build" value="${home}/build"/>
<property name="srcroot" value="${home}/src"/>
<property name="lib" value="${home}/lib"/>
<target name="build" depends="clean,init,compileJava,Deploy">
<echo message="---------------------------------------------------"/>
<echo message="** ** Build for testHibernate - Completed ** **"/>
<echo message="---------------------------------------------------"/>
</target>
<!-- deletes the build folder and other cleanup stuff -->
<target name="clean">
<!-- delete the root build folder -->
<delete dir="${build}/classes"/>
<delete dir="${build}"/>
</target>
<!-- create the root build folder -->
<target name="init" depends="clean">
<!-- create the time stamp and sets the dstamp, tstamp, and today properties in the current project -->
<tstamp>
<format property="build.datetime" pattern="MM/dd/yyyy hh:mm aa" unit="hour"/>
</tstamp>
<echo message="------------------------------------------------------------------"/>
<echo message="** ** Build for testHibernate - Started at ${build.datetime}"/>
<echo message="------------------------------------------------------------------"/>
<!-- create the root folder for the build process -->
<mkdir dir="${build}"/>
<mkdir dir="${build}/classes"/>
</target>
<!-- this task kicks off build for all the modules, the sequence of module is important here -->
<target name="compileJava" depends="init">
<path id="compileJars">
<pathelement location="${lib}/ant.jar"/>
<pathelement location="${lib}/cglib-2.1.2.jar"/>
<pathelement location="${lib}/commons-collections-2.1.1.jar"/>
<pathelement location="${lib}/commons-lang.jar"/>
<pathelement location="${lib}/commons-logging-1.0.4.jar"/>
<pathelement location="${lib}/dom4j-1.6.1.jar"/>
<pathelement location="${lib}/hibernate3.jar"/>
<pathelement location="${lib}/jdbc2_0-stdext.jar"/>
<pathelement location="${lib}/jta.jar"/>
<pathelement location="${lib}/log4j-1.2.11.jar"/>
<pathelement location="${lib}/mysql-connector-java-3.1.12-bin.jar"/>
<pathelement location="${srcroot}/hibernate.cfg.xml"/>
</path>
<!-- general module, contains common code which is required in many modules and doesn't fit under framework -->
<javac srcdir="${srcroot}" destdir="${build}/classes/" debug="true" classpathref="compileJars"/>
</target>
<target name="Deploy" depends="compileJava">
<copy file="${srcroot}/hibernate/AppLabsUser.hbm.xml" todir="${build}" />
<copy file="${srcroot}/hibernate.cfg.xml" todir="${build}" />
<copy file="${srcroot}/log4j.properties" todir="${build}" />
</target>
</project>