Skip to main content
Jump to: navigation, search

Stardust/Knowledge Base/Integration/Data/Hibernate Data Examples

Hibernate Data Type Examples

Purpose

This article covers the Hibernate Data Type (one of the data types used in Modelling workbench), and uses audit trail database as datasource.

Requirements

  • Hibernate libraries
  • hibernate3.jar
  • Database client driver
  • derbyclient.jar
Preparation Create the table TESTUSER in the audittrail data source. Use the following SQL-Statement to do this.
CREATE TABLE testuser ( 
    id BIGINT NOT NULL,
   name VARCHAR(255),
   PRIMARY KEY (id)
 );

Configuration

carnot-spring-context.xml

...
<!-- deactivate the common CARNOT-TXManager
<bean id="carnotTxManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="carnotAuditTrailDataSource" />
</property>
</bean>
-->
<!-- create a new hibernate based TXManager -->
<bean id="carnotTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="hibernateSessionFactory" />
</property>
</bean>

...

 <!-- create a hibernate session factory -->
<!-- using the audittrail database as datasource and use a separat hibernate configuration file -->
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="carnotAuditTrailDataSource" />
<property name="configLocation" value="classpath:/hibernate.cfg.xml" />
</bean>
  • Change to View "java perspective" --> "package Explorer" in Eclipse
  • Create a folder "etc" in your Application and the file "hibernate.cfg.xml"
  • To add the Sourse to a Build Path execute a Command "Buil Path --> Use as Source Folder" on the "etc"-Folder

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="show_sql">true</property> <!-- shows the executed sql-statements as console output -->
		<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
		<property name="hiberante.connection.autocommit">false</property>
		<property name="hibernate.current_session_context_class">thread</property>
		<!-- Mapping files -->
		<mapping resource="UserImpl.hbm.xml" />
	</session-factory>
</hibernate-configuration>

UserImpl.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.sungard.hibernate.test">
  <class name="com.sungard.hibernate.test.User" table="testuser">
    <id name="id" column="id" type="long" unsaved-value="0">
      <generator class="increment" />
    </id>
    <property name="id" type="long" update="false" insert="false" column="id" />
    <property name="name" type="java.lang.String" update="true" insert="true" column="name" />
  </class>
</hibernate-mapping>

Java classes

User.java
package com.sungard.hibernate.test;
 
import java.io.Serializable;
 
public class User implements Serializable { private static final long serialVersionUID = -3748194293463505622L;
 
private long id; private String name;
 
public User() { }
 
public String getName() { 
  return name; 
} 
public void setName(String name) { 
  this.name = name;
}
public long getId() {
  return id; 
} 
public void setId(long id) { 
  this.id = id; 
}

Selecting Hibernate Data Object

  • Create a manual activity to collect all data
  • id of type long
  • Create an application activity
  • Set id as in-data
  • Application must load an object of your configured hibernate data type
  • Return hibernate data type to process
  • Create an application activity
  • Application must nullify your hibernate data type
  • Return null for hibernate data type to process

Sample Process Definition

Hibernate Data Process

Sample Application

package com.sungard.hibernate.test;
 
public class LoadApplication {
 
private User user;
 
public void complete() {
 
   ApplicationContext appContext = SpringUtils.getApplicationContext();
 
   SessionFactory sessionFactory = (SessionFactory) appContext.getBean("hibernateSessionFactory");
 
   Session session = sessionFactory.openSession();
 
   this.user = (User)session.get(User.class, new Long(id));
 
   session.close();
}
 
public User getUser () {
 
   return this.user;
 }
}

Creating Hibernate Data Object

  • Create a manual activity to collect all data
  • name of type String
  • Create an application activity
  • set name as in-data
  • application must create an object of your configured hibernate data type
  • return hibernate data type to process
  • Create a manual activity to display the stored data
  • ID mapped from user.getId()
  • name mapped from user.getName()

Sample Process Definition

Hibernate Create Data Process

Sample Application
package com.sungard.hibernate.test;
 
public class CreateApplication {
 
private String userName;
 
public void setUserName (String userName) {
  this.userName = userName;
}
 
public void complete() {
}
 
public User getUser () {
 User user = new User();
 user.setName (this.userName);
 return user; 
}
}

Updating Hibernate Data Object

  • Create a manual activity to collect all data
  • id of type long
  • Create an application activity
  • set id as in-data
  • application must load (create) an object of your configured hibernate data type
  • return hibernate data type to process
  • Create a manual activity to modify the stored data
  • you can map id and name as in-data to display these informations (see screenshot)
  • name mapped to user.setName()
  • Create a manual activity to display the stored data
  • ID mapped from user.getId()
  • name mapped from user.getName()

Sample Process Definition

Hibernate Update Data Process

Sample Application
package com.sungard.hibernate.test;
 
public class UpdateApplication {
 
private long userId;
 
public void setUserId (String userId) {
 
 this.userId = userId;
}
 
public void complete() {
 
}
 
public User getUser () {
// we create a user here -> to load from db use hibernate! -> e.g. see LoadApplication
User user = new User();
user.setId (this.userId);
return user;
}
}

Deleting Hibernate Data Object

  • Create a manual activity to collect all data
  • id of type long
  • Create an application activity
  • set id as in-data
  • application must load (create) an object of your configured hibernate data type
  • return hibernate data type to process
  • Create an application activity
  • application must nullify your hibernate data type
  • return null for hibernate data type to process

Sample Process Definition

Hibernate Delete Data Process

Sample Application

package com.sungard.hibernate.test;
 
public class DeleteApplication {
 
public void complete() {
}
 
public User getUser () {
  return null;
}
}

Back to the top