File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes it is giving fatal exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "it is giving fatal exception" Watch "it is giving fatal exception" New topic
Author

it is giving fatal exception

umesh rawat
Ranch Hand

Joined: Mar 03, 2007
Posts: 41

Note- this example is from professional hibernate wrox publication chapter 10

whenever i am doing a program with two tables having a relationship .
Eclipse is showing fatal exception .


when i am running this program
it is giving fatal excepiton .
follwoing are the files





group.java


import java.util.*;

public class Group {
private int id;
private String name;
private List stories;

public Group(){
}

public Group(String name) {
this.name = name;
}

public void setId(int i) {
id = i;
}

public int getId() {
return id;
}

public void setName(String n) {
name = n;
}

public String getName() {
return name;
}

public void setStories(List l) {
stories = l;
}

public List getStories() {
return stories;
}
}
===================================
story.java

import java.util.*;

public class Story {
private int id;
private String info;

public Story(){
}

public Story(String info) {
this.info = info;
}

public void setId(int i) {
id = i;
}

public int getId() {
return id;
}

public void setInfo(String n) {
info = n;
}

public String getInfo() {
return info;
}
}
====================================
HibernateSession.java


import org.hibernate.*;
import org.hibernate.cfg.*;


public class HibernateSession {
private static final SessionFactory sessionFactory;

static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException e) {
throw new RuntimeException("SessionFactory Error - " + e.getMessage(), e);
}
}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();

if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}

return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session. set(null);
if (s != null)
s.close();
}
}
============================
hibernet.cfg.xml


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

<hibernate-configuration>

<session-factory>
<property name="connection.driver">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/test</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>

<mapping resource="Group.hbm.xml"/>

</session-factory>

</hibernate-configuration>============================
GroupTest.java

import java.io.*;
import java.util.*;


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


public class GroupTest {

public static void main(String [] args) {

try {
Session session = HibernateSession.currentSession();

Group sp = new Group("accouting");

ArrayList list = new ArrayList();
list.add(new Story("A Story"));
list.add(new Story("And yet another story"));
sp.setStories(list);

Transaction transaction = null;

try {
transaction = session.beginTransaction();

session.save(sp);

transaction.rollback();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
throw e;
}
} finally {
session.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

=======================================



group.xml

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

<hibernate-mapping>

<class name="Group"
table="grouptable">
<id name="id" unsaved-value="0">
<generator class="native"/>
</id>

<list name="stories" cascade="all">
<key column="parent_id"/>
<index column="idx"/>
<one-to-many class="Story"/>
</list>
<property name="name" type="string"/>
</class>
<class name="Story"
table="story">
<id name="id" unsaved-value="0">
<generator class="native"/>
</id>
<property name="info"/>
</class>


</hibernate-mapping>
=======================================

Umesh rawat <br />Rmw -2019

http://100javaquestions.blogspot.com
umesh rawat
Ranch Hand

Joined: Mar 03, 2007
Posts: 41
This is the console report
-------------------------------------------








log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
java.lang.ExceptionInInitializerError
at GroupTest.main(GroupTest.java:16)
Caused by: java.lang.RuntimeException: SessionFactory Error - Dialect class not found: net.sf.hibernate.dialect.MySQLDialect
at HibernateSession.<clinit>(HibernateSession.java:13)
... 1 more
Caused by: org.hibernate.HibernateException: Dialect class not found: net.sf.hibernate.dialect.MySQLDialect
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:81)
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:42)
at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:409)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:119)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2006)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1289)
at HibernateSession.<clinit>(HibernateSession.java:11)
... 1 more
Exception in thread "main"
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Dialect class not found: net.sf.hibernate.dialect.MySQLDialect

This is a Hibernate 2 class, I'm guessing you are using Hibernate 3? There was a fairly substantial refactoring job between the two releases. You'll need to change the dialect class appropriately (see the Hibernatedocumentation). If you are learning Hibernate I also recommend you get a more up to date source to learn from.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
umesh rawat
Ranch Hand

Joined: Mar 03, 2007
Posts: 41
it is giging same fatal exception
while i have chaged hibernate2.jar


other programs are working propely ,
only diff is that i did not create a utill class for sessionFactory.
This is code of workign program



// this is working properly




package roseindia.tutorial.hibernate;

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

public class FirstExample {
public static void main(String[] args) {
Session session = null;

Transaction tx = 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("Inserting Record");
Contact contact = new Contact();
contact.setId(5);
contact.setFirstName("umesh5");
contact.setLastName("rawat5");
contact.setEmail("5umeshrawat30@gmail.com");
tx= session.beginTransaction();

session.save(contact);
//session.
System.out.println("Done");
contact.setFirstName("umesh5Changed");
session.update(contact);
tx.commit();
//session.commit();
}catch(Exception e){

System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
//session.flush();
//session.close();

}

}
}

================
contact.hbm

<?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="roseindia.tutorial.hibernate.Contact" table="CONTACT" mutable="false" >
<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>
=========











hibernate.cfg.xml

<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">1</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
<mapping resource="insurance.hbm.xml"/>
<mapping resource="book.hbm.xml"/>

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






Contact .java




package roseindia.tutorial.hibernate;

/**
* @author Deepak Kumar
*
* http://www.roseindia.net
* 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 email) {
this.email = email;
}

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

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

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

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

}
==




Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


it is giging same fatal exception
while i have chaged hibernate2.jar

That class is part of Hibernate 2 as far as I remember. Did you look in the Jar file to see if it is?


other programs are working propely ,
only diff is that i did not create a utill class for sessionFactory.


Well the error is because you are using a Hibernate 2 dialect which is not on your classpath, nothing to do with the class you access Hibernate through.

By the way, the roseindia tutorials are not very good. I'd recommend going through the Hibernate documentation itself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: it is giving fatal exception