-->
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.  [ 10 posts ] 
Author Message
 Post subject: Error Reading Resource
PostPosted: Thu May 27, 2004 5:27 am 
Newbie

Joined: Thu May 27, 2004 5:16 am
Posts: 4
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?...


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 27, 2004 5:43 am 
Regular
Regular

Joined: Wed May 12, 2004 3:03 am
Posts: 51
Location: France
Hi,

You must add a default constructor.

Tell me if it's ok then...

Charles


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 27, 2004 9:17 pm 
Newbie

Joined: Thu May 27, 2004 5:16 am
Posts: 4
I've added the constructor....
but it doesn't work.

I still have the same exception.

I guess the problem is in the mapping file,
maybe I have wrong date type or someting like that,
but I'm not sure.


Top
 Profile  
 
 Post subject: Is it in the classpath?
PostPosted: Thu Jun 03, 2004 10:37 am 
Regular
Regular

Joined: Fri Sep 12, 2003 12:40 pm
Posts: 65
Make sure the factory/material/MaterialDTO.hbm.xml file is in the classpath

e.g.
classes/factory/material/MaterialDTO.class
classes/factory/material/MaterialDTO.hbm.xml

You probably just didn't copy this file after compilation.

_________________
- Brian


Top
 Profile  
 
 Post subject: P.S.
PostPosted: Thu Jun 03, 2004 10:38 am 
Regular
Regular

Joined: Fri Sep 12, 2003 12:40 pm
Posts: 65
FYI. since MaterialDTO is not extending anything and no other constructors are specified, it has an implied default constructor.

_________________
- Brian


Top
 Profile  
 
 Post subject: Re: P.S.
PostPosted: Thu Jun 03, 2004 11:14 am 
Regular
Regular

Joined: Wed May 12, 2004 3:03 am
Posts: 51
Location: France
bbonner wrote:
FYI. since MaterialDTO is not extending anything and no other constructors are specified, it has an implied default constructor.

I know that there's a default constructor, but Hibernate requires to have it written...

Quote:
4.1.2. Implement a default constructor
Cat has an implicit default (no-argument) constructor. All persistent classes must have a default constructor (which may be non-public) so Hibernate can instantiate them using Constructor.newInstance().


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 03, 2004 11:17 am 
Regular
Regular

Joined: Wed May 12, 2004 3:03 am
Posts: 51
Location: France
Oh....
sorry, I made a mistake with this :

Quote:
14.2. Proxies for Lazy Initialization
Note that the proxied class must implement a default constructor with at least package visibility.


Top
 Profile  
 
 Post subject: Exactly
PostPosted: Thu Jun 03, 2004 11:22 am 
Regular
Regular

Joined: Fri Sep 12, 2003 12:40 pm
Posts: 65
In the example on 4.1.2 just cited, Cat has an implicit constructor, and thus doesn't have to specified explicitly. I don't see a Cat() constructor anywhere in the example.

_________________
- Brian


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 04, 2004 3:21 am 
Regular
Regular

Joined: Wed May 12, 2004 3:03 am
Posts: 51
Location: France
That's what I wanted to say when I appologize...


Top
 Profile  
 
 Post subject: No problem.
PostPosted: Fri Jun 04, 2004 10:06 am 
Regular
Regular

Joined: Fri Sep 12, 2003 12:40 pm
Posts: 65
No problem really. I just wanted to make sure the forum had accurate info. Thanks for clarifying.

_________________
- Brian


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.