Thursday, February 8, 2007

Hibernate tutorial 2 (Intermediate)

Table of Content

  1. Introduction
    1. Employee class
    2. Task class
    3. HibernateHelper class
    4. CreateEmployee class
    5. hibernate.properties file
  2. Running the example
    1. Download the example
    2. Install Java 1.5 and Ant
    3. Database configuration
    4. Run the Ant build script

Introduction

This Hibernate example maps two Java Objects (Employee.java and Task) to tables in a relational database. This example also creates some instances of these Objects and saves them into a database.

The following diagram list the four classes which make up this example.


Employee class

The Employee class is a simple POJO which has been setup to be able to be used as an Hibernate Object.

package com.francoisnadeau.test;
import java.util.*;
import javax.persistence.*;
/**
* A single Employee.
*
* @author Francois Nadeau
* @link http://www.FrancoisNadeau.com
*/
@Entity                                   1
public class Employee {
    /** The Employee's unique Id. */
    private Long theId;
    /** The Employee's name. */
    private String theName;
    /** The date that the Employee was hired. */
    private Date theHireDate;
    /** The employee's address */
    private String theAddress;
    /** List of task owned by Employee */
    ListallTasks;

    @Id(generate = GeneratorType.AUTO)    2
    public Long getId() { return theId; }
    public void setId(Long aId) { theId = aId; }

    public String getName() { return theName; }
    public void setName(String aName) { theName = aName; }

    public Date getHireDate() { return theHireDate;    }
    public void setHireDate(Date aHireDate) { theHireDate = aHireDate; }

    public String getAddress() { return theAddress;    }
    public void setAddress(String aAddress) { theAddress = aAddress; }
    
    @OneToMany (mappedBy = "employee")    3
    @OrderBy ("name")                     4
    public List getAllTasks() { return allTasks;    }
    public void setAllTasks(List someTasks) { allTasks = someTasks; }

    @Transient                            5
    public List getCompletedTasks() {
        List allCompletedTasks = new LinkedList();
        for(Task aTask : getAllTasks()) {
            if(aTask.getCompletionDate() != null) {
                allCompletedTasks.add(aTask);
            }
        }
        return allCompletedTasks;
    }

    @Transient
    public List getToDoList() {
        List theToDoList = new LinkedList();
        for(Task aTask : getAllTasks()) {
            if(aTask.getCompletionDate() == null) {
                theToDoList.add(aTask);
            }
        }
        return theToDoList;
    }
}
  1. @Entity
    Hibernate uses Sun's EJB3 Annotation standard to map persistent Objects. Because of this, all Hibernate Objects are treated as Entity Beans.
    Note: Although Hibernate uses the EJB3 standard in this manner, the Objects do not need to be deployed in an EJB container to be used. An interesting benefit of this is that no modifications would be required for this class to be used inside a J2EE compliant server as an Entity Bean .
  2. @Id(generate = GeneratorType.AUTO)
    This annotation defines the primary key for this Object. The following key generators can be used for generating IDs: AUTO, TABLE, IDENTITY, SEQUENCE, or NONE.
  3. @OneToMany (mappedBy = "employee")
    This annotation is used to define a one-to-many relationship with an other Object. The "mappedBy" field is the foreign key of the Object.
  4. @OrderBy ("name")
    This annotation is used in combination with @OneToMany to define that the collection is to be ordered. In this example the collection will be ordered by the "name" field in the Task table.
  5. @Transient
    By default all functions inside an EJB are persistent, and therefore are mapped to a field in the database. However, in this case both the getCompletedTasks() and getToDoList() are not persistent fields.

Task class

The Task class is a simple POJO which has been setup to be able to be used as an Hibernate Object.

package com.francoisnadeau.test;

import java.util.Date;
import javax.persistence.*;
/**
* A single Task wich can be performed by an employee.
*
* @author Francois Nadeau
* @link http://www.FrancoisNadeau.com
*/
@Entity
public class Task {
    /** The Task's unique Id. */
    private Long theId;
    /** The Task name. */
    private String theName;
    /** The date that the Taks was created. */
    private Date theEntryDate;
    /** The date that the Taks was assigned to the Employee. */
    private Date theAssignedDate;
    /** The date that the Taks was completed by the Employee. */
    private Date theCompletionDate;
    /** The Employee assigned to this task */
    private Employee theEmployee;

    @Id(generate = GeneratorType.AUTO)
    public Long getId() { return theId; }
    public void setId(Long aId) { theId = aId; }

    public String getName() { return theName; }
    public void setName(String aName) { theName = aName; }

    public Date getEntryDate() { return theEntryDate; }
    public void setEntryDate(Date aEntryDate) { theEntryDate = aEntryDate; }

    public Date getAssignedDate() { return theAssignedDate; }
    public void setAssignedDate(Date aAssignedDate) { theAssignedDate = aAssignedDate; }

    public Date getCompletionDate() { return theCompletionDate; }
    public void setCompletionDate(Date aCompletionDate) {
        theCompletionDate = aCompletionDate;
    }

    @ManyToOne                          1
    public Employee getEmployee() { return theEmployee; }
    public void setEmployee(Employee aEmployee) { theEmployee= aEmployee; }
}
  1. @ManyToOne
    This annotation is used to define a many-to-one relationship with an other Object.

HibernateHelper class

The HibernateHelper class is used to access the Hibernate Sessions. This class is copied from and often referred to in the Hibernate documentation.

package com.francoisnadeau.test;

import org.hibernate.*;
import org.hibernate.cfg.AnnotationConfiguration;
/**
* This class is ued to access the Hibernate session.
*
* @author Francois Nadeau
* @link http://www.FrancoisNadeau.com
*/
public class HibernateHelper {
    private static SessionFactory theSessionFactory;
    
    static {
        try {
            // Create the SessionFactory
            theSessionFactory = new AnnotationConfiguration()
                    .addPackage("com.francoisnadeau.test")
                    .addAnnotatedClass(Employee.class)
                    .addAnnotatedClass(Task.class)
                    .setProperty("hibernate.hbm2ddl.auto", "create")     1
                    .buildSessionFactory();
        } catch (Throwable ex) {
            theSessionFactory = null;
            System.out.print("Error Initializing Hibenrate:\n");
            ex.printStackTrace();
        }
    }

    public static Session getSession() throws HibernateException
{        
        return theSessionFactory.openSession();
    }
}
  1. .setProperty("hibernate.hbm2ddl.auto", "create")
    The "hibernate.hbm2ddl.auto" will reload the database tables every time the session factory is built. This is an useful method to test the data element structure. Programmers should remove this before deploying the program.

CreateEmployee class

The CreateEmployee class is a simple program which creates and saves 5 new Employees, 15 completed tasks, 10 uncompleted yet assigned tasks, and two unassigned tasks. The program then outputs the information that is located in the database. This is included in the package to demonstrate how the Employee and Task classes are stored and retrieved from the database using Hibernate.

package com.francoisnadeau.test;

import java.util.*;
import org.hibernate.*;
import org.hibernate.criterion.Expression;
/**
* Test class to show how to access the Hibernate Objects.
*
* @author Francois Nadeau
* @link http://www.FrancoisNadeau.com
*/
public class CreateEmployee {
    /**
     * Create a numnber of tasks which
     * are not assigned to any Employees.
     *
     * @param aNumberOfTasks number of tasks to be created.
     */
    private static void createUnassignedTasks(int aNumberOfTasks) {
        for(int i = 0 ; i < aNumberOfTasks ; i++) {
            Session theSession = HibernateHelper.getSession();
            Task theTask = new Task();
            theTask.setName("Unassigned Task, number " + i);
            theTask.setEntryDate(new Date());

            theSession.save(theTask);
            theSession.flush();
            theSession.close();
        }
    }

    /**
     * Create some Uncompleted Tasks which
     * have been assigned to an Employee.
     *
     * @param anEmployee the employee assigned to the task
     * @param aNumberOfTasks number of tasks to be created
     */
    private static void createUncompletedTaskFor(Employee anEmployee, int aNumberOfTasks) {
        for(int i = 0 ; i < aNumberOfTasks ; i++) {
            Session theSession = HibernateHelper.getSession();
            Task theTask = new Task();
            theTask.setName("Assigned, Uncompleted Task " + i + ", for Employee "
                    + anEmployee.getName() );
            theTask.setEntryDate(new Date());
            theTask.setAssignedDate(new Date());
            theTask.setEmployee(anEmployee);

            theSession.save(theTask);
            theSession.flush();
            theSession.close();
        }
    }

    /**
     * Create some completed tasks to an
     * Employee.
     *
     * @param anEmployee the employee with the assigned tasks
     * @param aNumberOfTasks the number of tasks to be created
     */
    private static void createCompletedTaskFor(Employee anEmployee, int aNumberOfTasks) {
        for(int i = 0 ; i < aNumberOfTasks ; i++) {
            Session theSession = HibernateHelper.getSession();
            Task theTask = new Task();
            theTask.setName("Assigned, Completed Task " + i + ", for Employee "
                    + anEmployee.getName() );
            theTask.setEntryDate(new Date());
            theTask.setAssignedDate(new Date());
            theTask.setCompletionDate(new Date());
            theTask.setEmployee(anEmployee);
            
            theSession.save(theTask);
            theSession.flush();
            theSession.close();
        }
    }
    
    /**
     * Create and save some Employees and tasks.
     *
     * @param args
     */
    public static void main(String[] args) {
        createUnassignedTasks(2);

        for (int i = 1; i < 6; i++) {
            Employee theEmployee = new Employee();
            Session theSession = HibernateHelper.getSession();

            theEmployee.setName("Employee " + i);

            theSession.save(theEmployee);
            theSession.flush();
            theSession.close();

            createCompletedTaskFor(theEmployee, 6 - i );
            createUncompletedTaskFor(theEmployee, i - 1);
        }
        
        //
        // Display the information which has been added in the database.
        //
        Session theSession = HibernateHelper.getSession();
        Criteria theCriteria = theSession.createCriteria(Employee.class);
        List allEmployees = theCriteria.list();
        
        System.out.println("-------------------------------------------------------------");
        System.out.println("| Employee Name\t| CompletedTask\t| Uncompleted Tasks\t|");
        System.out.println("-------------------------------------------------------------");
        for(Employee anEmployee : allEmployees) {
            System.out.println("| " + anEmployee.getName() + "\t| "
                    + anEmployee.getCompletedTasks().size() + "\t\t| "
                    + anEmployee.getToDoList().size() + "\t\t\t| ");
        }
        System.out.println("-------------------------------------------------------------\n");
        
        System.out.println("-------------------------------------------------------------");
        System.out.println("| Un-assigned tasks \t\t\t\t\t|");
        System.out.println("-------------------------------------------------------------");

        theCriteria = theSession.createCriteria(Task.class);
        theCriteria.add(Expression.isNull("employee"));
        List theUnassignedTasks = theCriteria.list();
        
        for(Task aTask : theUnassignedTasks) {
            System.out.println("| " + aTask.getName() + "\t\t\t\t\t|");
        }
        System.out.println("-------------------------------------------------------------");
    }
}

hibernate.properties file

The last component for this example is the Hibernate configuration file, which is required for the communicate with the database. Information such as the database location, user-name, and password are entered in this file.

######################
### Query Language ###
######################
## define query language constants / function names
hibernate.query.substitutions true 1, false 0, yes 'Y', no 'N'
## package imports
#hibernate.query.imports com.kingdomfounder.data

#################
### Platforms ###
#################
## MySQL
hibernate.dialect org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql://localhost/TESTHIBERNATE
hibernate.connection.username aUserName
hibernate.connection.password aPassword

#################################
### Hibernate Connection Pool ###
#################################

hibernate.connection.pool_size 2
hibernate.statement_cache.size 100

Running the example

To run this example follow the following steps:

Download the example

Download and un-pack the example code and binary libraries:

Install Java 1.5 and Ant

Download and install Java 1.5 and apache Ant:
  • Download and install Sun's JDK 1.5 if it is not already installed
  • Download and install the Apache's Ant build tool if it is not already installed

Database Configuration

This example uses MySQL databse since it is Open Source, and freely available.

Once the server is install complete the following commands to load the database schema into the database.

franck@linux:~/code> cd scripts/
franck@linux:~/code/scripts> mysql -u root -p
Enter password:                               The password is empty by default
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 368 to server version: 4.0.21

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> source dbCreate.sql                     Execute the sql commands in file
Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql>

Run the Ant build script

Open a command prompt and run the following command in the directory which was created when the source code was unpacked.

    ant run.CreateEmployee

The following is the output that will be generated by this command.
franck@linux:~/code> ant run.CreateEmployee
Buildfile: build.xml

init:
    [mkdir] Created dir: /home/franck/work/95_university/comp601/Project/code/build
    [mkdir] Created dir: /home/franck/work/95_university/comp601/Project/code/build/classes

resources:
    [copy] Copying 6 files to /home/franck/work/95_university/comp601/Project/code/build/classes

compile:
    [javac] Compiling 4 source files to /home/franck/work/95_university/comp601/Project/code/build/classes
    [javac] Note:
/home/franck/work/95_university/comp601/Project/code/src/com/francoisnadeau/test/CreateEmployee.java uses unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.

jar:
    [jar] Building jar:
/home/franck/work/95_university/comp601/Project/code/build/hibernateTest-1.0.jar

run.CreateEmployee:
    [java] Nov 25, 2005 3:34:37 PM org.hibernate.cfg.Environment <clinit>
    [java] INFO: Hibernate 3.1 rc1
    [java] Nov 25, 2005 3:34:37 PM org.hibernate.cfg.Environment <clinit>
    [java] INFO: loaded properties from resource hibernate.properties:
{hibernate.connection.driver_class=com.mysql.jdbc.Driver, hibernate.cglib.use_reflection_optimizer=true,
hibernate.dialect=org.hibernate.dialect.MySQLDialect, hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N',
hibernate.connection.username=aUserName, hibernate.connection.url=jdbc:mysql://localhost/TESTHIBERNATE, hibernate.connection.password=****, hibernate.statement_cache.size=100, hibernate.connection.pool_size=2}
    [java] Nov 25, 2005 3:34:37 PM org.hibernate.cfg.Environment <clinit>
    [java] INFO: using CGLIB reflection optimizer
    [java] Nov 25, 2005 3:34:37 PM org.hibernate.cfg.Environment <clinit>
    [java] INFO: using JDK 1.4 java.sql.Timestamp handling
    [java] Nov 25, 2005 3:34:37 PM org.hibernate.cfg.AnnotationConfiguration addPackage
    [java] INFO: Mapping package com.francoisnadeau.test
    [java] Nov 25, 2005 3:34:37 PM org.hibernate.cfg.AnnotationBinder bindPackage
    [java] WARNING: Package not found or wo package-info.java: com.francoisnadeau.test
    [java] Nov 25, 2005 3:34:37 PM org.hibernate.cfg.Configuration secondPassCompile
    [java] INFO: processing extends queue
    [java] Nov 25, 2005 3:34:37 PM org.hibernate.cfg.Configuration secondPassCompile
    [java] INFO: processing collection mappings
    [java] Nov 25, 2005 3:34:37 PM org.hibernate.cfg.annotations.CollectionBinder
bindOneToManySecondPass
    [java] INFO: Mapping collection: com.francoisnadeau.test.Employee.allTasks -> Task
    [java] Nov 25, 2005 3:34:37 PM org.hibernate.cfg.Configuration secondPassCompile
    [java] INFO: processing association property references
    [java] Nov 25, 2005 3:34:37 PM org.hibernate.cfg.Configuration secondPassCompile
    [java] INFO: processing foreign key constraints
    [java] Nov 25, 2005 3:34:37 PM org.hibernate.connection.DriverManagerConnectionProvider configure
    [java] INFO: Using Hibernate built-in connection pool (not for production use!)
    [java] Nov 25, 2005 3:34:37 PM org.hibernate.connection.DriverManagerConnectionProvider configure
    [java] INFO: Hibernate connection pool size: 2
    [java] Nov 25, 2005 3:34:37 PM org.hibernate.connection.DriverManagerConnectionProvider configure
    [java] INFO: autocommit mode: false
    [java] Nov 25, 2005 3:34:37 PM org.hibernate.connection.DriverManagerConnectionProvider configure
    [java] INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/TESTHIBERNATE
    [java] Nov 25, 2005 3:34:37 PM org.hibernate.connection.DriverManagerConnectionProvider configure
    [java] INFO: connection properties: {user=aUserName, password=****}
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: RDBMS: MySQL, version: 4.0.21
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.8 ( $Date: 2005/04/14 20:36:13
$, $Revision: 1.27.4.64 $ )    [java] Nov 25, 2005 3:34:38 PM org.hibernate.dialect.Dialect <init>
    [java] INFO: Using dialect: org.hibernate.dialect.MySQLDialect
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
    [java] INFO: Using default transaction strategy (direct JDBC transactions)
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.transaction.TransactionManagerLookupFactory
getTransactionManagerLookup
    [java] INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: Automatic flush during beforeCompletion(): disabled
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: Automatic session close at end of transaction: disabled
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: JDBC batch size: 15
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: JDBC batch updates for versioned data: disabled
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: Scrollable result sets: enabled
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: JDBC3 getGeneratedKeys(): enabled
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: Connection release mode: null
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: Maximum outer join fetch depth: 2
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: Default batch fetch size: 1
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: Generate SQL with comments: disabled
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: Order SQL updates by primary key: disabled
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
    [java] INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
    [java] INFO: Using ASTQueryTranslatorFactory
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: Query language substitutions: {no='N', true=1, yes='Y', false=0}
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: Second-level cache: enabled
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: Query cache: disabled
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory createCacheProvider
    [java] INFO: Cache provider: org.hibernate.cache.EhCacheProvider
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: Optimize cache for minimal puts: disabled
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: Structured second-level cache entries: disabled
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: Statistics: disabled
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: Deleted entity synthetic identifier rollback: disabled
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    [java] INFO: Default entity-mode: POJO
    [java] Nov 25, 2005 3:34:38 PM org.hibernate.impl.SessionFactoryImpl <init>
    [java] INFO: building session factory
    [java] Nov 25, 2005 3:34:38 PM net.sf.ehcache.config.Configurator configure
    [java] WARNING: No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath:
jar:file:/home/franck/work/95_university/comp601/Project/code/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
    [java] Nov 25, 2005 3:34:39 PM org.hibernate.impl.SessionFactoryObjectFactory addInstance
    [java] INFO: Not binding factory to JNDI, no JNDI name configured
    [java] Nov 25, 2005 3:34:39 PM org.hibernate.cfg.Configuration secondPassCompile
    [java] INFO: processing extends queue
    [java] Nov 25, 2005 3:34:39 PM org.hibernate.cfg.Configuration secondPassCompile
    [java] INFO: processing collection mappings
    [java] Nov 25, 2005 3:34:39 PM org.hibernate.cfg.Configuration secondPassCompile
    [java] INFO: processing association property references
    [java] Nov 25, 2005 3:34:39 PM org.hibernate.cfg.Configuration secondPassCompile
    [java] INFO: processing foreign key constraints
    [java] Nov 25, 2005 3:34:39 PM org.hibernate.cfg.Configuration secondPassCompile
    [java] INFO: processing extends queue
    [java] Nov 25, 2005 3:34:39 PM org.hibernate.cfg.Configuration secondPassCompile
    [java] INFO: processing collection mappings
    [java] Nov 25, 2005 3:34:39 PM org.hibernate.cfg.Configuration secondPassCompile
    [java] INFO: processing association property references
    [java] Nov 25, 2005 3:34:39 PM org.hibernate.cfg.Configuration secondPassCompile
    [java] INFO: processing foreign key constraints
    [java] Nov 25, 2005 3:34:39 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
    [java] INFO: Running hbm2ddl schema export
    [java] Nov 25, 2005 3:34:39 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
    [java] INFO: exporting generated schema to database
    [java] Nov 25, 2005 3:34:39 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
    [java] INFO: schema export complete
    [java] Nov 25, 2005 3:34:39 PM org.hibernate.impl.SessionFactoryImpl checkNamedQueries
    [java] INFO: Checking 0 named queries
    [java] -----------------------------------------------------
    [java] | Employee Name | CompletedTask | Uncompleted Tasks |
    [java] -----------------------------------------------------
    [java] | Employee 1    | 5             | 0                 |
    [java] | Employee 2    | 4             | 1                 |
    [java] | Employee 3    | 3             | 2                 |
    [java] | Employee 4    | 2             | 3                 |
    [java] | Employee 5    | 1             | 4                 |
    [java] -----------------------------------------------------

    [java] -----------------------------------------------------
    [java] | Un-assigned tasks                                 |
    [java] -----------------------------------------------------
    [java] | Unassigned Task, number 0                         |
    [java] | Unassigned Task, number 1                         |
    [java] -----------------------------------------------------

BUILD SUCCESSFUL
Total time: 5 seconds
Once this program has been executed, you should be able to use the mysql client to select the new Employees which have been created by this program.
franck@linux:~/> mysql -u aUserName -p TESTHIBERNATE
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 366 to server version: 4.0.21

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> select * from Employee;
+----+----------+---------+------------+
| id | hireDate | address | name       |
+----+----------+---------+------------+
|  1 | NULL     | NULL    | Employee 1 |
|  2 | NULL     | NULL    | Employee 2 |
|  3 | NULL     | NULL    | Employee 3 |
|  4 | NULL     | NULL    | Employee 4 |
|  5 | NULL     | NULL    | Employee 5 |
+----+----------+---------+------------+
5 rows in set (0.00 sec)

mysql> select count(*) from Task;
+----------+
| count(*) |
+----------+
|       27 |
+----------+
1 row in set (0.00 sec)

mysql>

No comments: