Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

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

< Stardust‎ | Knowledge Base‎ | Integration‎ | Data
Revision as of 05:39, 13 November 2011 by Unnamed Poltroon (Talk) (Configuration)

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"<br> "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; 
}

Back to the top