hbm2pascal – online Object Pascal source generator for database entities

betasoft released a web application at http://hbm2pascal.appspot.com/ which can generate Object Pascal source for database objects based on Hibernate mapping documents.

How to use hbm2pascal: the web page contains a form which shows an example Hibernate mapping document, and a button to start the code generation. The example document may be edited freely. hbm2pascal will parse the table descriptions (including table relationships) and build Object Pascal source code. The generated sources then will be displayed and can be copied from the results page, or sent by email.

Hibernate mapping documents can be created manually, or extracted from metadata for existing databases using Hibernate Tools, a free toolkit available at http://www.hibernate.org/

The code templates used by hbm2pascal can be customized to generate source code specifc for O/R mappers like tiOPF, hcOPF, InstantObjects and the G Framework. Applications which use these object persistency frameworks and hbm2pascal will not require Hibernate at run time. Hibernate Tools are only required in the first step, to reverse engineer the metadata of an existing database to hbm mapping files, which then will be converted by hbm2pascal.

Code example part 1 – Hibernate mapping for the database table ‘Event’:

<class name="Event" table="events">
 <id name="id" column="uid" type="long" unsaved-value="null">
 <generator/>
 </id>

 <property name="name" type="string"  length="100">
 <meta attribute="field-description">The name of the event.</meta>
 </property>

 <property name="startDate" column="start_date" type="date">
 <meta attribute="field-description">The start date of the event.</meta>
 </property>

 <property name="duration" type="integer" >
 <meta attribute="field-description">The duration of the event.</meta>
 </property>

 <many-to-one name="location" column="location_id">
 <meta attribute="field-description">The location of the event.</meta>
 </many-to-one>

 <set name="speakers">
 <meta attribute="field-description">The speakers of the event.</meta>
 <key column="event_id"/>
 <one-to-many/>       
 </set>

 <set name="attendees">
 <meta attribute="field-description">The attendees of the event.</meta>
 <key column="event_id"/>
 <one-to-many/>           
 </set>
 </class>

Code example part 2 – generated source

(**
 * Generated Dec 3, 2009 4:29:51 PM by hbm2pascal
 * (c) Michael Justin - http://www.habarisoft.com/
 *)
unit Event;

interface

uses
 // Interface declarations
 AllInterfaces,
 // required units
 SysUtils, Classes;

type
 (**
 * Event class.
 *)
 TEvent = class (TInterfacedObject, IEvent)
 private
 FId: Int64;
 FName: string;
 FStartDate: TDateTime;
 FDuration: Integer;
 FLocation: ILocation;
 FSpeakers: TInterfaceList;
 FAttendees: TInterfaceList;

 // getter methods
 function GetId: Int64;
 function GetName: string;
 function GetStartDate: TDateTime;
 function GetDuration: Integer;
 function GetLocation: ILocation;

 // setter methods
 procedure SetId(const AValue: Int64);
 procedure SetName(const AValue: string);
 procedure SetStartDate(const AValue: TDateTime);
 procedure SetDuration(const AValue: Integer);
 procedure SetLocation(const AValue: ILocation);

 public
 constructor Create;
 destructor Destroy; override;

 // properties
 property Id: Int64 read GetId write SetId;

 (**
 * The name of the event.
 *)
 property Name: string read GetName write SetName;

 (**
 * The start date of the event.
 *)
 property StartDate: TDateTime read GetStartDate write SetStartDate;

 (**
 * The duration of the event.
 *)
 property Duration: Integer read GetDuration write SetDuration;

 (**
 * The location of the event.
 *)
 property Location: ILocation read GetLocation write SetLocation;

 (**
 * The speakers of the event.
 *)
 property Speakers: TInterfaceList read GetSpeakers;

 (**
 * The attendees of the event.
 *)
 property Attendees: TInterfaceList read GetAttendees;

 end;

implementation

{ TEvent }

constructor TEvent.Create;
begin
 inherited;

 FSpeakers := TInterfaceList.Create;
 FAttendees := TInterfaceList.Create;
end;

destructor TEvent.Destroy;
begin
 FSpeakers.Free;
 FAttendees.Free;

 inherited;
end;

function TEvent.GetId: Int64;
begin
 Result := FId;
end;

function TEvent.GetName: string;
begin
 Result := FName;
end;

function TEvent.GetStartDate: TDateTime;
begin
 Result := FStartDate;
end;

function TEvent.GetDuration: Integer;
begin
 Result := FDuration;
end;

function TEvent.GetLocation: ILocation;
begin
 Result := FLocation;
end;

procedure TEvent.SetId(const Value: Int64);
begin
 FId := AValue;
end;

procedure TEvent.SetName(const Value: string);
begin
 FName := AValue;
end;

procedure TEvent.SetStartDate(const Value: TDateTime);
begin
 FStartDate := AValue;
end;

procedure TEvent.SetDuration(const Value: Integer);
begin
 FDuration := AValue;
end;

procedure TEvent.SetLocation(const Value: ILocation);
begin
 FLocation := AValue;
end;

end.
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s