It has been a long time since I have used Hibernate and something strange is happening. I am getting the following error for no apparent reason:
Initial SessionFactory creation failed.org.hibernate.MappingNotFoundException: resource: scott.addressbook.AddressForm.hbm.xml not found
Any suggestions, thoughts, comments??? Thanks.
Hibernate version:
3.2.1
Name and version of the database I am using:
MySQL 5.0
Framework and version:
Struts 1.2.7
Name and version of IDE:
Netbeans 5.5
Mapping document:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="scott.addressbook.AddressForm" table="addresses">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="lastName" type="string" column="LAST_NAME"/>
<property name="firstName" type="string" column="FIRST_NAME"/>
<property name="street" type="string" column="STREET"/>
<property name="state" type="string" column="STATE"/>
<property name="city" type="string" column="CITY"/>
<property name="zip" type="string" column="ZIP"/>
<property name="country" type="string" column="COUNTRY"/>
<property name="phone" type="string" column="PHONE"/>
<property name="email" type="string" column="EMAIL"/>
<property name="cell" type="string" column="CELL"/>
<property name="birthdate" type="calendar" column="BIRTH_DATE"/>
</class>
</hibernate-mapping>
Hibernate.cfg.xml:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/contacts</property>
<property name="connection.username">wolverine</property>
<property name="connection.password">xmen</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!--property name="hbm2ddl.auto">create</property-->
<mapping resource="scott.addressbook.AddressForm.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Code between sessionFactory.openSession() and session.close():
package scott.addressbook.utilities;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Web Form Code:
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head><title>Address Book</title></head>
<body>
<center><h3>Address Book</h3></center>
<html:errors/>
<table align=center>
<html:form action="insertAddress.do">
<tr><td>First Name:</td> <td><html:text property="firstName"/><br></td></tr>
<tr><td>Last Name:</td> <td><html:text property="lastName"/><br></td></tr>
<tr><td>Street</td> <td><html:text property="street"/><br></td></tr>
<tr><td>City</td> <td><html:text property="city"/><br></td></tr>
<tr><td>State</td> <td><html:text property="state"/><br></td></tr>
<tr><td>Zip</td> <td><html:text property="zip"/><br></td></tr>
<tr><td>Country</td> <td><html:text property="country"/><br></td></tr>
<tr><td>Phone:</td> <td><html:text property="phone"/><br></td></tr>
<tr><td>Cell:</td> <td><html:text property="cell"/></br></td></tr>
<tr><td>Email:</td> <td><html:text property="email"/></br></td></tr>
<tr><td>Birth Date:</td> <td><html:text property="birthDate"/><br></td></tr>
<tr><td colspan="2" align="center"><html:submit value="Add Contact"/></td></tr>
</html:form>
</table>
</body>
</html>
Bean Code:
package scott.addressbook.test;
import org.hibernate.Session;
import scott.addressbook.utilities.HibernateUtil;
import scott.addressbook.AddressForm;
public class AddressTest {
public static void main(String[] args) {
AddressTest addressTest = new AddressTest();
addressTest.createAndStoreAddress("Blow", "Joe","1289 Bean St.", "Chicago", "IL", "USA",
"38690","123-456-78910","nobody@nowhere.com","123-789-6986"
,"01/22/1970");
HibernateUtil.getSessionFactory().close();
}
private void createAndStoreAddress(String lastName, String firstName, String street,
String city, String state, String country, String zip,
String cell, String email, String phone,
String birthDate) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
AddressForm addressForm = new AddressForm();
addressForm.setLastName(lastName);
addressForm.setFirstName(firstName);
addressForm.setStreet(street);
addressForm.setCity(city);
addressForm.setState(state);
addressForm.setState(country);
addressForm.setZip(zip);
addressForm.setCell(cell);
addressForm.setEmail(email);
addressForm.setPhone(phone);
addressForm.setBirthDate(birthDate);
session.save(addressForm);
session.getTransaction().commit();
}
Full stack trace of exception:
init:
deps-module-jar:
deps-ear-jar:
deps-jar:
compile-single:
run-main:
12:06:04,664 INFO Environment:500 - Hibernate 3.2.1
12:06:04,684 INFO Environment:533 - hibernate.properties not found
12:06:04,694 INFO Environment:667 - Bytecode provider name : cglib
12:06:04,704 INFO Environment:584 - using JDK 1.4 java.sql.Timestamp handling
12:06:04,834 INFO Configuration:1423 - configuring from resource: /hibernate.cfg.xml
12:06:04,834 INFO Configuration:1400 - Configuration resource: /hibernate.cfg.xml
12:06:04,985 DEBUG DTDEntityResolver:38 - trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd]
12:06:04,995 DEBUG DTDEntityResolver:40 - recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
12:06:04,995 DEBUG DTDEntityResolver:50 - located [http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd] in classpath
12:06:05,095 DEBUG Configuration:1384 - connection.driver_class=com.mysql.jdbc.Driver
12:06:05,095 DEBUG Configuration:1384 - connection.url=jdbc:mysql://localhost:3306/contacts
12:06:05,095 DEBUG Configuration:1384 - connection.username=wolverine
12:06:05,095 DEBUG Configuration:1384 - connection.password=xmen
12:06:05,095 DEBUG Configuration:1384 - connection.pool_size=1
12:06:05,095 DEBUG Configuration:1384 - dialect=org.hibernate.dialect.MySQLDialect
12:06:05,095 DEBUG Configuration:1384 - current_session_context_class=thread
12:06:05,095 DEBUG Configuration:1384 - cache.provider_class=org.hibernate.cache.NoCacheProvider
12:06:05,135 DEBUG Configuration:1384 - show_sql=true
12:06:05,145 DEBUG Configuration:1583 - null<-org.dom4j.tree.DefaultAttribute@1754ad2 [Attribute: name resource value "scott.addressbook.AddressForm.hbm.xml"]
12:06:05,145 INFO Configuration:553 - Reading mappings from resource : scott.addressbook.AddressForm.hbm.xml
Initial SessionFactory creation failed.org.hibernate.MappingNotFoundException: resource: scott.addressbook.AddressForm.hbm.xml not found Exception in thread "main" java.lang.ExceptionInInitializerError
at scott.addressbook.utilities.HibernateUtil.<clinit>(HibernateUtil.java:24)
at scott.addressbook.test.AddressTest.createAndStoreAddress(AddressTest.java:25)
at scott.addressbook.test.AddressTest.main(AddressTest.java:13)
Caused by: org.hibernate.MappingNotFoundException: resource: scott.addressbook.AddressForm.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:563)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1584)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1552)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1531)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1505)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
at scott.addressbook.utilities.HibernateUtil.<clinit>(HibernateUtil.java:20)
... 2 more
Scott
|