Smooks Entity Persistence Frameworks - Hibernate

With the new Smooks Persistence cartridge in Smooks 1.2, we can directly use several entity persistence frameworks from within Smooks (Hibernate, JPA etc). So in this blog post let's try out the Hibernate sample specified in here.
Since smooks have given the basic configurations only, this blog post is going to assist the peolple who need full implementation.

What we are going to do is process the following XML message and store the product information to a bean and persist the order information.


First we have to Specify our Entity classes (Order, OrderLine, Product)
//Order class
package example.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

public class Order {
    private Integer ordernumber;
    private String customerId;
    @OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
    private List orderItems = new ArrayList();
    public void addOrderLine(OrderLine orderLine) {
    public Integer getOrdernumber() {
 return ordernumber;

   public void setOrdernumber(Integer ordernumber) {
 this.ordernumber = ordernumber;

   public String getCustomerId() {
 return customerId;

   public void setCustomerId(String customerId) {
 this.customerId = customerId;

   public List getOrderItems() {
 return orderItems;

   public void setOrderItems(List orderItems) {
 this.orderItems = orderItems;

//OrderLine class 
package example.entity;

import javax.persistence.*;

@Table(name = "orderlines")
public class OrderLine {

 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Integer id;

 @JoinColumn(name = "orderid")
 private Order order;
 private Integer quantity;

 @JoinColumn(name = "productid")
 private Product product;

 public Integer getId() {
  return id;

 public void setId(Integer id) { = id;

 public Order getOrder() {
  return order;

 public void setOrder(Order order) {
  this.order = order;

 public Integer getQuantity() {
  return quantity;

 public void setQuantity(Integer quantity) {
  this.quantity = quantity;

 public Product getProduct() {
  return product;

 public void setProduct(Product product) {
  this.product = product;

//Product Class
package example.entity;

import javax.persistence.*;

@Table(name = "products")
public class Product {
    private Integer id;
    private String name;

 public Integer getId() {
  return id;

 public void setId(Integer id) { = id;

 public String getName() {
  return name;

 public void setName(String name) { = name;

Here is the Smooks configuration to bind xml message to java beans and persist the order entity. 

<smooks-resource-list xmlns:dao="" xmlns:jb="" xmlns="">

    <jb:bean beanid="order" class="example.entity.Order" createonelement="order">
        <jb:value data="ordernumber" property="ordernumber"></jb:value>
        <jb:value data="customer" property="customerId"></jb:value>
        <jb:wiring beanidref="orderLine" settermethod="addOrderLine">
    <jb:bean beanid="orderLine" class="example.entity.OrderLine" createonelement="order-item">
        <jb:value data="quantity" property="quantity"></jb:value>
        <jb:wiring beanidref="order" property="order">
        <jb:wiring beanidref="product" property="product">
    <dao:locator beanid="product" lookuponelement="order-item" onnoresult="EXCEPTION" uniqueresult="true">
        <dao:query>from Product p where = :id</dao:query>
            <dao:value data="product" decoder="Integer" name="id"></dao:value>
    <dao:inserter beanid="order" insertonelement="order"></dao:inserter>
So now you would wonder where we store the datasource configuration. If we just use Hibernate we can configure datasource information inside hibernate.cfg.xml
    <property name="connection.url">jdbc:mysql://localhost:3306/dbname</property>
    <property name="connection.username">uname</property>
    <property name="connection.password">pwd</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">false</property>
    <property name="format_sql">true</property>
    <property name="">update</property>
    <property name="connection.pool_size">1</property>
    <property name="current_session_context_class">thread</property>
  <mapping class="example.entity.Product"></mapping>
  <mapping class="example.entity.Order"></mapping>
  <mapping class="example.entity.OrderLine"></mapping>
The database represented in the connection must have 4 tables.

customers (id int(11), name varchar(30))
products (id int(11), name varchar(30))
orders (ordernumber int(11), customerId varchar(30))
orderlines (id int(11), quantity int(11), orderid int(11), productid int(11))

Insert the following sample data to customers and procucts tables.

insert into customers (id, name) values (123456, 'Devin Snow');
insert into customers (id, name) values (789101, 'Homer Simpson');
insert into customers (id, name) values (999999, 'John Doe');

insert into products  (id, name) values (11, 'Cheese cake');
insert into products  (id, name) values (22, 'Chocolate 300gr');
insert into products  (id, name) values (33, 'Beer');
insert into products  (id, name) values (44, 'Smooks in Action');
insert into products  (id, name) values (55, 'Ultimate guide to Smooks');

Let's execute Smooks now. 

package example.main;


import javax.xml.transform.Source;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.milyn.Smooks;
import org.milyn.container.ExecutionContext;
import org.milyn.persistence.util.PersistenceUtil;
import org.milyn.scribe.adapter.hibernate.SessionRegister;
import org.milyn.scribe.register.DaoRegister;
import org.xml.sax.SAXException;

public class Main {

 public static void main(String args[]) throws IOException, SAXException {
  Smooks smooks = new Smooks("smooks-hbt-config.xml");

  ExecutionContext executionContext = smooks.createExecutionContext();

  SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
  Session session = sessionFactory.openSession();
  DaoRegister register = new SessionRegister(session);
  // This sets the DAO Register in the executionContext for Smooks //to access it. 
  Transaction transaction = session.beginTransaction();
  Source source = new StreamSource(new File("input.xml"));
  smooks.filterSource(executionContext, source);



Post a Comment

Popular posts from this blog

PHP-SOAP web service with out a WSDL

Boomi Mapping - Removing special chars from an input

How to add Maven dependency from a relative path referencing a local jar