I am using Hibernate 3.0.5 with struts 1.2.4 on Debian Linux
problems to get Configuration initialized ... it appears hibernate ware able to get properties value read from default hibernate.cfg.xml correctly ... but throw ".
NoClassDefFoundError: javax/transaction/Synchronization"
appreciated for your value comments
max
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.0.5
Mapping documents:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- local connection properties -->
<property name="connection.url">
jdbc:mysql://localhost/club?useUnicode=true&characterEncoding=utf8
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.username">club</property>
<property name="connection.password">club</property>
<!-- dialect for MySQL -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- JDBC connection pool (use the built-in) -->
<!--property name="connection.pool_size">1</property-->
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="use_outer_join">true</property>
<!--property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property-->
<!--property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property-->
<!--property name="jta.UserTransaction">java:comp/UserTransaction</property-->
<!-- Use the C3P0 connection pool. -->
<!--property name="c3p0.min_size">2</property-->
<!--property name="c3p0.max_size">5</property-->
<!--property name="c3p0.timeout">900</property-->
<mapping resource="com/cfreecom/club/model/Cat.hbm.xml" />
<!--mapping resource="com/cfreecom/club/model/User.hbm.xml" /-->
<!--mapping resource="com/cfreecom/club/model/Role.hbm.xml" /-->
<!--mapping resource="com/cfreecom/club/model/Photo.hbm.xml" /-->
</session-factory>
</hibernate-configuration>
Code between sessionFactory.openSession() and session.close():
////////////////// HibernateUtil.java ///////////////////////
import org.apache.commons.logging.Log;
public class HibernateUtil {
protected Log log = LogFactory.getLog(HibernateUtil.class);
private static SessionFactory sessionFactory = null;
static {
try {
/*
* Use the mappings and properties specified in
* an application resource named hibernate.cfg.xml.
*/
Configuration cfg = new Configuration();
sessionFactory = cfg.configure().buildSessionFactory();
} catch (Throwable t) {
t.printStackTrace();
throw new ExceptionInInitializerError(t);
}
}
public static Session getSession() throws HibernateException {
return sessionFactory.openSession();
}
}
//////////////////////////// CatAction //////////////////////////////
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Configuration;
import com.cfreecom.club.util.*;
import com.cfreecom.club.model.*;
import com.cfreecom.club.model.dao.*;
public class CatAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Log log = LogFactory.getLog(CatAction.class);
System.out.println("Hibernate - CatAction ...");
Session session = null;
try {
System.out.println("ActAction ... try ");
session = HibernateUtil.getSession();
System.out.println("HibernateUtil.getSession(): "+session.toString());
Transaction tx = session.beginTransaction();
System.out.println("Transaction: "+tx.toString());
// do your job here ...
Cat cat = new Cat();
cat.setId("Cat1");
cat.setName("Cat1");
cat.setWeight(new java.math.BigDecimal(7.4f));
session.save(cat, session);
System.out.println("session save ");
tx.commit();
System.out.println("trans commit ");
} catch (HibernateException e) {
System.out.println("HibernateException: "+e.getMessage());
e.printStackTrace();
} finally {
if(session != null) {
try {
session.close();
System.out.println("session close ");
} catch (HibernateException e) {
System.out.println("HibernateException: "+e.getMessage());
e.printStackTrace();
}
}
}
return (mapping.getInputForward());
}
public void printCol(String str) {
System.out.print(str);
System.out.print(" | ");
}
}
Full stack trace of any exception that occurs:
Hibernate - CatAction ...
ActAction ... try
15:10:28,066 INFO Environment:464 - Hibernate 3.0.5
15:10:28,067 INFO Environment:477 - hibernate.properties not found
15:10:28,069 INFO Environment:510 - using CGLIB reflection optimizer
15:10:28,070 INFO Environment:540 - using JDK 1.4 java.sql.Timestamp handling
15:10:28,219 INFO Configuration:1110 - configuring from resource: /hibernate.cfg.xml
15:10:28,220 INFO Configuration:1081 - Configuration resource: /hibernate.cfg.xml
15:10:32,265 INFO Configuration:444 - Mapping resource: com/cfreecom/club/model/Cat.hbm.xml
15:10:38,711 INFO HbmBinder:260 - Mapping class: com.cfreecom.club.model.Cat -> cat
15:10:38,730 INFO Configuration:1222 - Configured SessionFactory: null
15:10:38,730 INFO Configuration:875 - processing extends queue
15:10:38,731 INFO Configuration:879 - processing collection mappings
15:10:38,736 INFO Configuration:888 - processing association property references
15:10:38,737 INFO Configuration:917 - processing foreign key constraints
15:10:38,813 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
15:10:38,813 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 20
15:10:38,814 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
15:10:38,823 INFO DriverManagerConnectionProvider:80 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/club?useUnicode=true&characterEncoding=utf8
15:10:38,824 INFO DriverManagerConnectionProvider:86 - connection properties: {user=club, password=****}
15:10:39,021 INFO SettingsFactory:77 - RDBMS: MySQL, version: 4.1.11-Debian_1-log
15:10:39,022 INFO SettingsFactory:78 - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.8 ( $Date: 2005/04/14 20:36:13 $, $Revision: 1.27.4.64 $ )
15:10:39,052 INFO Dialect:92 - Using dialect: org.hibernate.dialect.MySQLDialect
15:10:39,060 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
15:10:39,063 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
15:10:39,064 INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled
15:10:39,064 INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled
15:10:39,065 INFO SettingsFactory:136 - JDBC batch size: 15
15:10:39,070 INFO SettingsFactory:139 - JDBC batch updates for versioned data: disabled
15:10:39,072 INFO SettingsFactory:144 - Scrollable result sets: enabled
15:10:39,072 INFO SettingsFactory:152 - JDBC3 getGeneratedKeys(): enabled
15:10:39,073 INFO SettingsFactory:160 - Connection release mode: null
15:10:39,074 INFO SettingsFactory:184 - Maximum outer join fetch depth: 2
15:10:39,075 INFO SettingsFactory:187 - Default batch fetch size: 1
15:10:39,075 INFO SettingsFactory:191 - Generate SQL with comments: disabled
15:10:39,076 INFO SettingsFactory:195 - Order SQL updates by primary key: disabled
15:10:39,132 INFO SettingsFactory:334 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
15:10:39,136 INFO ASTQueryTranslatorFactory:21 - Using ASTQueryTranslatorFactory
15:10:39,136 INFO SettingsFactory:203 - Query language substitutions: {}
15:10:39,137 INFO SettingsFactory:209 - Second-level cache: enabled
15:10:39,137 INFO SettingsFactory:213 - Query cache: disabled
15:10:39,138 INFO SettingsFactory:321 - Cache provider: org.hibernate.cache.EhCacheProvider
15:10:39,142 INFO SettingsFactory:228 - Optimize cache for minimal puts: disabled
15:10:39,143 INFO SettingsFactory:237 - Structured second-level cache entries: disabled
15:10:39,151 INFO SettingsFactory:257 - Echoing all SQL to stdout
15:10:39,152 INFO SettingsFactory:261 - Statistics: disabled
15:10:39,152 INFO SettingsFactory:265 - Deleted entity synthetic identifier rollback: disabled
15:10:39,158 INFO SettingsFactory:279 - Default entity-mode: pojo
java.lang.NoClassDefFoundError: javax/transaction/Synchronization
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1005)
at com.cfreecom.club.util.HibernateUtil.<clinit>(HibernateUtil.java:24)
at com.cfreecom.club.action.CatAction.execute(CatAction.java:40)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397)
Name and version of the database you are using: MySQL 4.1.11
mapping hbm.xml :
///////////// Cat.hbm.xml ///////////////////
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="com.cfreecom.club.model">
<class name="Cat" table="cat">
<id
column="cat_id"
name="id"
type="string"
>
<generator class="vm" />
</id>
<property
column="sex"
length="1"
name="sex"
not-null="false"
type="string"
/>
<property
column="name"
length="16"
name="name"
not-null="true"
type="string"
/>
<property
column="weight"
length="10"
name="weight"
not-null="false"
type="big_decimal"
/>
</class>
</hibernate-mapping>
POJO Cat.java:
/////////////////// POJO Cat.java ////////////////////////
import com.cfreecom.club.model.base.BaseCat;
/**
* This is the object class that relates to the cat table.
* Any customizations belong here.
*/
public class Cat extends BaseCat {
/*[CONSTRUCTOR MARKER BEGIN]*/
public Cat () {
super();
}
/**
* Constructor for primary key
*/
public Cat (java.lang.String _id) {
super(_id);
}
/**
* Constructor for required fields
*/
public Cat (
java.lang.String _id,
java.lang.String _name) {
super (
_id,
_name);
}
/*[CONSTRUCTOR MARKER END]*/
}
/////////////////////// BaseCat.java //////////////////////////
import java.io.Serializable;
public abstract class BaseCat implements Serializable {
public static String PROP_WEIGHT = "weight";
public static String PROP_SEX = "sex";
public static String PROP_NAME = "name";
public static String PROP_ID = "id";
private int hashCode = Integer.MIN_VALUE;
// primary key
private java.lang.String _id;
// fields
private java.lang.String _sex;
private java.lang.String _name;
private java.math.BigDecimal _weight;
// constructors
public BaseCat () {
initialize();
}
/**
* Constructor for primary key
*/
public BaseCat (java.lang.String _id) {
this.setId(_id);
initialize();
}
/**
* Constructor for required fields
*/
public BaseCat (
java.lang.String _id,
java.lang.String _name) {
this.setId(_id);
this.setName(_name);
initialize();
}
protected void initialize () {}
/**
* Return the unique identifier of this class
* @hibernate.id
* generator-class="vm"
* column="cat_id"
*/
public java.lang.String getId () {
return _id;
}
/**
* Set the unique identifier of this class
* @param _id the new ID
*/
public void setId (java.lang.String _id) {
this._id = _id;
this.hashCode = Integer.MIN_VALUE;
}
/**
* Return the value associated with the column: sex
*/
public java.lang.String getSex () {
return _sex;
}
/**
* Set the value related to the column: sex
* @param _sex the sex value
*/
public void setSex (java.lang.String _sex) {
this._sex = _sex;
}
/**
* Return the value associated with the column: name
*/
public java.lang.String getName () {
return _name;
}
/**
* Set the value related to the column: name
* @param _name the name value
*/
public void setName (java.lang.String _name) {
this._name = _name;
}
/**
* Return the value associated with the column: weight
*/
public java.math.BigDecimal getWeight () {
return _weight;
}
/**
* Set the value related to the column: weight
* @param _weight the weight value
*/
public void setWeight (java.math.BigDecimal _weight) {
this._weight = _weight;
}
public boolean equals (Object obj) {
if (null == obj) return false;
if (!(obj instanceof com.cfreecom.club.model.base.BaseCat)) return false;
else {
com.cfreecom.club.model.base.BaseCat mObj = (com.cfreecom.club.model.base.BaseCat) obj;
if (null == this.getId() || null == mObj.getId()) return false;
else return (this.getId().equals(mObj.getId()));
}
}
public int hashCode () {
if (Integer.MIN_VALUE == this.hashCode) {
if (null == this.getId()) return super.hashCode();
else {
String hashStr = this.getClass().getName() + ":" + this.getId().hashCode();
this.hashCode = hashStr.hashCode();
}
}
return this.hashCode;
}
public String toString () {
return super.toString();
}
}