Meaningless Drivel is fun!*
The moose likes Object Relational Mapping and the fly likes Doubt in using JNDI with Hibernate. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Doubt in using JNDI with Hibernate. " Watch "Doubt in using JNDI with Hibernate. " New topic
Author

Doubt in using JNDI with Hibernate.

Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 266
Please explain me how to use JNDI as a data source provider if I use JBoss as an application server with Hibernate application. Please explain with a hibernate.cfg.xml file example and also what all do we have to do in JBoss application server. I tried with a simple example but it says that data source not found.. Please Help me !!!
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

A similar post was answered at hibernate.org:

JNDI and Hibernate Session Question

Well, it's a great idea to bind a Hibernate Session to you JNDI (Java Naming and Directory Interface) server. This way, rather than having to maintain the Session yourself, the JNDI server will, and that resource can then be accessed by any resource that has access and the rights to do a JNDI lookup.

Here you can see it in a sample hibernate.cfg.xml file.

This was loosely stolen from Chapter 3 of the Hibernate documentation. Look it up for more details:

Free Hibernate Documentation for People to Use to Learn Things About Hibernate


[ July 08, 2008: Message edited by: Cameron Wallace McKenzie ]
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 266
Hi,
I tried mapping with hibernate.cfg.xml like this only , but it said that datasource not found. What could be the cause of this error. I had a file by the name of MySQL-ds.xml in JBoss\server\default\deploy. Please do help me..
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 266
Hi All,
The code is as follows :



hibernate.cfg.xml



Exception which occurs is :



Its gives exception in session.flush(); statement.

mysql-ds.xml contents



mysql-ds.xml is in deploy directory of JBoss. and JBoss shows


09:09:43,661 INFO [ConnectionFactoryBindingService] Bound ConnectionManager 'jboss.jca:service=DataSourceBinding,name=MySqlDS' to JNDI name 'java:MySqlDS'


Please help. !!! Any help will be greatly appreciated.
Thanks in advance.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10102
    
165

Could not find datasource
Exception in thread "main" java.lang.NullPointerException
at com.example.jndi.SaveExample.main(SaveExample.java:50)


Are you doing this lookup from a standalone java client? By default, the datasources in JBoss are bound to java: namespace and are not visible outside the server JVM, to remote clients. See this for more details.


[My Blog] [JavaRanch Journal]
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 266
Yeah I am doing this lookup from a standalone Java Client program.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

When running stand-along code that access J2EE resource, you need to ensure that you are running from a J2EE client container, otherwise, the various enterprise resources will not be available to you.

-Cameron McKenzie
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 266
Hi,
How can I do this. Please do explain me. I am trying out a simple standalone application using Eclipse. Thanks in advance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in using JNDI with Hibernate.