3 Replies Latest reply on Jul 25, 2006 1:50 PM by laxman

    How to map DSN name with actual database ? Please help.

    laxman

      I am trying to connect to database using hibernate in a seam project. I have put the following code in hibernate.cfg.xml

      com.mysql.jdbc.Driver
      jdbc:mysql://localhost/testdb
      root

      10
      true
      org.hibernate.dialect.MySQLDialect
      update
      <!-- Mapping files -->


      I have a test program which uses the DSN to insert a record. Code goes like this:

      SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
      session =sessionFactory.openSession();
      Contact contact = new Contact();
      contact.setId(3);
      contact.setFirstName("Deepak");
      contact.setLastName("Kumar");
      contact.setEmail("deepak_38@yahoo.com");
      session.save(contact);

      when I run this program , database gets connected but it throws a strange exception ( ehCache exception ) in the first line of test program.

      I tried various possible options. But no luck. I have been trying this from past 1 week. Please help me. Please let me know if anyone needs more information.

      regards,
      Laxman

        • 1. Re: How to map DSN name with actual database ? Please help.

           

          when I run this program , database gets connected but it throws a strange exception ( ehCache exception ) in the first line of test program.

          Post the relevant code and your exception. I suspect you may need ehcache on your classpath - but without the above, I can only guess.

          • 2. Re: How to map DSN name with actual database ? Please help.
            laxman



            Justin,

            thanks alot for your attention. Here is the code.

            hibernate.cfg.xml
            --------------------

            <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

            <hibernate-configuration>
            <session-factory>
            org.gjt.mm.mysql.Driver
            jdbc:mysql://localhost/testdb
            root

            10
            true
            org.hibernate.dialect.MySQLDialect
            update
            <!-- Mapping files -->


            </session-factory>
            </hibernate-configuration>


            POJO
            ------

            package org.jboss.seam.example.booking;

            /**
            *
            * Java Class to map to the datbase Contact Table
            */
            public class Contact {
            private String firstName;
            private String lastName;
            private String email;
            private long id;

            /**
            * @return Email
            */
            public String getEmail() {
            return email;
            }

            /**
            * @return First Name
            */
            public String getFirstName() {
            return firstName;
            }

            /**
            * @return Last name
            */
            public String getLastName() {
            return lastName;
            }

            /**
            * @param string Sets the Email
            */
            public void setEmail(String string) {
            email = string;
            }

            /**
            * @param string Sets the First Name
            */
            public void setFirstName(String string) {
            firstName = string;
            }

            /**
            * @param string sets the Last Name
            */
            public void setLastName(String string) {
            lastName = string;
            }

            /**
            * @return ID Returns ID
            */
            public long getId() {
            return id;
            }

            /**
            * @param l Sets the ID
            */
            public void setId(long l) {
            id = l;
            }

            }


            user.hbm.xml
            ----------------

            <?xml version="1.0"?>
            <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

            <hibernate-mapping>















            </hibernate-mapping>

            Test Client to insert record in database
            --------------------------------------------


            package org.jboss.seam.example.booking;

            import org.hibernate.Session;
            import org.hibernate.SessionFactory;
            import org.hibernate.cfg.Configuration;

            /**
            * @author Deepak Kumar
            *
            * http://www.roseindia.net
            * Hibernate example to inset data into Contact table
            */
            public class FirstExample {




            public static void main(String[] args) {


            Session session = null;

            try{
            // This step will read hibernate.cfg.xml and prepare hibernate for use
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            session =sessionFactory.openSession();
            //Create new instance of Contact and set values in it by reading them from form object
            System.out.println("jjjjjjjjjjj jjj j j j j j Inserting Record");
            Contact contact = new Contact();
            contact.setId(3);
            contact.setFirstName("Deepak");
            contact.setLastName("Kumar");
            contact.setEmail("deepak_38@yahoo.com");
            session.save(contact);
            System.out.println("Done");
            session.flush();
            session.close();
            }catch(Exception e){
            System.out.println("Exception vachindi:"+e.getMessage());
            }finally{
            // Actual contact insertion will happen at this step



            }

            }
            }


            Exception that I get
            -----------------------

            INFO 25-07 22:38:48,375 (Log4JLogger.java:info:94) -Hibernate 3.2 cr2
            INFO 25-07 22:38:48,390 (Log4JLogger.java:info:94) -hibernate.properties not found
            INFO 25-07 22:38:48,390 (Log4JLogger.java:info:94) -Bytecode provider name : cglib
            INFO 25-07 22:38:48,390 (Log4JLogger.java:info:94) -using JDK 1.4 java.sql.Timestamp handling
            INFO 25-07 22:38:48,468 (Log4JLogger.java:info:94) -configuring from resource: /hibernate.cfg.xml
            INFO 25-07 22:38:48,468 (Log4JLogger.java:info:94) -Configuration resource: /hibernate.cfg.xml
            INFO 25-07 22:38:48,593 (Log4JLogger.java:info:94) -Reading mappings from resource: user.hbm.xml
            INFO 25-07 22:38:48,703 (Log4JLogger.java:info:94) -Mapping class: org.jboss.seam.example.booking.Contact -> CONTACT
            INFO 25-07 22:38:48,734 (Log4JLogger.java:info:94) -Configured SessionFactory: null
            INFO 25-07 22:38:48,796 (Log4JLogger.java:info:94) -Using Hibernate built-in connection pool (not for production use!)
            INFO 25-07 22:38:48,796 (Log4JLogger.java:info:94) -Hibernate connection pool size: 10
            INFO 25-07 22:38:48,796 (Log4JLogger.java:info:94) -autocommit mode: false
            INFO 25-07 22:38:48,812 (Log4JLogger.java:info:94) -using driver: org.gjt.mm.mysql.Driver at URL: jdbc:mysql://localhost/testdb
            INFO 25-07 22:38:48,812 (Log4JLogger.java:info:94) -connection properties: {user=root, password=****}
            INFO 25-07 22:38:49,015 (Log4JLogger.java:info:94) -RDBMS: MySQL, version: 4.0.26-nt
            INFO 25-07 22:38:49,015 (Log4JLogger.java:info:94) -JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.0.2-beta ( $Date: 2005-11-17 16:14:47 +0100 (Thu, 17 Nov 2005) $, $Revision: 4560 $ )
            INFO 25-07 22:38:49,046 (Log4JLogger.java:info:94) -Using dialect: org.hibernate.dialect.MySQLDialect
            INFO 25-07 22:38:49,046 (Log4JLogger.java:info:94) -Using default transaction strategy (direct JDBC transactions)
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -Automatic flush during beforeCompletion(): disabled
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -Automatic session close at end of transaction: disabled
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -JDBC batch size: 15
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -JDBC batch updates for versioned data: disabled
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -Scrollable result sets: enabled
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -JDBC3 getGeneratedKeys(): enabled
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -Connection release mode: auto
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -Maximum outer join fetch depth: 2
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -Default batch fetch size: 1
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -Generate SQL with comments: disabled
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -Order SQL updates by primary key: disabled
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -Using ASTQueryTranslatorFactory
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -Query language substitutions: {}
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -Second-level cache: enabled
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -Query cache: disabled
            INFO 25-07 22:38:49,062 (Log4JLogger.java:info:94) -Cache provider: org.hibernate.cache.EhCacheProvider
            Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
            at java.lang.Class.getDeclaredConstructors0(Native Method)
            at java.lang.Class.privateGetDeclaredConstructors(Class.java:2328)
            at java.lang.Class.getConstructor0(Class.java:2640)
            at java.lang.Class.newInstance0(Class.java:321)
            at java.lang.Class.newInstance(Class.java:303)
            at org.hibernate.cfg.SettingsFactory.createCacheProvider(SettingsFactory.java:346)
            at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:220)
            at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1928)
            at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1211)
            at org.jboss.seam.example.booking.FirstExample.main(FirstExample.java:25)



            Then I have downloaded ehcache1-1.jar and put it in classpath.

            Then I get different error which is related purely to ehcache.jar. Here is the error.

            Exception in thread "main" java.lang.IllegalAccessError: tried to access method net.sf.ehcache.CacheManager.()V from class org.hibernate.cache.EhCacheProvider
            at org.hibernate.cache.EhCacheProvider.start(EhCacheProvider.java:124)
            at org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:180)
            at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1213)
            at org.jboss.seam.example.booking.FirstExample.main(FirstExample.java:25)


            Actually the following line in test client is throwing ehcache exception for some reason.

            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

            The errors I have copied above are just related to that exception cache not found. But the actual problem i different. If everything would have worked fine, it would not have thrown ehcache exception at all.

            Please ask me if you need further information.

            I am using Jboss eclipse IDE jboss-4.0.4.GA

            please help

            regards,
            Laxman

            • 3. Re: How to map DSN name with actual database ? Please help.
              laxman

              Hurry. problem is solved. I have downloaded ehcache1-2.jar and replaced it in the classpath.

              my test client works successfully,

              thanks alot justin,

              Laxman