Problems with creating a hello wold hibernate project

124 Views Asked by At

ok I have spent more than 2 days now searching for tutorials and guide on creating a hibernate project, but I cant get things to work,

so I have Two Main questions

  1. Are my Assumptions on how to use the hibernate framework correct?

  2. If yes how do I proceed with each step?

Here are my Assumptions

1a. I complete data modeling step and have a schema ready to be used in application. (Done)

1b. I, then, create object relational mapping, meaning create physical classes to be mapped with each relation. (I have created these classes using NetBeans: New->Entity Classes from database. Will this work or I need to use hibernate somehow to create these?)

1c. I need to have hibernate jars in my application's classpath and some configuration/setting files

1d. I load these configuration/setting files

1e. Start using hibernate

Now all the tutorials I have seen don't elaborate on the details on how to do these steps, I have a lot of tables I cant create all classes manually. Can anyone give a concise solution to this? Something like:

for 1b create object classes using this command: hibernate-object.jar -db -classes

1c to create settings/configuration file use so and so command/plugin

1d this is how you load these files in code

3

There are 3 best solutions below

0
On

First of all, I would try with an easy example to set up the Hibernate framework:

  1. I would choose a simple table, if it were possible, without relationships.
  2. With this simple table, I would follow the tutorial posted by @Igor Sadovnikov.
  3. When your example works I would extend it to a 1:N relationship.

I recommend you to build your own ORM classes and do not delegate this task to a program (Netbeans or similar) because these programs add a lot of useless code. If you have to use a program because you have a lot of tables you must to check the generated ORM classes one by one.

P.S.: Sorry for my English, I'm still learning...

0
On

There are several ways to start using hibernate, and your way (create database tables and relationships first) is one possible way.

My preference would be to create your Java Classes with annotations first, and have Hibernate set up the database tables for you.

A third possibility is to create the object relation mapping XML, and have Hibernate generate the Java classes AND database tables for you (middle-out).

Whichever way you choose, a useful starting point is to create a HibernateUtil class, as follows:

public class HibernateUtil {

  private static final SessionFactory sessionFactory = buildSessionFactory();

  private static SessionFactory buildSessionFactory() {
    try {
        // Create the SessionFactory from hibernate.cfg.xml
        return new Configuration().configure("myproject.cfg.xml").buildSessionFactory();
    } catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
  }

  public static SessionFactory getSessionFactory() {
    return sessionFactory;
  }

  public static void shutdown() {
    // Close caches and connection pools
    getSessionFactory().close();
  }

}

That makes it easy to grab a SessionFactory where needed. This approach is only to get you started and not recommended for production use as it's not very scalable, I'd consider Spring integration.

0
On

FOr step 1B, you need not create it using any supporting tool of NetBeans, I would suggest manually writing these classes, because it will be easy to recall that way when you are debugging the application. You will find a lot of exact tutorials online. Most go like this:

1) download a set of official Hibernate JARs + database driver JAR (this will be custom to the database(s) you are using)

2) Add these libraries to the build path of your project.

3) Start using Hibernate

If you using NetBeans is not of prime importance, I would suggest you to switch to Eclipse as you would hardly find few tutorials (going ahead) that use NetBeans.