-->
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.  [ 8 posts ] 
Author Message
 Post subject: Doubt in using JNDI with Hibernate.
PostPosted: Tue Jul 08, 2008 2:03 pm 
Newbie

Joined: Thu Feb 14, 2008 1:47 am
Posts: 9
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.2.5

Name and version of the database you are using: MySQL 5.0.45

Please explain me in 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 !!! :(


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 08, 2008 5:25 pm 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
Two things and config files you have to deploy:

1) in the directory "deploy" of your JBoss server you define the datasource by a file named "xxxxxx-ds.xml". It looks something like
Code:
<?xml version="1.0" encoding="UTF-8"?>

<datasources>
  <local-tx-datasource>
     <jndi-name>someName</jndi-name>
     <connection-url>jdbc:mysql://localhost/schemaName</connection-url>

     <driver-class>com.mysql.jdbc.Driver</driver-class>
     <user-name>usrname</user-name>
     <password>morethansecret</password>
(....)

-- read the JBoss docs about datasources!

2) if you have an EJB/JPA application, then create a directory named "META-INF" in the root classpath. There, you deploy the hibernate configuration file named "persistence.xml". It looks something like this:
Code:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
             version="1.0">

   <persistence-unit name="yourName">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:/someName</jta-data-source>
      <mapping-file>META-INF/orm_xyz.xml</mapping-file>
      <class>org.myboss.base.domain.Check</class>
    (...)
    <properties>
         <property name="hibernate.default_schema" value="schemaName"/>
   (... other Hibernate properties)


-- read the Hibernate documentation.

Then: - sit back: and you will see: it works!

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 08, 2008 10:58 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
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

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

<hibernate-configuration>

    <!-- a SessionFactory instance listed as /jndi/name -->

    <session-factory
        name="java:hibernate/SessionFactory">

        <!-- properties -->
        <property name="connection.datasource">java:/comp/env/jdbc/MyDB</property>
        <property name="dialect">org.hibernate.dialect.SCJASQLDialect</property>
        <property name="show_sql">true</property>
        <property name="transaction.factory_class">
            org.hibernate.transaction.JTATransactionFactory
        </property>
        <property name="jta.UserTransaction">java:comp/UserTransaction</property>

        <!-- mapping files -->


        <!-- cache settings -->


    </session-factory>

</hibernate-configuration>

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 09, 2008 2:09 pm 
Newbie

Joined: Thu Feb 14, 2008 1:47 am
Posts: 9
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 09, 2008 3:35 pm 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
siddharth_2279 wrote:
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.


You should apply us with more infos:
- Stacktrace

- what kind of application do you have? EJB? Simple Web-App?

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 11, 2008 6:47 am 
Newbie

Joined: Thu Feb 14, 2008 1:47 am
Posts: 9
Hi All,
The code is as follows :

code:


try {
System.out.println("Hi....");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
System.out.println("After Hi....");
session = sessionFactory.openSession();

System.out.println("Inserting Record");

Transaction tx = session.beginTransaction();

Contact contact = new Contact();
// contact.setId(7);
contact.setFirstName("Neel");
contact.setLastName("Bhargava");
contact.setEmail("neel_bhargava@gmail.com");

Address address = new Address();
address.setAddress1("Flat No. 59");
address.setAddress2("Rajouri Apartments");
address.setCity("New Delhi");
address.setZipcode(110064);

contact.setHomeAddress(address);

session.save(contact);

tx.commit();

System.out.println("Done");

} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
session.flush();
session.close();
}



hibernate.cfg.xml

code:



<hibernate-configuration>

<session-factory>

<property name="hibernate.connection.datasource">java:MySqlDS</property>
<property name="hibernate.jndi.url"></property>
<property name="hibernate.jndi.class"></property>
<property name="hibernate.connection.username"></property>
<property name="hibernate.connection.password"></property>

<property name="show_sql">true</property>
<property name="format_sql">true</property>

<!-- Show and print nice SQL on stdout. -->
<!-- <property name="dialect">org.hibernate.dialect.MySQLDialect</property> -->
<property name="hibernate.hbm2ddl.auto">create</property>

<!-- List of XML Mapping files -->
<mapping resource="contact.hbm.xml"/>
<!-- <mapping resource="address.hbm.xml"/> -->

</session-factory>

</hibernate-configuration>




Exception which occurs is :

code:


Hi....
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Could not find datasource
Exception in thread "main" java.lang.NullPointerException
at com.example.jndi.SaveExample.main(SaveExample.java:50)



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

mysql-ds.xml contents

code:


<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/jbossdb</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>root</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<!-- should only be used on drivers after 3.22.1 with "ping" support
<valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
-->
<!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->
<!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->

<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>




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

quote:
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.


Top
 Profile  
 
 Post subject: Configure JNDI in hibernate
PostPosted: Sat Jul 12, 2008 10:52 pm 
Newbie

Joined: Wed Jul 02, 2008 10:39 pm
Posts: 6
Location: Hyderabad
Hi Siddharath,


See if the Below works for u.
Me too using JNDI with mysql in hibernate.
It is working.

http://technosatish.blogspot.com/2008/07/how-to-set-jndi-in-hibernate.html

_________________
Regards,
Racha Satish Kumar,
Visit Me Here!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 20, 2008 2:57 am 
Newbie

Joined: Sun Jul 20, 2008 2:28 am
Posts: 1
Siddharth,

I was having some issues setting up my datasource as well. I am using oracle but having the same issue as you. I finally got it to work with the following hibernate config
Code:
<hibernate-configuration>
<session-factory name="java:hibernate/SessionFactory">
<property name="hibernate.connection.datasource">java:OraDS</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> -->
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>


I am not sure at this point but I think the name in the session-factory tag is required for this thing to work correctly.

I hope this helps.


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