Search This Blog

Sunday, 12 February 2012


In the previous post we saw how to create additional SQL using the <database-object> element. The queries were specified in the mapping file and Hibernate picked it from there and executed them as a part of the hbm2ddl operation.However Hibernate also allows us to programmatically control the generation of scripts.
For this level of control we need to extend the AbstractAuxiliaryDatabaseObject class. The class includes methods that return the scripts for create and drop operations.I removed the scripts from the previous example in the following modified hbm:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "" >
<hibernate-mapping package="com.sql.index">
    <class name="User">
        <id name="id" type="long">
            <generator class="native" />
        <property name="firstName">
            <column name="FIRST_NAME" />

        <definition class="com.sql.auxilarydatabaseobject.UserHistoryDDL" />
        <dialect-scope name="org.hibernate.dialect.MySQLDialect"></dialect-scope>
The UserHistoryDDL class is now responsible for generating the SQL:
package com.sql.auxilarydatabaseobject;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.Mapping;
import org.hibernate.mapping.AbstractAuxiliaryDatabaseObject;

public class UserHistoryDDL extends AbstractAuxiliaryDatabaseObject {

    private static final Logger logger = Logger.getLogger(UserHistoryDDL.class);
    public String sqlCreateString(Dialect dialect, Mapping mapping,
            String defaultCatalog, String defaultSchema)
            throws HibernateException {"sqlCreateString: The dialect is " + dialect);
        //operations specific to a dialect can be performed here
        return "create table User_History ("
                + "id bigint not null auto_increment,"
                + "First_Name varchar(255)," + "Last_Name varchar(255),"
                + "primary key (id)" + ")";

    public String sqlDropString(Dialect dialect, String defaultCatalog,
            String defaultSchema) {
        return "drop table if exists User_History";

In case we need to support multiple dialects, we simply add additional dialect-scope elements. The sql queries can be seen in the application logs:
2312 [main] INFO  com.sql.auxilarydatabaseobject.UserHistoryDDL  - sqlCreateStri
ng: The dialect is org.hibernate.dialect.MySQLDialect
2312 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaExport  - Running hbm2ddl sch
ema export
2312 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport  - import file not fou
nd: /import.sql
2312 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaExport  - exporting generated
 schema to database
2312 [main] DEBUG org.hibernate.connection.DriverManagerConnectionProvider  - to
tal checked-out connections: 0
2312 [main] DEBUG org.hibernate.connection.DriverManagerConnectionProvider  - us
ing pooled JDBC connection, pool size: 0
2328 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport  - 
    drop table if exists User_History
2359 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport  - 
    drop table if exists User
2390 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport  - 
    create table User (
        id bigint not null auto_increment,
        FIRST_NAME varchar(255),
        primary key (id)
2390 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport  - 
    create table User_History (
        id bigint not null auto_increment,
       First_Name varchar(255),
       Last_Name varchar(255),
       primary key (id)
2406 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaExport  - schema export compl

No comments:

Post a Comment