These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Trying to get hibernate to run for the first
PostPosted: Fri Feb 11, 2005 12:44 pm 
Newbie

Joined: Fri Feb 11, 2005 11:50 am
Posts: 3
Hi,

I 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.0

hibernate.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://10.150.128.33:1433;SelectMethod=cursor;DatabaseName=hibernate</property>
        <property name="hibernate.connection.username">hibernate</property>
        <property name="hibernate.connection.password">hibernate</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.Java
Code:

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 2000

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject: Re: Trying to get hibernate to run for the first
PostPosted: Fri Feb 11, 2005 12:58 pm 
Newbie

Joined: Fri Feb 11, 2005 11:50 am
Posts: 3
sellc wrote:
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.0

hibernate.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.Java
Code:

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 2000

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 11, 2005 1:31 pm 
Beginner
Beginner

Joined: Tue Jan 11, 2005 5:50 am
Posts: 43
Location: Zurich (Suisse)
Hello

Code:
<hibernate-mapping>
  <class name="com.hoffmanonline.hibernate.Cate" table="CAT">


Should read:
Code:
<hibernate-mapping>
  <class name="com.hoffmanonline.hibernate.Cat" table="CAT">


Cate is misspelled.

Regards
Tarik


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 14, 2005 6:55 pm 
Newbie

Joined: Fri Feb 11, 2005 11:50 am
Posts: 3
Okay so I fixed the spelling error, but I still get this error.


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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 14, 2005 7:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Please read the DTD, it is "resource" not "resources"


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.