-->
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.  [ 6 posts ] 
Author Message
 Post subject: Problem with Hibernate Dialect
PostPosted: Fri May 06, 2011 6:08 am 
Newbie

Joined: Fri May 06, 2011 5:34 am
Posts: 1
Being a novice, I started to try out a simple example using hibernate. I am trying to insert Id, firstname, lastname and emailId to a dB table in MySQL.

Following are the files I have...
1. hibernate.cfg.xml
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>
<session-factory>
        <property name="show_sql">true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetutorial</property>    
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password"></property>
      <property name="hibernate.connection.pool_size">10</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
     
      <!-- Mapping files -->
      <mapping resource="contact.hbm.xml"/>       
</session-factory>
</hibernate-configuration>

2. contact.hbm.xml
Code:
<?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>
  <class name="hello.Contact" table="contact">
   <id name="id" type="long" column="ID" >
   <generator class="assigned"/>
  </id>

  <property name="firstName">
     <column name="FIRSTNAME" />
  </property>
  <property name="lastName">
    <column name="LASTNAME"/>
  </property>
  <property name="email">
    <column name="EMAIL"/>
  </property>
</class>
</hibernate-mapping>

3. Persistent class file:
Code:
public class Contact {
  private String firstName;
  private String lastName;
  private String email;
  private long id;

  public String getEmail() {
    return email;
  }

  public String getFirstName() {
    return firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setEmail(String string) {
    email = string;
  }

  public void setFirstName(String string) {
    firstName = string;
  }
 
  public void setLastName(String string) {
    lastName = string;
  }
 
  public long getId() {
    return id;
  }

  public void setId(long l) {
    id = l;
  }

}

4. Application code
Code:
public class FirstExample {   
   public static void main(String[] args) {      
      Session session = null;      
      try {               
         Configuration cfg = new Configuration()                              
                                                            .addResource("hibernate.cfg.xml");                  
         SessionFactory sessionFactory = cfg.buildSessionFactory();               
         session = sessionFactory.openSession();
         // Create new instance of Contact and set
         // values in it by reading them from form object
         System.out.println("Inserting Record");
         Contact contact = new Contact();
         contact.setId(1);
         contact.setFirstName("Dany");
         contact.setLastName("Richards");
         contact.setEmail("dannnny@yahoo.com");
         session.save(contact);
         System.out.println("Done");
      } catch (MappingException me) {
         System.out.println("Mapping Exception occured..." + me.getMessage());
      } catch (Exception e) {
         System.out.println("Exception in the try..." + e.getMessage());
         e.printStackTrace();
      } finally {
         // Actual contact insertion will happen at this step
         session.flush();
         session.close();
      }
   }
}

When i try to compile the Application code i am getting the following error...
Code:
WARNING: No connection properties specified - the user must supply JDBC connections
Exception in the try...Hibernate Dialect must be explicitly set
org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
   at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57)
   at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39)
   at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:378)
   at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:110)
   at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1881)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1174)
   at hello.FirstExample.main(FirstExample.java:33)

Kindly help me out of this plz...
Thanks in advance...


Top
 Profile  
 
 Post subject: Re: Problem with Hibernate Dialect
PostPosted: Sun May 08, 2011 8:53 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
it looks like it didn't find your hibernate.cfg.xml, make sure it's packed as resource with your other classes and it's on your classpath

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Problem with Hibernate Dialect
PostPosted: Thu May 19, 2011 3:22 am 
Newbie

Joined: Thu May 19, 2011 2:36 am
Posts: 3
i have same error as this...
i put my hibernate.cfg.xml in Default Package..


Top
 Profile  
 
 Post subject: Re: Problem with Hibernate Dialect
PostPosted: Thu May 19, 2011 4:07 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
i have same error as this...
i put my hibernate.cfg.xml in Default Package..

make sure that when you build your application this file is also included in the created jar

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Problem with Hibernate Dialect
PostPosted: Thu May 19, 2011 6:30 am 
Newbie

Joined: Fri Dec 24, 2010 2:08 pm
Posts: 6
Hi,

Config file should be in classpath to of the project to get it referred.

If you are using Eclipse IDE for project development, u can verify classpath entry in
.classpath file for the hibernate.cfg.xml file location.
For instance the entry would be
<classpathentry kind="src" path="resources"/>

Where as my config file is in .\resource folder.


Top
 Profile  
 
 Post subject: Re: Problem with Hibernate Dialect
PostPosted: Thu May 19, 2011 9:00 pm 
Newbie

Joined: Thu May 19, 2011 2:36 am
Posts: 3
s.grinovero wrote:
Quote:
i have same error as this...
i put my hibernate.cfg.xml in Default Package..

make sure that when you build your application this file is also included in the created jar


how to do that??
i'm sorry..i'm new in java dev.. :(

santoshjh wrote:
Hi,

Config file should be in classpath to of the project to get it referred.

If you are using Eclipse IDE for project development, u can verify classpath entry in
.classpath file for the hibernate.cfg.xml file location.
For instance the entry would be
<classpathentry kind="src" path="resources"/>

Where as my config file is in .\resource folder.


i'm using net bean IDE 7.0


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