Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [Teneo][Hibernate] Hints on general usage or lifecycle(Table creation, initialising the DataStore etc.)
[Teneo][Hibernate] Hints on general usage or lifecycle [message #1016991] Fri, 08 March 2013 13:59 Go to next message
Marina Knieling is currently offline Marina Knieling
Messages: 78
Registered: February 2013
Member
Hi community,

I'm quite new to Teneo and Hibernate and so I'm a bit confused on how this initialisation and table creation process works.

I've worked through the Quick Start Tutorial and it works fine. I want to use Teneo/Hibernate for my e4 project, which is no problem, but how is the workflow to work with Teneo/Hibernate?

I know I need to create those tables from my EMF model, but presumably only once. Will Teneo look after that or do I have to make sure, that my tables don't get generated (and cleared?) every time I start my application? Where and how would I do that?

How would I seperate - if necessary - the initialisation (creating the tables, doing the mapping) from the actual usage (getting objects out of the database, saving objects, etc.).

I'm using HSQL DB and I want my database connection to work as an OSGI service (already implemented the service stuff).

Could someone please point out any hints or best practices on how to use Teneo/Hibernate in the real world? Tutorials are great, but they often do everything in one class.

Thanks in advance
Marina
Re: [Teneo][Hibernate] Hints on general usage or lifecycle [message #1017016 is a reply to message #1016991] Fri, 08 March 2013 16:17 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5351
Registered: July 2009
Senior Member
Am 08.03.13 14:59, schrieb Marina Knieling:
> Hi community,
>
> I'm quite new to Teneo and Hibernate and so I'm a bit confused on how
> this initialisation and table creation process works.
>
> I've worked through the Quick Start Tutorial and it works fine. I want
> to use Teneo/Hibernate for my e4 project, which is no problem, but how
> is the workflow to work with Teneo/Hibernate?
>
> I know I need to create those tables from my EMF model, but presumably
> only once. Will Teneo look after that or do I have to make sure, that my
> tables don't get generated (and cleared?) every time I start my
> application? Where and how would I do that?

I hibernate does that. Teneo simply translates the EMF-Model into a
hbm.xml file. I'm not a fan of this auto-table-creation in production
(well if you don't have a standalone app you probably don't even have
the privileges to drop/create tables).

So turned it of using:

Properties configure = new Properties();
configure.setProperty("hibernate.hbm2ddl.auto", "none");
// ... more settings

HbSessionDataStore hbds = new HbSessionDataStore();
hbds.setDataStoreProperties(props);


>
> How would I seperate - if necessary - the initialisation (creating the
> tables, doing the mapping) from the actual usage (getting objects out of
> the database, saving objects, etc.).
>

I have a small RCP-Application simply executing the hbm.xml and DDL so
that I'm able to provide it to the DBA to create the tables on the server.

The small util does something like this:

It does something like this:

EPackage[] packages = ...

Properties configure = new Properties();
String mapping = HbHelper.INSTANCE.generateMapping(packages, configure);
File hbmXML = new File("hibernate.hbm.xml");
FileWriter writer = new FileWriter(hbmXML);
writer.write(mapping);
writer.close();


org.hibernate.cfg.Configuration cfg = new Configuration();
cfg.setProperty("hibernate.dialect", dbdialect);
cfg.addXML(slurpFile(hbmXML));

SchemaExport export = new SchemaExport(configuration);
....
export.execute(true, false, false, true);

> I'm using HSQL DB and I want my database connection to work as an OSGI
> service (already implemented the service stuff).

What do you mean with database connection as OSGi. Hibernate holds
itsown Pool of connections and you configure that when setting up the
HbSessionDataStore.

In our application the HbSessionDataStore is registered as an
OSGi-Service and gets its configuration (e.g. username, ...) from a
Configuration-Service registered also in the OSGi-Service-Registry.

>
> Could someone please point out any hints or best practices on how to use
> Teneo/Hibernate in the real world? Tutorials are great, but they often
> do everything in one class.
>

IMHO you UI-Code should not see that you are loading the information
through Hibernate.

Say you have an UI showing Person-Objects then you have the following
classes:

PersonForm - the ui
PersonDatasource - interface to load person instance, save them, ...
TeneoPersonDatasource - concrete implementation of the PersonDatasource
=> Registered as an OSGi-Service so that it can
get the HbSessionStore injected



Tom
Re: [Teneo][Hibernate] Hints on general usage or lifecycle [message #1017129 is a reply to message #1017016] Sat, 09 March 2013 17:40 Go to previous messageGo to next message
Marina Knieling is currently offline Marina Knieling
Messages: 78
Registered: February 2013
Member
Hi Tom,

I annotated your post a bit, to put my questions into context.
The green things are just notes for myself, the red things are questions.

Quote:

>>EPackage[] packages = ...

>>Properties configure = new Properties();

// Do I add more Properties here, just like for the HbDataStore or is this object just there to put it in the following method?

// File creation
>>String mapping = HbHelper.INSTANCE.generateMapping(packages, configure);
>>File hbmXML = new File("hibernate.hbm.xml");
>>FileWriter writer = new FileWriter(hbmXML);
>>writer.write(mapping);
>>writer.close();

// Hibernate configuration
org.hibernate.cfg.Configuration cfg = new Configuration();
cfg.setProperty("hibernate.dialect", dbdialect);
cfg.addXML(slurpFile(hbmXML)); // what is slurpFile?

// table creation?
SchemaExport export = new SchemaExport(configuration);
....
export.execute(true, false, false, true);


Is this a seperate bundle then that is only responsible for creating the mapping (file) and the tables in my database? Is this executed only once to set everything up or does it belong somewhere in my application life cycle?

Quote:

What do you mean with database connection as OSGi. Hibernate holds
itsown Pool of connections and you configure that when setting up the
HbSessionDataStore.

In our application the HbSessionDataStore is registered as an
OSGi-Service and gets its configuration (e.g. username, ...) from a
Configuration-Service registered also in the OSGi-Service-Registry.


I meant exactly something like that. To have an OSGi-Service that gives me access to the database throughout my whole application.

Quote:

IMHO you UI-Code should not see that you are loading the information
through Hibernate.

That's the plan Smile

Quote:

Say you have an UI showing Person-Objects then you have the following
classes:

PersonForm - the ui // clear

PersonDatasource - interface to load person instance, save them, ...
// is this again an OSGi-service interface?

TeneoPersonDatasource - concrete implementation of the PersonDatasource
=> Registered as an OSGi-Service so that it can
get the HbSessionStore injected


As I understand this, you have a PersonDatasource service that provides AND references a service. It provides the access to the database actions like create, save, edit... and references therefore the HbSessionDataStore service. Or do I only use DI to get the HbSessionDataStore without referencing the service?

I probably still have to configure that HbSessionDataStore in that service. eclipse wiki says:
Quote:

To create and initialize a HbSessionDataStore, follow these steps:

1. Create and register the HbSessionDataStore using HbHelper.INSTANCE.createRegisterDataStore().
2. Configure the EPackages to be handled by this DataStore using HbDataStore.setEPackages().
3. Configure Hibernate properties for accessing the database.
4. Call initialize() to build the Hibernate mapping and create the tables in the database.


Do I understand it right, that I only need to do steps 1-3 as I have created the mapping and tables already in that little application above? And step 3 would include the code you mentioned before:

Properties configure = new Properties();
configure.setProperty("hibernate.hbm2ddl.auto", "none");
// ... more settings

HbSessionDataStore hbds = new HbSessionDataStore();
hbds.setDataStoreProperties(props);


but how does Hibernate find the mapping then?

Can I also use a config xml file instead? I know I can reference the mapping file there like this:
<!-- Import mapping resource -->
<mapping resource="hibernate.hbm.xml"/>


I know these are a lot of questions again. This is all a bit confusing for first timers, but I hope once all these little bits are clear, this is going to be a walk in the park for future projects. All beginnings are difficult.

Thanks again in advance for your patience and time

Marina
Re: [Teneo][Hibernate] Hints on general usage or lifecycle [message #1017267 is a reply to message #1017129] Mon, 11 March 2013 11:35 Go to previous messageGo to next message
Martin Taal is currently offline Martin Taal
Messages: 5335
Registered: July 2009
Senior Member
Hi Marina,
Colours don't show up in all email clients. But I think I found your remarks, see below marked with MT>>

Btw, to make it somewhat harder, you can also check out this alternative solution:
http://martintaal.wordpress.com/2012/08/30/taking-emf-to-the-third-tier-the-texo-json-resource/
http://martintaal.wordpress.com/2012/04/05/emf-texo-json-rest-web-service-support-connecting-your-client-to-an-emf-supporting-web-server/

gr. Martin

On 03/09/2013 06:40 PM, Marina Knieling wrote:
> Hi Tom,
>
> I annotated your post a bit, to put my questions into context.
> The green things are just notes for myself, the red things are questions.
>
> Quote:
>> >>EPackage[] packages = ...
>>
>> >>Properties configure = new Properties();
>>
>> // Do I add more Properties here, just like for the HbDataStore or is this object just there to put it in the
>> following method?
>>
>> // File creation
>> >>String mapping = HbHelper.INSTANCE.generateMapping(packages, configure);
>> >>File hbmXML = new File("hibernate.hbm.xml");
>> >>FileWriter writer = new FileWriter(hbmXML);
>> >>writer.write(mapping);
>> >>writer.close();
>>
>> // Hibernate configuration
>> org.hibernate.cfg.Configuration cfg = new Configuration();
>> cfg.setProperty("hibernate.dialect", dbdialect);
>> cfg.addXML(slurpFile(hbmXML)); // what is slurpFile?
>>
>> // table creation?
>> SchemaExport export = new SchemaExport(configuration);
>> ....
>> export.execute(true, false, false, true);
>
>
> Is this a seperate bundle then that is only responsible for creating the mapping (file) and the tables in my database?
> Is this executed only once to set everything up or does it belong somewhere in my application life cycle?
MT>> At install time and when you change the model, so not sure if it needs a separate bundle, that's up to you.

>
> Quote:
>> What do you mean with database connection as OSGi. Hibernate holds
>> itsown Pool of connections and you configure that when setting up the
>> HbSessionDataStore.
>>
>> In our application the HbSessionDataStore is registered as an
>> OSGi-Service and gets its configuration (e.g. username, ...) from a
>> Configuration-Service registered also in the OSGi-Service-Registry.
>
>
> I meant exactly something like that. To have an OSGi-Service that gives me access to the database throughout my whole
> application.

MT>> Yes, this service then inside uses hibernate to talk to the database. So the service api itself should only contain
higher level methods I guess.
MT>> But it depends a bit if you are sticking/developing a service oriented app, or a straight client-server app which
may benefit from a lower api.

>
> Quote:
>> IMHO you UI-Code should not see that you are loading the information
>> through Hibernate.
>
> That's the plan :)
MT>> This can also be done through EMF resources, to hide the underlying implementation from the UI. Or look at the 2
links I pasted above, in this case using json as the communication format.

>
> Quote:
>> Say you have an UI showing Person-Objects then you have the following
>> classes:
>>
>> PersonForm - the ui // clear
>>
>> PersonDatasource - interface to load person instance, save them, ...
>> // is this again an OSGi-service interface?
>>
>> TeneoPersonDatasource - concrete implementation of the PersonDatasource
>> => Registered as an OSGi-Service so that it can
>> get the HbSessionStore injected
>
>
> As I understand this, you have a PersonDatasource service that provides AND references a service. It provides the access
> to the database actions like create, save, edit... and references therefore the HbSessionDataStore service. Or do I only
> use DI to get the HbSessionDataStore without referencing the service?
>
> I probably still have to configure that HbSessionDataStore in that service. eclipse wiki says:
> Quote:
>> To create and initialize a HbSessionDataStore, follow these steps:
>>
>> 1. Create and register the HbSessionDataStore using HbHelper.INSTANCE.createRegisterDataStore().
>> 2. Configure the EPackages to be handled by this DataStore using HbDataStore.setEPackages().
>> 3. Configure Hibernate properties for accessing the database.
>> 4. Call initialize() to build the Hibernate mapping and create the tables in the database.
>
>
> Do I understand it right, that I only need to do steps 1-3 as I have created the mapping and tables already in that
> little application above?
MT>> The above steps also happen when your application starts except for creating the tables, this only needs to happen
at application install.

And step 3 would include the code you mentioned before:
>
>
> Properties configure = new Properties();
> configure.setProperty("hibernate.hbm2ddl.auto", "none");
> // ... more settings
>
> HbSessionDataStore hbds = new HbSessionDataStore();
> hbds.setDataStoreProperties(props);
>
>
> but how does Hibernate find the mapping then?
>
> Can I also use a config xml file instead? I know I can reference the mapping file there like this:
>
> <!-- Import mapping resource -->
> <mapping resource="hibernate.hbm.xml"/>
MT>> Teneo has to control hibernate at runtime so you can't use a mapping file in the config.xml. To use a hbm file
(which should be generated by teneo) check out the relevant option (mapping_file_name) here:
http://wiki.eclipse.org/Teneo/Hibernate/Configuration_Options

For your understanding, teneo generates the mapping at runtime in-memory based on the model, you only need to use an
explicit mapping file if you would want to have very special/extensive customization (of this mapping file after letting
it be generated by teneo). It is better to use in-model annotations to control the mapping:
http://wiki.eclipse.org/Teneo/Hibernate/ModelRelational/Annotations_Format


>
>
> I know these are a lot of questions again. This is all a bit confusing for first timers, but I hope once all these
> little bits are clear, this is going to be a walk in the park for future projects. All beginnings are difficult.
>
> Thanks again in advance for your patience and time
>
> Marina


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: [Teneo][Hibernate] Hints on general usage or lifecycle [message #1059059 is a reply to message #1017267] Thu, 16 May 2013 13:21 Go to previous messageGo to next message
Athanasios Papakostopoulos is currently offline Athanasios Papakostopoulos
Messages: 3
Registered: March 2013
Junior Member
Hi,

I am also new to Teneo and Hibernate and I am trying to figure out the appropriate usage. I will try to summarize what I understood from the conversation above.

Installation Time
1 Set up properties for Hibernate (this can be done also by a hibernate.properties file in the classpath)
2 Set up properties for Teneo (programmatically or additional properties file)
3 Create HbDataStore
4 Initialize HbDataStore
5 Save Teneo generated mapping file (create xxx.hbm.xml)

Run time
I thought that I needed only a SessionFactory. I was planning to get one from Configuration().configure().buildSessionFactory() after suppling a hibernate.cfg.xml and the xxx.hbm.xml I created during installation.This comes from Hibernate tutorials.

But Martin suggested to use the teneo configuration option teneo.mapping.mapping_file_name which means I have to repeat 1-4?

Questions
1 What are the appropriate actions at run time?
2 Can someone elaborate on the teneo.mapping.hibernate_mapping_file option?

Thank you

[Updated on: Thu, 16 May 2013 13:23]

Report message to a moderator

Re: [Teneo][Hibernate] Hints on general usage or lifecycle [message #1059275 is a reply to message #1059059] Thu, 16 May 2013 15:00 Go to previous message
Martin Taal is currently offline Martin Taal
Messages: 5335
Registered: July 2009
Senior Member
Hi Athanasios,
Normally you don't need to save the mapping file or work with an explicit mapping file. Teneo will generate the mapping
for you in memory and use that. A separate mapping file is only needed if you want to manually change the mapping and
then use that, but this is better avoided as the next time you change the model you need to regenerate and change the
mapping file again.

The steps are:
1 Set up properties for Hibernate (this can be done also by a hibernate.properties file in the classpath)
2 Set up properties for Teneo (progemmatically or additional properties file)
3 Create HbDataStore
4 Set the epackage to map/model in the HbDataStore
5 Initialize HbDataStore
6 Get a SessionFactory from the HbDataStore (it has a getSessionFactory method)

gr. Martin

On 05/16/2013 03:21 PM, Athanasios Papakostopoulos wrote:
> Hi,
>
> I am also new to Teneo and Hibernate and I am trying to figure out the appropriate usage. I will try to summarize what
> I understood from the conversation above.
>
> Installation Time
> 1 Set up properties for Hibernate (this can be done also by a hibernate.properties file in the classpath)
> 2 Set up properties for Teneo (progemmatically or additional properties file)
> 3 Create HbDataStore
> 4 Initialize HbDataStore
> 5 Save Teneo generated mapping file (create xxx.hbm.xml)
>
> Run time
> I thought that I needed only a SessionFactory. I was planning to get one from
> Configuration().configure().buildSessionFactory() after suppling a hibernate.cfg.xml and the xxx.hbm.xml I created
> during installation.This comes from Hibernate tutorials.
>
> But Martin suggested to use the teneo configuration option teneo.mapping.mapping_file_name which means I have to repeat
> 1-4?
>
> Questions
> 1 What are the appropriate actions at run time?
> 2 Can someone elaborate on the teneo.mapping.hibernate_mapping_file option?
>
> Thank you


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Previous Topic:EMF 2.7 Unable to support enumerations called Enum
Next Topic:[CDO] IProgressMonitor and Saveable
Goto Forum:
  


Current Time: Thu Oct 02 04:36:08 GMT 2014

Powered by FUDForum. Page generated in 0.01908 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software