But this tutorial is a bit old, so I will tell here how integrate.
I will use mysql here. You can download backup database here. Restore this file to your mysql database.
You should use some jars to compile, I archived them here. (of course you can download latest versions, some of them can be out of date.)
You should extract these jars to YOUR_ECLIPSE_DIRECTORY/plugins/org.eclipse.birt.report.viewer_YOUR_VERSION/birt/WEB-INF/lib directory, as a result of this birt can use these libraries. (If you want to deploy this application you should copy these jars to your birt-runtime same directory)
This is my Customer.java bean. I will use it to get database records.
/**
*
*/
package org.eclipse.birt.samples.scripted.hibernate;
import java.util.Date;
/*
* Class Name: Customer
* Example User Class for Birt Scripted
* Datasource - Hibernate access
* BIRT Team
* August 15, 2005
*/
public class Customer {
private int customerNumber;
private String customerName;
private String contactLastName;
private String contactFirstName;
private String phone;
private String addressLine1;
private String addressLine2;
private String city;
private String state;
private String postalCode;
private String country;
private Integer salesRepEmployeeNumber;
private double creditLimit;
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
/**
*
*/
public Customer() {
super();
}
public String getAddressLine1() {
return addressLine1;
}
public void setAddressLine1(String addressLine1) {
this.addressLine1 = addressLine1;
}
public String getAddressLine2() {
return addressLine2;
}
public void setAddressLine2(String addressLine2) {
this.addressLine2 = addressLine2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getContactFirstName() {
return contactFirstName;
}
public void setContactFirstName(String contactFirstName) {
this.contactFirstName = contactFirstName;
}
public String getContactLastName() {
return contactLastName;
}
public void setContactLastName(String contactLastName) {
this.contactLastName = contactLastName;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public double getCreditLimit() {
return creditLimit;
}
public void setCreditLimit(double creditLimit) {
this.creditLimit = creditLimit;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public int getCustomerNumber() {
return customerNumber;
}
public void setCustomerNumber(int customerNumber) {
this.customerNumber = customerNumber;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public java.lang.Integer getSalesRepEmployeeNumber()
{
return this.salesRepEmployeeNumber;
}
/**
* Set the value of the salesRepEmployeeNumber column.
* @param salesrepemployeenumber
*/
public void setSalesRepEmployeeNumber(java.lang.Integer salesrepemployeenumber)
{
this.salesRepEmployeeNumber = salesrepemployeenumber;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
Then write your hibernate config file. Mine is under src directory named hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/classicmodels</property>
<property name="connection.username">root</property>
<property name="connection.password">mypassword</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">create</property> -->
<mapping resource="Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Of course you should use your username and password here for mysql.The xml below is my Customer.hbm.xml file which is under my src directory.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.eclipse.birt.samples.scripted.hibernate">
<class name="Customer" table="Customers">
<id name="customerNumber" column="customerNumber" type="integer">
<generator class="native"/>
</id>
<property name="customerName" column="customerName" type="string" not-null="true" />
<property name="contactLastName" column="contactLastName" type="string" not-null="true" />
<property name="contactFirstName" column="contactFirstName" type="string" not-null="true" />
<property name="phone" column="phone" type="string" not-null="true" />
<property name="addressLine1" column="addressLine1" type="string" not-null="true" />
<property name="addressLine2" column="addressLine2" type="string" />
<property name="city" column="city" type="string" not-null="true" />
<property name="state" column="state" type="string" />
<property name="postalCode" column="postalCode" type="string" />
<property name="country" column="country" type="string" not-null="true" />
<!-- this field caused problems do to null values in the table. Set nulls to 0 -->
<property name="salesRepEmployeeNumber" column="salesRepEmployeeNumber" type="integer"/>
<property name="creditLimit" column="creditLimit" type="double" />
<property name="date" column="date" type="date" />
</class>
</hibernate-mapping>
The HibernateUtil.java class is like below
package org.eclipse.birt.samples.scripted.hibernate;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
public static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this thread has none yet
if (s == null) {
s = sessionFactory.openSession();
// Store it in the ThreadLocal variable
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
}
And my CustomerList.java class is below.
package org.eclipse.birt.samples.scripted.hibernate;
import org.hibernate.Transaction;
import org.hibernate.Session;
import java.util.*;
public class CustomerList {
public static void main(String[] args) {
CustomerList cul = new CustomerList();
List customers = cul
.listCustomers("from Customer where salesRepEmployeeNumber = 0");
for (int i = 0; i < customer =" (Customer)" hibsession =" HibernateUtil.currentSession();" trans =" hibsession.beginTransaction();" result =" hibsession.createQuery(hql).list();">
You are allmost done. Create a new report, and pate the code below to your hibernat.rptdesign file.
<data-sources>
<script-data-source name="hibernate" id="6"/>
</data-sources>
<data-sets>
<script-data-set name="Hibernate_st" id="7">
<list-property name="resultSetHints">
<structure>
<property name="position">1</property>
<property name="name">STATE</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">2</property>
<property name="name">COUNTRY</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">3</property>
<property name="name">DATE</property>
<property name="dataType">any</property>
</structure>
<structure>
<property name="position">4</property>
<property name="name">ADDRESSLINE1</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">5</property>
<property name="name">ADDRESSLINE2</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">6</property>
<property name="name">CITY</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">7</property>
<property name="name">CONTACTFIRSTNAME</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">8</property>
<property name="name">CONTACTLASTNAME</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">9</property>
<property name="name">CREDITLIMIT</property>
<property name="dataType">float</property>
</structure>
<structure>
<property name="position">10</property>
<property name="name">CUSTOMERNAME</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">11</property>
<property name="name">CUSTOMERNUMBER</property>
<property name="dataType">integer</property>
</structure>
<structure>
<property name="position">12</property>
<property name="name">PHONE</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">13</property>
<property name="name">POSTALCODE</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">14</property>
<property name="name">SALESREPEMPLOYEENUMBER</property>
<property name="dataType">integer</property>
</structure>
</list-property>
<list-property name="columnHints">
<structure>
<property name="columnName">STATE</property>
</structure>
<structure>
<property name="columnName">COUNTRY</property>
</structure>
<structure>
<property name="columnName">DATE</property>
</structure>
<structure>
<property name="columnName">ADDRESSLINE1</property>
</structure>
<structure>
<property name="columnName">ADDRESSLINE2</property>
</structure>
<structure>
<property name="columnName">CITY</property>
</structure>
<structure>
<property name="columnName">CONTACTFIRSTNAME</property>
</structure>
<structure>
<property name="columnName">CONTACTLASTNAME</property>
</structure>
<structure>
<property name="columnName">CREDITLIMIT</property>
</structure>
<structure>
<property name="columnName">CUSTOMERNAME</property>
</structure>
<structure>
<property name="columnName">CUSTOMERNUMBER</property>
</structure>
<structure>
<property name="columnName">PHONE</property>
</structure>
<structure>
<property name="columnName">POSTALCODE</property>
</structure>
<structure>
<property name="columnName">SALESREPEMPLOYEENUMBER</property>
</structure>
</list-property>
<structure name="cachedMetaData">
<list-property name="resultSet">
<structure>
<property name="position">1</property>
<property name="name">STATE</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">2</property>
<property name="name">COUNTRY</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">3</property>
<property name="name">DATE</property>
<property name="dataType">any</property>
</structure>
<structure>
<property name="position">4</property>
<property name="name">ADDRESSLINE1</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">5</property>
<property name="name">ADDRESSLINE2</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">6</property>
<property name="name">CITY</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">7</property>
<property name="name">CONTACTFIRSTNAME</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">8</property>
<property name="name">CONTACTLASTNAME</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">9</property>
<property name="name">CREDITLIMIT</property>
<property name="dataType">float</property>
</structure>
<structure>
<property name="position">10</property>
<property name="name">CUSTOMERNAME</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">11</property>
<property name="name">CUSTOMERNUMBER</property>
<property name="dataType">integer</property>
</structure>
<structure>
<property name="position">12</property>
<property name="name">PHONE</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">13</property>
<property name="name">POSTALCODE</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">14</property>
<property name="name">SALESREPEMPLOYEENUMBER</property>
<property name="dataType">integer</property>
</structure>
</list-property>
</structure>
<property name="dataSource">hibernate</property>
<method name="open"><![CDATA[importPackage(Packages.org.eclipse.birt.samples.scripted.hibernate);
cul = CustomerList();
customers = cul.listCustomers("from Customer");
iterator = customers.iterator();]]></method>
<method name="fetch"><![CDATA[if(iterator.hasNext() == false ){
return false;
}
var customer = iterator.next( );
row["STATE"] = customer.getState();
row["COUNTRY"] = customer.getCountry();
row["DATE"] = customer.getDate();
row["ADDRESSLINE1"] = customer.getAddressLine1();
row["ADDRESSLINE2"] = customer.getAddressLine2();
row["CITY"] = customer.getCity();
row["CONTACTFIRSTNAME"] = customer.getContactFirstName();
row["CONTACTLASTNAME"] = customer.getContactLastName();
row["CREDITLIMIT"] = customer.getCreditLimit();
row["CUSTOMERNAME"] = customer.getCustomerName();
row["CUSTOMERNUMBER"] = customer.getCustomerNumber();
row["PHONE"] = customer.getPhone();
row["POSTALCODE"] = customer.getPostalCode();
row["SALESREPEMPLOYEENUMBER"] = customer.getSalesRepEmployeeNumber();
return true;]]></method>
<method name="close"><![CDATA[cul = null;
customers = null;
iterator = null;]]></method>
</script-data-set>
</data-sets>
I know you will say that how will I create such classes and xml's. I have the same problem. The article.rar file which I linked on my post tells in detailed way, how you will create using eclipse.
But I create a class to auto generate code for report xml and Classlist file. if you download and open my project, you will see the file named XMLCreatorExample.java file. By changing these variables packageName,packagepath and classNameToHibernate, you can easily generate these codes. And at last you can easily drag and drop the fields from your palette to your report.
You can download full project here
Enjoy!