I'm trying to use Hibernate as a newbie.
I use Tomcat 5.0.24, Hibernate 2.1.3 on Windows 2000 pro sp4
And I expected the error message as follow
Error reading resource: factory/material/MaterialDTO.hbm.xml
factory.material.ConnectionFactory.<init>(ConnectionFactory.java:63)
(line 63 is :Configuration cfg = new Configuration().addClass(MaterialDTO.class);)
all the listing files are in the same package....
====== MaterialDTO.hbm.xml =====
Code:
<?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-mapping package="factory.material">
<class name="MaterialDTO" table="material">
<id name="matID" column="MATID" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="matNum" column="MATNUM" not-null="true"/>
<property name="matType" column="MATTYPE"/>
<property name="matName" column="MATNAME" not-null="true"/>
<property name="matProvider" column="MATPROVIDER"/>
<property name="matUnit" column="MATUNIT"/>
<property name="matCost" column="MATCOST"/>
<property name="isDelete" column="ISDELETE"/>
</class>
</hibernate-mapping>
===== MaterialDTO.java =====
Code:
package factory.material;
import java.io.Serializable;
public class MaterialDTO implements Serializable
{
private String matID = null;
private String matNum = null;
private String matName = null;
private String matType = null;
private String matUnit = null;
private String matProvider = null;
private float matCost = 0.0f;
private String isDelete = null;
public void setMatID(String matID)
{
this.matID = matID;
}
public void setMatNum(String matNum)
{
this.matNum = matNum;
}
public void setMatName(String matName)
{
this.matName = matName;
}
public void setMatType(String matType)
{
this.matType = matType;
}
public void setMatUnit(String matUnit)
{
this.matUnit = matUnit;
}
public void setMatProvider(String matProvider)
{
this.matProvider = matProvider;
}
public void setMatCost(float matCost)
{
this.matCost = matCost;
}
public void setIsDelete(String isDelete)
{
this.isDelete = isDelete;
}
public String getMatID()
{
return this.matID;
}
public String getMatNum()
{
return this.matNum;
}
public String getMatName()
{
return this.matName;
}
public String getMatType()
{
return this.matType;
}
public String getMatUnit()
{
return this.matUnit;
}
public String getMatProvider()
{
return this.matProvider;
}
public float getMatCost()
{
return this.matCost;
}
public String getIsDelete()
{
return this.isDelete;
}
}
====== ConnectionFactory.java =====
Code:
package factory.material;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
/**
* Singleton class that creates and returns an open Hibernate <code>Session</code> to the user.
*
* Copyright 2003 Edward Hand
*
* @author Edward Hand
*
*/
/*
* This class was created to encapsulate the Hibernate SessionFactory and assist
* the service layer, in this example consisting of ItemService.
*
*/
public class ConnectionFactory
{
private static ConnectionFactory instance = null;
private SessionFactory sessionFactory = null;
private ConnectionFactory()
{
// Establish SessionFactory for Hibernate
try
{
/*
* The Hibernate Configuration will contain all the classes to be
* persisted by Hibernate. For each class persisted, Hibernate will
* expect to find a ClassName.hbm.xml file in the same location as the
* class file. This XML file will define the mapping between the Java
* object and the database.
*
* To add additional classes to the configuration, you may cascade the
* method calls thusly:
*
* Configuration cfg = new Configuration().
* addClass(Foo.class).
* addClass(Bar.class);
*
*/
Configuration cfg = new Configuration().addClass(MaterialDTO.class);
sessionFactory = cfg.buildSessionFactory();
}
catch (MappingException e)
{
/*
* Upon encountering a Hibernate generated Exception, we are throwing
* an unchecked RuntimeExcpetion that will cause the user's
* request to fail.
*
*/
System.err.println("Mapping Exception" + e.getMessage());
throw new RuntimeException(e);
}
catch (HibernateException e)
{
/*
* Upon encountering a Hibernate generated Exception, we are throwing
* an unchecked RuntimeExcpetion that will cause the user's request to fail.
*
*/
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
}
/**
* getInstance() returns the instance of the ConnectionFactory singleton.
*
* Example call to retrieve session:
*
* <pre>
* Session session = ConnectionFactory.getInstance().getSession();
* </pre>
*
* @return Instance of the <code>ConnectionFactory</code> singleton.
*/
public static synchronized ConnectionFactory getInstance()
{
/*
* If the instance of the Singleton has not been created, create and
* return.
*/
if (instance == null)
{
instance = new ConnectionFactory();
}
return instance;
}
/**
* getSession() returns a Hibernate <code>Session</code>
*
* @return <code>Session</code> retrieved from Hibernate <Code>SessionFactory</code>
*/
public Session getSession()
{
try
{
/*
* Use the Hibernate Session Factory to return an open session to the caller.
*/
Session s = sessionFactory.openSession();
return s;
}
catch (HibernateException e)
{
/*
* Upon encountering a Hibernate generated Exception, we are throwing
* an unchecked RuntimeExcpetion that will cause the user's request to fail.
*
*/
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
}
}
===== material table in mysql =====
Code:
MATID bigint(20) UNSIGNED auto_increment
MATNUM varchar(120)
MATTYPE varchar(30) NULL
MATNAME varchar(60)
MATPROVIDER varchar(30) NULL
MATUNIT varchar(30) NULL
MATCOST float UNSIGNED NULL
ISDELETE varchar(6) NULL
Any ideas?...