Search This Blog

Monday 9 January 2012

Creating Custom Insert,Update,Delete Queries

At start up Hibernate generates standard CRUD queries for each Entity that is mapped in Hibernate. However just like custom loaders, Hibernate provides with the option of using our own custom queries for the Create, Update and Delete (CUD) Operations
Consider the Item Entity of the previous example.On start up the following CUD queries can be seen:
2641 [main] DEBUG org.hibernate.persister.entity.AbstractEntityPersister  -  Ins
ert 0: insert into ITEM (MODEL, SHELF_ID, ID) values (?, ?, ?)
2641 [main] DEBUG org.hibernate.persister.entity.AbstractEntityPersister  -  Upd
ate 0: update ITEM set MODEL=?, SHELF_ID=? where ID=?
2641 [main] DEBUG org.hibernate.persister.entity.AbstractEntityPersister  -  Del
ete 0: delete from ITEM where ID=?
2641 [main] DEBUG org.hibernate.persister.entity.AbstractEntityPersister  -  Ide
ntity insert: insert into ITEM (MODEL, SHELF_ID) values (?, ?)
I now modified the hbm file to include the queries for CUD operations.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.custom.crud">
    <class name="Item" table="ITEM">
        <id name="id" type="integer" column="ID">
            <generator class="native" />
        </id>
        <property name="model" column="MODEL" />
        <many-to-one name="shelf" class="Shelf" column="SHELF_ID"
            foreign-key="ITEM_FK1" />
        <sql-insert>
            Insert INTO ITEM(MODEL, SHELF_ID)
            values (?,?)
        </sql-insert>
        <sql-update>
            UpDaTe ITEM
            set MODEL =? ,SHELF_ID =?
            where
            ID =?
        </sql-update>
        <sql-delete>
            DeleTe fRom ITEM
            where ID = ?
        </sql-delete>
    </class>
</hibernate-mapping>
The queries are specified within each class element. The  sequence of columns in the insert and update queries is very important and must match the same sequence as those seen in the Hibernate generated queries.
The custom queries specified above include strange writing style so that their presence is visible in the logs.
On restart the logs indicate the new CUD queries:
2906 [main] DEBUG org.hibernate.persister.entity.AbstractEntityPersister  -  Ins
ert 0: Insert INTO ITEM(MODEL, SHELF_ID) values (?,?)
2906 [main] DEBUG org.hibernate.persister.entity.AbstractEntityPersister  -  Upd
ate 0: UpDaTe ITEM set MODEL =? ,SHELF_ID =? where ID =?
2906 [main] DEBUG org.hibernate.persister.entity.AbstractEntityPersister  -  Del
ete 0: DeleTe fRom ITEM where ID = ? 

I tested the queries using the below code:
static SessionFactory sessionFactory;
static Integer objectId;

public static void main(String[] args) {
    Configuration configuration = new Configuration();
    configuration = configuration.configure();
    sessionFactory = configuration.buildSessionFactory();
    testCreate();
    testUpdate();
    testDelete();
}
static void testCreate() {
    Item item = new Item();
    item.setModel("Liza");
    Session session = sessionFactory.openSession();
    Transaction t = session.beginTransaction();
    Shelf shelf = (Shelf) session.get(Shelf.class, 1);
    item.setShelf(shelf);
    session.save(item);
    t.commit();
    objectId = item.getId();    
}
static void testUpdate() {
    Session session = sessionFactory.openSession();
    Transaction t = session.beginTransaction();
    Item item = (Item) session.get(Item.class, objectId);
    item.setModel("Dinshaws");
    t.commit();
}
static void testDelete() {
    Session session = sessionFactory.openSession();
    Transaction t = session.beginTransaction();
    Item item = (Item) session.get(Item.class, objectId);
    session.delete(item);
    t.commit();
}
The logs indicate that the operations were performed succesfully.
2953 [main] DEBUG org.hibernate.jdbc.AbstractBatcher  - about to open PreparedSt
atement (open PreparedStatements: 0, globally: 0)
2953 [main] DEBUG org.hibernate.SQL  - 
    Insert 
    INTO
        ITEM
        (MODEL, SHELF_ID) 
    values
        (?,?)
...
3047 [main] DEBUG org.hibernate.persister.entity.AbstractEntityPersister  - Upda
ting entity: [com.custom.crud.Item#4]
3047 [main] DEBUG org.hibernate.SQL  - 
    UpDaTe
        ITEM 
    set
        MODEL =? ,
        SHELF_ID =? 
    where
        ID =?
...
3078 [main] DEBUG org.hibernate.persister.entity.AbstractEntityPersister  - Dele
ting entity: [com.custom.crud.Item#4]
3078 [main] DEBUG org.hibernate.SQL  - 
    DeleTe 
    fRom
        ITEM 
    where
        ID = ?
In the next post we shall perform the CUD operations directly involving database procedures and functions.

No comments:

Post a Comment