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.xmlCode:
<?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.xmlCode:
<?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 codeCode:
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...