Hi,
I'm having trouble getting the Cat demo to work. Can someone telling what I'm doing wrong?
Hibernate version: 2.1.8 with WebSphere Rational 6.0hibernate.cfg.xml file 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-configuration>
<session-factory>
<!-- Telling Hibernate what jdbc driver that you are using -->
<property name="hibernate.connection.driver_class">com.microsoft.jdbc.sqlserver</property>
<property name="hibernate.connection.url">jdbc:microsoft:sqlserver://xx.xx.xxx.xxx;SelectMethod=cursor;DatabaseName=hibernate</property>
<property name="hibernate.connection.username">hibernate</property>
<property name="hibernate.connection.password"></property>
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>
<property name="show_sql">true</property>
<!-- Here is where you map your map files -->
<mapping resources="com/hoffmanonline/hibernate/Cat.hbm.xml" />
</session-factory>
</hibernate-configuration>
Mapping documents: Cat.hbm.xml Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.hoffmanonline.hibernate.Cate" table="CAT">
<!-- A 32 hex Character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="id" type="string" unsaved-value="null">
<column name="CAT_ID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<!-- Setting up all the other columns -->
<property name="name">
<column name="NAME" length="16" not-null="true"/>
</property>
<property name="sex"/>
<property name="weight"/>
</class>
</hibernate-mapping>
Cat.JavaCode:
package com.hoffmanonline.hibernate;
public class Cat {
private String id;
private String name;
private char sex;
private float weight;
/**
* @return Returns the id.
*/
public String getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(String id) {
this.id = id;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Returns the sex.
*/
public char getSex() {
return sex;
}
/**
* @param sex The sex to set.
*/
public void setSex(char sex) {
this.sex = sex;
}
/**
* @return Returns the weight.
*/
public float getWeight() {
return weight;
}
/**
* @param weight The weight to set.
*/
public void setWeight(float weight) {
this.weight = weight;
}
}
HibernateUtil.java[code]
ackage com.hoffmanonline.hibernate;
import org.apache.commons.logging.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory
System.out.println("Start the Build Session Factory.");
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex) {
log.error("Intial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
testCat.java[code]
package com.hoffmanonline.test;
import com.hoffmanonline.hibernate.*;
import net.sf.hibernate.*;
public class testCat {
public static void main (String[] args) throws HibernateException{
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Cat princess = new Cat();
princess.setName("Princess");
princess.setSex('F');
princess.setWeight(7.4f);
session.save(princess);
tx.commit();
HibernateUtil.closeSession();
}
}
[/code]
Code between sessionFactory.openSession() and session.close():Full stack trace of any exception that occurs:Start the Build Session Factory.
log4j:WARN No appenders could be found for logger (net.sf.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.hoffmanonline.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:20)
at com.hoffmanonline.test.testCat.main(testCat.java:8)
Caused by: net.sf.hibernate.HibernateException: problem parsing configuration/hibernate.cfg.xml
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:972)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:911)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:897)
at com.hoffmanonline.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:16)
... 1 more
Caused by: net.sf.hibernate.MappingException: invalid configuration
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:968)
... 4 more
Caused by: org.xml.sax.SAXParseException: Attribute "resources" must be declared for element type "mapping".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.addDTDDefaultAttrsAndValidate(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.emptyElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:339)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:967)
... 4 more
Name and version of the database you are using: MSSQL 2000The generated SQL (show_sql=true):Debug level Hibernate log excerpt: