Hallo Johannes,
Yes. They are both in the same directory WEB-INF/classes/hib.
Here is the source code for both file.
HibernateFactory.java
*************************************************************
package hib;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.excalibur.datasource.DataSourceComponent;
import org.apache.avalon.framework.service.ServiceSelector;
// import all classes from your package so you can later persist them
import hib.*;
public class HibernateFactory
extends AbstractLogEnabled
implements PersistenceFactory, Configurable, Serviceable,
Initializable,
Disposable, ThreadSafe
{
private boolean initialized = false;
private boolean disposed = false;
private ServiceManager manager = null;
// do not confuse with Avalon Configuration, Cocoon Session, etc.
net.sf.hibernate.cfg.Configuration cfg;
net.sf.hibernate.SessionFactory sf;
//for debugging: which instance am I using?
long time_start;
public HibernateFactory() {
System.out.println("Hibernate factory instance created");
}
public final void configure(Configuration conf) throws
ConfigurationException {
if (initialized || disposed) {
throw new IllegalStateException ("Illegal call");
}
System.out.println("Hibernate configure called");
}
public final void service(ServiceManager smanager) throws
ServiceException {
if (initialized || disposed) {
throw new IllegalStateException ("Illegal call");
}
if (null == this.manager) {
this.manager = smanager;
}
System.out.println("Hibernate service called");
}
public final void initialize() throws Exception {
if (null == this.manager) {
throw new IllegalStateException("Not Composed");
}
if (disposed) {
throw new IllegalStateException("Already disposed");
}
// adapt:
// build sessionfactory
// map all classes we need
// keep in sync with configuration file
//
try {
cfg = new net.sf.hibernate.cfg.Configuration();
/* ***** ADD PERSISTENT CLASSES, VARIANT 1 ***** */
// persistent classes can be added here using
cfg.addClass(hib.User.class);
// Make sure the corresponding .class and .hbm.xml files are
located in
// (the same directory of) your classpath (e.g.
WEB-INF/classes)
//This method requires correct config of
hibernate.properties file
sf = cfg.buildSessionFactory();
/* ***** ADD PERSISTENT CLASSES, VARIANT 2 ***** */
// alternatively, you might be using a hibernate.cfg.xml
file to load mappings,
// then use the following line instead:
//sf = cfg.configure().buildSessionFactory();
// no additional cfg.addClass(...) statements needed, since
you can define
// mappings in the XML config file
}
catch ( Exception e) {
e.printStackTrace();
getLogger().error("Hibernate:" + e.getMessage());
return;
}
this.initialized = true;
System.out.println("Hibernate initialize called");
}
public final void dispose() {
//
try {
sf.close();
}
catch ( Exception e) {
getLogger().error("Hibernate:" + e.getMessage());
}
finally {
sf = null;
cfg = null;
}
this.disposed = true;
this.manager = null;
System.out.println("Hibernate dispose called");
}
public net.sf.hibernate.Session createSession() {
net.sf.hibernate.Session hs;
DataSourceComponent datasource = null;
// When creating a session, use a connection from
// cocoon's connection pool
try {
// Select the DataSourceComponent named "test"
// This is a normal SQL connection configured in cocoon.xconf
ServiceSelector dbselector =(ServiceSelector)
manager.lookup(DataSourceComponent.ROLE+"Selector");
datasource = (DataSourceComponent) dbselector.select("teste");
//name as defined in cocoon.xconf
hs = sf.openSession(datasource.getConnection());
System.out.println("Criei sess�o com a pooling do cocoon...");
}
catch ( Exception e) {
e.printStackTrace();
getLogger().error("Hibernate: " + e.getMessage());
hs = null;
}
return hs;
}
}
*****************************************************************
PersistenceFactory.java
package hib;
import org.apache.avalon.framework.component.*;
public interface PersistenceFactory extends Component {
String ROLE = PersistenceFactory.class.getName();
public net.sf.hibernate.Session createSession();
}
*****************************************************************
Aditional Notes:
- i used jdk1.5 to compile both classes without problem
- i use tomcat 5.5 as container
- cocoon version is 2.1.7
- hibernate version 2.1.8
Thanks is advance,
CarlosN.
ps: shouldn't we do PersistenceFactory.class.getName(); in other way??
On Sun, 22 May 2005, Johannes Textor wrote:
Hi Carlos !
You should not need to import the class explicitly when
addressing it via packages.
Another question, though: Do you also have the "HibernateFactory"
class in WEB-INF/classes/hib ?
If so, please post the source codes of both to have a look.
Greetings,
Johannes
Carlos M. S. Bento Nogueira schrieb:
hi mike,
that's a good point. i just tried it with the same result. Yes i restarted
tomcat before doing it...
Greetings,
CarlosN.
On Sun, 22 May 2005, Michel Erard wrote:
hi carlos,
did you made an import like this
"importClass(Packages.ch.bfh.hti.elquerito.User);" in your flowscript
file?
regards,
Mike
Am 22.05.2005 um 19:07 schrieb Carlos M. S. Bento Nogueira:
Hello!
I'm using cocoon 2.1.7 on tomcat 5.5 and in my
flowscript when i do:
cocoon.getComponent(Packages.hib.PersistenceFactory.ROLE)
i always get this exception:
java.lang.NoClassDefFoundError: hib/PersistenceFactory$1 .
This PersistenceFactory class is in
\Web-inf\classes\hib\
and the role is declared on
\Web-inf\cocoon.xconf
as
<component class="hib.HibernateFactory" role="hib.PersistenceFactory"/>
What can be the problem here??
The PersistenceFactory is an interface class and Role is a string of this
type:
String ROLE = PersistenceFactory.class.getName();
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]