-->
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: Mapping files naming convention
PostPosted: Thu Aug 25, 2005 4:34 am 
Newbie

Joined: Tue Aug 16, 2005 8:12 am
Posts: 10
Hi,

I have a class called "A" which I wanna map to table called "xyz". Can I name my .hbm.xml file as "b.hbm.xml"? I tried this but it gives me error.
Any reason for this because in Hibernate http://www.hibernate.org/hib_docs/v3/re ... orial.html
, they say "[b]The naming of mapping files can be arbitrary, however the hbm.xml suffix became convention in the Hibernate developer community"[/b].
Please try it at your end and let me know if this works for you.

Thanks
Medhavi.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 4:38 am 
Senior
Senior

Joined: Thu Aug 04, 2005 4:54 am
Posts: 153
Location: Birmingham, UK
First of all what was the error?

It may (probably) be completely unrelated to your naming.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 4:50 am 
Newbie

Joined: Tue Aug 16, 2005 8:12 am
Posts: 10
The error was
[java] net.sf.hibernate.MappingException: Resource: Medhavi.hbm.xml not found
[java] at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:334)
[java] at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:1013)
[java] at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:969)
[java] at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:897)
[java] at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:883)
[java] at Main.main(Unknown Source)
[java] Exception in thread "main"
[java] Java Result: 1

And my Main.java file is:


import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.HibernateException;
import java.lang.Integer;

public class Main {
public static void main ( String[] args )
throws MappingException,
HibernateException
{

Integer i = new Integer(1);
System.out.println("Cfg. the initial values");
Configuration cfg = new Configuration();
//cfg.addClass( Medhavi.class );
SessionFactory sessions = cfg.configure().buildSessionFactory();
System.out.println("Cfg. ended");
Session session = sessions.openSession();
Transaction tx = null;
Medhavi kw = new Medhavi("k","blue" );
kw.setId(i);
System.out.println("Inserted keyword in database Medhavi");
try {
tx = session.beginTransaction();
session.save( kw );
tx.commit();
System.out.println("TranX committed");
}
catch ( HibernateException he ) {
he.printStackTrace();
if ( tx != null ) tx.rollback();
throw he;
}
finally {
session.close();

sessions.close() ;

System.out.println("Session closed");

}
}
}


Mapping file:Keyword.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<!-- table created by: CREATE TABLE KEYWORDS ( ID IDENTITY, NAME VARCHAR(25) ); -->
<class name="Medhavi"
table="employee2">
<id name="id" type="integer" column="id">
<generator class="increment"/>
</id>
<property name="id1" type="string" column="NAME" />
<property name="id2" type="string" column="NAME2" />
</class>

Thanks,
Medhavi


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 8:55 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
Do you have a file hibernate.cfg.xml containing the following line (among other lines of course)?
Code:
<mapping resource="Keyword.hbm.xml"/>


See the documentation for more information:
http://www.hibernate.org/hib_docs/refer ... tart-intro

Best regards
Sven

_________________
Please don't forget to give credit, if this posting helped to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 9:12 am 
Newbie

Joined: Tue Aug 16, 2005 8:12 am
Posts: 10
Thanks for the replyy.
Yes this mapping is there in hibernate.cfg.xml file.

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:data/medhavi</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">net.sf.hibernate.dialect.HSQLDialect</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="Keyword.hbm.xml"/>
<mapping resource="First.hbm.xml"/>

</session-factory>

</hibernate-configuration>

Thanks,
Medhavi.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 9:20 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
You can name the files anything you want. But the generally accepted standard is to name them the same as your POJOs.

Are your mapping files in the root of your classpath ?

If they are nested alongside the Java classes in the package/directory structure, then you need to specify this as well.

<mapping resource="org/xyz/.../Keyword.hbm.xml" />

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 9:20 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
Quote:
The naming of mapping files can be arbitrary, however the hbm.xml suffix became convention in the Hibernate developer community.


Are you sure that this is true for Hibernate 2 as well? I can only find this quote in the Hibernate 3 documentation.

Best regards
Sven

_________________
Please don't forget to give credit, if this posting helped to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 9:26 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
sven wrote:
Quote:
The naming of mapping files can be arbitrary, however the hbm.xml suffix became convention in the Hibernate developer community.


Are you sure that this is true for Hibernate 2 as well? I can only find this quote in the Hibernate 3 documentation.

Best regards
Sven


I'm pretty sure the only time naming matters(in either version) is if you're configuring the SessionFactory programatically.

Code:
Configuration cfg = new Configuration().addClass(Keyword.class);

The addClass(Class c) method does the following to identify the mapping file.
Code:
String fileName = persistentClass.getName().replace(StringHelper.DOT,'/') + ".hbm.xml";

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 9:30 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
pksiv wrote:
I'm pretty sure the only time naming matters(in either version) is if you're configuring the SessionFactory programatically.


Sounds understandable, I just couldn't find the information in the 2.x documentation.

Best regards
Sven

_________________
Please don't forget to give credit, if this posting helped to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 26, 2005 12:53 am 
Newbie

Joined: Tue Aug 16, 2005 8:12 am
Posts: 10
Hi,

All mapping files are in my classpath and they are not embedded inside a package, still I am facing the same error. I guess may be its not true for 2.x. but then it is really nice to know that its a compulsion to follow the naming convention if we are using the addClass method. Thanks for this info.

Regards,
Medhavi.


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.