Hi, I have been trying the whole day getting hibernate working and it does not work.
Everything runs and no specific errors are shown when the application runs, but nothing shows in my MySql table when I persist my objects.
I am basically using "wrox professional hibernate" books tutorial
with hibernate version 2.1.
Below is my code, however if you have a simple tutorial from scratch including class path settings and everything else then I will be happy
to try that one instead.
Also note that I have set all jar files in the classpath for safety, but maybe
that is the problem.
Thanks!
java version=JDK1.5
hibernate folder=C:\hibernate-2.1
classpath settings:
.;C:\Program Files\Java\jdk1.5.0\lib\tools.jar;
C:\mysql-connector-java-3.0.16-ga\mysql-connector-java-3.0.16-ga\mysql-connector-java-3.0.16-ga-bin.jar;
C:\hibernate-2;
C:\hibernate-2.1\lib\cglib2.jar;
C:\hibernate-2.1\lib\commons-logging.jar;
C:\hibernate-2.1\lib\commons-collections.jar;
C:\hibernate-2.1\lib\dom4j.jar;
C:\hibernate-2.1\lib\ehcache.jar;
C:\hibernate-2.1\lib\jdbc2_0-stdext.jar;
C:\hibernate-2.1\lib\jta.jar;
C:\hibernate-2.1\lib\odmg.jar;
C:\hibernate-2.1\lib\xalan.jar;
C:\hibernate-2.1\lib\xerces.jar;
C:\hibernate-2.1\lib\xml-apis.jar;
C:\hibernate-2.1\hibernate2.jar;
the below ones two, I have tried with and without the below ones
C:\hibernate-2.1\lib\commons-dbcp.jar;
C:\hibernate-2.1\lib\commons-pool.jar;
C:\hibernate-2.1\lib\jaas.jar;
C:\hibernate-2.1\lib\oscache.jar;
C:\hibernate-2.1\lib\commons-lang.jar;
C:\hibernate-2.1\lib\concurrent.jar;
C:\hibernate-2.1\lib\proxool.jar;
C:\hibernate-2.1\lib\c3p0.jar;
C:\hibernate-2.1\lib\connector.jar;
C:\hibernate-2.1\lib\jcs.jar;
C:\hibernate-2.1\lib\jgroups.jar;
C:\hibernate-2.1\lib\optional.jar
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-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/products</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="user">root</property>
<property name="password">secret</property>
<mapping resource="book.hbm.xml"/>
</session-factory>
</hibernate-configuration>
hibernate.properties
Code:
hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql://localhost/products
hibernate.connection.username root
hibernate.connection.password secret
hibernate.show_sql true
book.hbm.xml
and
book.hbm.xml~ (Don't really know what that is!)
Code:
<?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="Book" table="books">
<id name="id"
type="int"
unsaved-value="0">
<column name="ID"
sql-type="int"
not-null="true"/>
<generator class="hilo"/>
</id>
<property name="title"/>
<property name="author"/>
<property name="isbn"
not-null="true"/>
<property name="pages"
type="integer"
column="pagecount" />
<property name="copyright"/>
<property name="cost">
<column name="cost"
sql-type="NUMERIC(12,2)"/>
</property>
</class>
</hibernate-mapping>
Java class Book
Code:
import java.io.*;
import java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class Book{
private int id;
private String title;
private String author;
private String isbn;
private int pages;
private int copyright;
private float cost;
public Book() {
}
public void setId(int i) {
id = i;
}
public int getId() {
return id;
}
public void setTitle(String s) {
title = s;
}
public String getTitle() {
return title;
}
public void setAuthor(String s) {
author = s;
}
public String getAuthor() {
return author;
}
public void setIsbn(String s) {
isbn = s;
}
public String getIsbn() {
return isbn;
}
public void setPages(int i) {
pages = i;
}
public int getPages() {
return pages;
}
public void setCopyright(int i) {
copyright = i;
}
public int getCopyright() {
return copyright;
}
public void setCost(float f) {
cost = f;
}
public float getCost() {
return cost;
}
}
Java class BookTest
Code:
import java.io.*;
import java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class BookTest {
public static void main(String [] args) {
try {
Session session = HibernateSession.currentSession();
Book book = new Book();
book.setTitle("Mastering Hibernate");
book.setIsbn("00-394849030-9");
book.setAuthor("Hibernate Guys");
book.setPages(300);
book.setCost(29.99f);
session.save(book);
session.flush();
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
data base sql
Code:
create database products;
create table books (
ID int not null primary key,
title text,
author text,
isbn text not null,
pagecount int,
copyright int,
cost numeric(12,2));
create table hibernate_unique_key (
next_hi int
);
insert into hibernate_unique_key values (1);