Hi,
It is the first time I am using hibernate and I am losing my patience with hibernate beacause I cannot configure it.
I followed a tutorial and I have the following error:
14:58:31,156 INFO Version:15 - Hibernate Annotations 3.2.0.GA
14:58:31,187 INFO Environment:543 - Hibernate 3.3.1.GA
14:58:31,187 INFO Environment:576 - hibernate.properties not found
14:58:31,187 INFO Environment:709 - Bytecode provider name : javassist
14:58:31,203 INFO Environment:627 - using JDK 1.4 java.sql.Timestamp handling
14:58:31,312 INFO Configuration:1460 - configuring from resource: /hibernate.cfg.xml
14:58:31,328 INFO Configuration:1437 - Configuration resource: /hibernate.cfg.xml
14:58:31,609 INFO Configuration:1575 - Configured SessionFactory: null
14:58:31,703 INFO AnnotationBinder:387 - Binding entity from annotated class: de.laliluna.example.Honey
14:58:31,765 INFO EntityBinder:340 - Bind entity de.laliluna.example.Honey on table Honey
14:58:31,968 WARN UserSuppliedConnectionProvider:46 - No connection properties specified - the user must supply JDBC connections
Exception in thread "main" java.lang.ExceptionInInitializerError
at de.laliluna.example.TestExample.createHoney(TestExample.java:90)
at de.laliluna.example.TestExample.main(TestExample.java:28)
Caused by: org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:80)
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:62)
at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:460)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:155)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2101)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1325)
at de.laliluna.hibernate.SessionFactoryUtil.<clinit>(SessionFactoryUtil.java:26)
... 2 moreI will post all my code and I appreciate if you can help-me.
hibernate.cfgCode:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- MySQL connection
<property name="connection.url">jdbc:mysql://localhost:3306/firsthibernate</property>
<property name="connection.username">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.password">r</property>
-->
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- thread is the short name for
org.hibernate.context.ThreadLocalSessionContext
and let Hibernate bind the session automatically to the thread
-->
<property name="current_session_context_class">thread</property>
<!-- this will show us all sql statements -->
<property name="hibernate.show_sql">true</property>
<!-- this will create the database tables for us -->
<property name="hibernate.hbm2ddl.auto">create</property>
<!--<mapping resource="de/laliluna/example/Honey.hbm.xml" />-->
<mapping class="de.laliluna.example.Honey" />
</session-factory>
</hibernate-configuration>
Honey.hbmCode:
<?xml version=“1.0” encoding=“UTF-8”?>
<!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd” >
<hibernate-mapping>
<class name=“de.laliluna.example.Honey” table=“honey”>
<id name=“id” column=“id” >
<generator class=“increment”/>
</id>
<property name=“name” column=“fooname” />
<property name=“taste” column=“bartaste” />
</class>
</hibernate-mapping>
log4j.propertiesCode:
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=debug, stdout
log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=debug
### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug
### log just the SQL
log4j.logger.org.hibernate.SQL=debug
### log JDBC bind parameters ###
log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug
### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=info
### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug
### log cache activity ###
log4j.logger.org.hibernate.cache=info
### log transaction activity
#log4j.logger.org.hibernate.transaction=debug
### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug
### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
SessionFactoryUtil.javaCode:
package de.laliluna.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class SessionFactoryUtil {
private SessionFactoryUtil() {
}
static{
// Annotation and XML
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
// XML only
// sessionFactory = new Configuration().configure().buildSessionFactory();
}
public static SessionFactory getInstance() {
return sessionFactory;
}
public Session openSession() {
return sessionFactory.openSession();
}
public Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
public static void close(){
if (sessionFactory != null)
sessionFactory.close();
sessionFactory = null;
}
}
Honey.javaCode:
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
@Entity
public class Honey {
@Id
@GeneratedValue
private Integer id;
private String name;
private String taste;
public Honey() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTaste() {
return taste;
}
public void setTaste(String taste) {
this.taste = taste;
}
@Override
public String toString() {
return "Honey: "+getId()+" Name: "+getName()+" Taste: "+getTaste();
}
}
TestExample.javaCode:
package de.laliluna.example;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.laliluna.hibernate.SessionFactoryUtil;
public class TestExample {
final static Logger logger = LoggerFactory.getLogger(TestExample.class);
public static void main(String[] args) {
Honey forestHoney = new Honey();
forestHoney.setName("forest honey");
forestHoney.setTaste("very sweet");
Honey countryHoney = new Honey();
countryHoney.setName("country honey");
countryHoney.setTaste("tasty");
createHoney(forestHoney);
createHoney(countryHoney);
// our instances have a primary key now:
logger.debug("{}", forestHoney);
logger.debug("{}", countryHoney);
listHoney();
deleteHoney(countryHoney);
listHoney();
forestHoney.setName("Norther Forest Honey");
updateHoney(forestHoney);
}
private static void listHoney() {
Transaction tx = null;
Session session = SessionFactoryUtil.getInstance().getCurrentSession();
try {
tx = session.beginTransaction();
List honeys = session.createQuery("select h from Honey as h")
.list();
for (Iterator iter = honeys.iterator(); iter.hasNext();) {
Honey element = (Honey) iter.next();
logger.debug("{}", element);
}
tx.commit();
} catch (RuntimeException e) {
if (tx != null && tx.isActive()) {
try {
// Second try catch as the rollback could fail as well
tx.rollback();
} catch (HibernateException e1) {
logger.debug("Error rolling back transaction");
}
// throw again the first exception
throw e;
}
}
}
private static void deleteHoney(Honey honey) {
Transaction tx = null;
Session session = SessionFactoryUtil.getInstance().getCurrentSession();
try {
tx = session.beginTransaction();
session.delete(honey);
tx.commit();
} catch (RuntimeException e) {
if (tx != null && tx.isActive()) {
try {
// Second try catch as the rollback could fail as well
tx.rollback();
} catch (HibernateException e1) {
logger.debug("Error rolling back transaction");
}
// throw again the first exception
throw e;
}
}
}
private static void createHoney(Honey honey) {
Transaction tx = null;
Session session = SessionFactoryUtil.getInstance().getCurrentSession();
try {
tx = session.beginTransaction();
session.save(honey);
tx.commit();
} catch (RuntimeException e) {
if (tx != null && tx.isActive()) {
try {
// Second try catch as the rollback could fail as well
tx.rollback();
} catch (HibernateException e1) {
logger.debug("Error rolling back transaction");
}
// throw again the first exception
throw e;
}
}
}
private static void updateHoney(Honey honey) {
Transaction tx = null;
Session session = SessionFactoryUtil.getInstance().getCurrentSession();
try {
tx = session.beginTransaction();
session.update(honey);
tx.commit();
} catch (RuntimeException e) {
if (tx != null && tx.isActive()) {
try {
// Second try catch as the rollback could fail as well
tx.rollback();
} catch (HibernateException e1) {
logger.debug("Error rolling back transaction");
}
// throw again the first exception
throw e;
}
}
}
}
And finally my library