-->
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.  [ 5 posts ] 
Author Message
 Post subject: Help, going mad!
PostPosted: Sat Apr 23, 2005 11:43 pm 
Newbie

Joined: Sat Apr 23, 2005 10:50 pm
Posts: 17
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);




Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 24, 2005 12:02 am 
Beginner
Beginner

Joined: Wed Apr 20, 2005 9:30 am
Posts: 39
Looks to me like you're missing the all important

Configuration config = new Configuration();
config.configure();
SessionFactory sessionFactory = config.buildSessionFactory();

Without that, Hibernate won't even be up and running to handle everything else... would also be worth your time to throw log4j into that test class so you can get some useful debug from Hibernate as it's pretty verbose in debug mode.

Cheers,

J --


Top
 Profile  
 
 Post subject: I am actually using the configure in this class....
PostPosted: Sun Apr 24, 2005 1:05 am 
Newbie

Joined: Sat Apr 23, 2005 10:50 pm
Posts: 17
Session session = HibernateSession.currentSession();

I am actually using the configuration with this class

Code:
import java.util.*;
import java.io.*;

import net.sf.hibernate.*;
import net.sf.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();
  }
}


Top
 Profile  
 
 Post subject: sorry, one more thing!
PostPosted: Sun Apr 24, 2005 1:09 am 
Newbie

Joined: Sat Apr 23, 2005 10:50 pm
Posts: 17
I have also used other examples, using the configuration as you
showed but it still don't work!
Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 24, 2005 3:52 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Using Innodb Tables and Transactions?


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