I am developing a plugin using Eclipse 3.2, MySQL 4.2, and has hibernate3, log4j and junit as dependencies. When I run it as a JUnit in order to test the db connection, it runs fine. However, when I ran it in a runtime workbench, I got the following exception.
HibernateUtil failed. org.hibernate.HibernateException: /hibernate.cfg.xml not found
org.hibernate.HibernateException: /hibernate.cfg.xml not found at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
I've statically found the hibernate.cfg.xml resource using Platform.getBundle and from there derived the URL of the hibernate.cfg.xml resource and gotten the plugin to find it, but then I simply get the "Reading mappings from resource: MappingException: Resource:Patient.hbm.xml not found" errors. When I've used the same strategy to find the Patient.hbm.xml, it doesn't work.
This is the code in the static initializer of HibernateUtil:
System.out.println("HibernateUtil.classloader = "+Thread.currentThread().getContextClassLoader());
Bundle bundle = Platform.getBundle("YetAnotherTry");
Thread.currentThread().setContextClassLoader(bundle.getClass().getClassLoader());
System.out.println("HibernateUtil.init>classloader= "+Thread.currentThread().getContextClassLoader());
URL findURL = bundle.getResource("hibernate.cfg.xml");
URL finalURL = FileLocator.resolve(findURL);
URL patURL = bundle.getResource("yetanothertry/medrecmodel/Patient.hbm.xml");
patURL = FileLocator.resolve(patURL);
File file = new File(patURL.toURI());
cfg.addResource(file.toString());
sessionFactory = cfg.configure(finalURL).buildSessionFactory();
I get:
HibernateUtil.classloader = org.eclipse.core.runtime.internal.adaptor.ContextFinder@119298d
HibernateUtil.init>classloader= org.eclipse.core.launcher.Main$StartupClassLoader@9304b1.
HibernateUtil failed. org.hibernate.MappingException: Resource: E:\Java\bin\yetanothertry\medrecmodel\Patient.hbm.xml not found
My plugin was derived simply from the NewPlugin Wizard and uses the "plugin with a view" template. In the "SampleView" class, under SampleView.makeAction(), I placed the following code in the action1 = new Action() inner class
action1 = new Action() {
public void run() {
Patient pat = Patient.getPatient("Test");
showMessage(pat.getFirstName()+" "+pat.getLastName());
}
};
The Patient.getPatient("Test") uses Hibernate to find the name:
public static Patient getPatient(String firstname){
Criteria crit = HibernateUtil.getSession().createCriteria(Patient.class);
HibernateUtil.commitTransaction();
crit.add(Expression.like("firstName",firstname));
List<Patient> list = crit.list();
Iterator iter = list.iterator();
while (iter.hasNext()){
pat = (Patient)iter.next();
if (pat.getFirstName().equals(firstname))
break;
}
return pat;
}
It works fine in the JUnit testing.
Hibernate version: 3.1
Mapping documents:
<?xml version="1.0" encoding="UTF-8"?>
<!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="connection.driver_class" >
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/medrec
</property>
<property name="connection.username">
mm
</property>
<property name="connection.password"></property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="yetanothertry/medrecmodel/Patient.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Patient.hbm.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate-mapping" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="yetanothertry.medrecmodel.Patient" table="patient">
<id name="id" column="patient_id">
<generator class="native"/>
</id>
<property name="lastName" column="lastname" />
<property name="firstName" column="firstname" />
<property name="birthdate" column="birthdate" type="java.sql.Date"/>
<property name="gender" column="gender" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Patient.java file:
/*
* Created on Oct 5, 2005
*/
package yetanothertry.medrecmodel;
import java.text.*;
import java.util.*;
import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.ui.views.properties.IPropertySource;
import org.hibernate.Session;
Code:
/**
* @author RAD
*/
public class Patient {
private String lastName = null;
private String firstName = null;
private Long id;
private java.sql.Date birthdate = null;
private String gender = null;
static Logger log = Logger.getLogger(Patient.class);
public Patient() {}
public Patient(String name) {
List list = HibernateUtil.getSession().createQuery("from Patient where name= '"+name+"'").list();
log.info(list.toString());
}
public Patient(String last,String first,String gender, String bday) {
this.lastName = last;
this.firstName = first;
this.gender = gender;
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
try {
birthdate = new java.sql.Date(df.parse(bday).getTime());
System.out.println("Patient.dateFormat. birthdate= "+birthdate+". bday= "+bday);
}catch(ParseException pe) {System.out.println("Patient.init> "+pe);}
}
public void setLastName(String last) { this.lastName=last;}
public String getLastName() { return lastName;}
public String getFirstName() { return firstName;}
public void setFirstName(String firstName) {this.firstName = firstName;}
public Long getId() { return id;}
private void setId(Long id) { this.id = id;}
public java.sql.Date getBirthdate() {return birthdate;}
public void setBirthdate(java.sql.Date birthday) {this.birthdate = birthday;}
public String getGender() {return gender;}
public void setGender(String gender) {this.gender = gender;}
public static List getPatients() {
System.out.println("Patient.getPatients called");
Session session = HibernateUtil.getSession();
session.beginTransaction();
List list = session.createQuery("from Patient").list();
session.getTransaction().commit();
log.info(list.toString());
return list;
}
public static Patient getPatient(String firstName, String lastName) {
System.out.println("Patient.getPatient called.");
Patient pat = null;
List allPatients = getPatients();
System.out.println("Patient.getPatient allPatients = "+allPatients);
Iterator ptIter = allPatients.iterator();
System.out.println("Patient.getPatient iterator. pat= "+ptIter);
search:
while (ptIter.hasNext()) {
pat = (Patient)ptIter.next();
System.out.println("Patient.getPatient iterator. pat= "+pat);
//search by last name first and make a list of all matches
if (firstName!=null) {
if (firstName.equals(pat.getFirstName())) {
break search;
}
}
}
return pat;
}
public boolean equals(Object obj) {
if (obj==this) return true;
if (!(obj instanceof Patient)) return false;
Patient pat = (Patient)obj;
if (!(pat.getFirstName().equals(getFirstName()))) return false;
if (!(pat.getLastName().equals(getLastName()))) return false;
if (!(pat.getBirthdate().equals(getBirthdate()))) return false;
return true;
}
public String toString() { return lastName+" , "+ firstName+". ID= "+getId();}
}
Full Stack Trace
HibernateUtil called
HibernateUtil.classloader = org.eclipse.core.runtime.internal.adaptor.ContextFinder@119298d
HibernateUtil.init>classloader= org.eclipse.core.launcher.Main$StartupClassLoader@9304b1. cload= org.eclipse.core.launcher.Main$StartupClassLoader@9304b1
HibernateUtil failed. org.hibernate.MappingException: Resource: yetanothertry/medrecmodel/Patient.hbm.xml not found
org.hibernate.MappingException: Resource: yetanothertry/medrecmodel/Patient.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:479)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1465)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1433)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1414)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1390)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1325)
at yetanothertry.medrecmodel.HibernateUtil.<clinit>(HibernateUtil.java:40)
Line 40 is sessionFactory = cfg.configure(finalURL).buildSessionFactory() line noted above
at yetanothertry.medrecmodel.Patient.getPatients(Patient.java:59)
at yetanothertry.medrecmodel.Patient.getPatient(Patient.java:70)
at yetanothertry.views.PatientView$2.run(PatientView.java:123)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:499)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:539)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:488)
at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:400)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:66)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:928)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3348)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2968)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:1914)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:1878)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:419)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.IDEApplication.run(IDEApplication.java:95)
at org.eclipse.core.internal.runtime.PlatformActivator$1.run(PlatformActivator.java:78)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:92)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:68)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:400)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.core.launcher.Main.invokeFramework(Main.java:336)
at org.eclipse.core.launcher.Main.basicRun(Main.java:280)
at org.eclipse.core.launcher.Main.run(Main.java:977)
at org.eclipse.core.launcher.Main.main(Main.java:952)
directory arrangement:
src
>yetanothertry
>>medrecmodel
>>>Patient.java
>>>Patient.hbm.xml
>>views
>>>SampleView.java
>>test
>>>PatientTest.java
>hibernate.cfg.xml
>log4j.properties
Name and version of the database you are using: MySQL Server 4.1
The generated SQL (show_sql=true): None: it never gets that far
Debug level Hibernate log excerpt: See above exception stack
I'm stumped. Why does this work from the Eclipse IDE as a simple junit application and fail when run as a plugin? Thanks in advance!
Roy