SharenGo Wiki
About Sharengo
- SharenGo
- Members
- Join us
- Contact

Methodology
- Methodology

Tools
- PragMaTic
- ABS
- JsRIA

Projects
- MarcoPolo

Components Directory

Infrastructure
- HAPlatForm

Legal

Community
- Tracker
- MailingLists

Wiki
- WikiHelp
- RecentChanges
- RSS
- WikiSearch

HelloWorld again

Starting with project from tutorial1 we add some functionnalities using a persistence layer based on hibernate.


Prerequisite
We must have a postgresql db engine up and running


1) adding dependency with hibernate library

All applications using hibernate needs to use hibernate2.jar in order to compile.

abs dep:add.bin
hibernate2.jar
abs dep:add.src
middleware/gluon-hibernate/tags/3
If the component is not locally installed, the script will failed with the message "component [middleware/gluon-hibernate/tags/3] not found". We have first to create the gluon component in the chain.

cd /home/$USER/test
abs new.scm.component
middleware/gluon-hibernate/tags/3
sharengo
nlassalle: to get this tutorial working, I had to add two others binary dependancies :
abs dep:add.bin
commons-collections-3.1.jar
abs dep:add.bin
jta.jar
2) adding entity class and EntityMgr in UML model

All persistents classes have the "entity" stereotype. This stereotype is used by PragMatic? code generator to create :
- all code related the persistent class
- a script to create the sql structure
And for each entities you must define an object identifier, you must add "PrimaryKey" stereotype on attribute.
WARNING : You must define a package for each entity.


We add a new class "HelloHistory" containing for each run of the application the name used and date of run.

We create a new class "HelloEntitiesMgr" with a "entitiesmgr" stereotype. The kind of stereotype caracterize a class similar to a "process" class but whose task is only to manipulate persistent datas (classes). A process class group business process and use exclusively entitiesmgr to get access to persistent classes.



3) generating code and sql schema
abs mda:gen.java.local
abs mda:gen.hbm.local
abs lib:all
abs hibernate:schemaexport.local
Five new classes are generated :
- HelloHistory.java persistent business class
- I_HelloEntitiesMgr.java interface for the entities manager
- Impl_HelloEntitiesMgr.java implementation for the entities manager

We have file ./server/main/conf/org/sharengo/tutorial1/HelloWorldHistory.hbm.xml defining the mapping between the java class and some sql tables.

And last but not least an sql script file ./server/sql/schemaexport.sql
to create table in our database. Be cautious, this script drop all the tables before recreating them.

NB: you must compile the module (abs lib:local) before generating the sql schema.

3) create a new database
su postgres
/usr/local/pgsql/bin/createdb tutorial

4) create table
/usr/local/pgsql/bin/psql -d tutorial < server/sql/schemaexport.sql
5) updating model
update model to describe two functionnalities : storing each hello in database and retrieving list of "hello".

We add two methods in HelloEntitiesMgr




public void save(HelloHistory hello)

This method receive a HelloHistory object and save it in database.
Note that a generic exception is automatically added. The GenericHelloProcessException class which has been automatically generated in the model in order to be able to use it in our method.

For some specifics methods name (create, save, update, delete, remove) PragMatic generate automatically some code that you will be able to tune manually.
In our case, we have nothing to do.


        //proposed code
        net.sf.hibernate.Session session = null;
        net.sf.hibernate.Transaction tx = null;

        try {
           session = (net.sf.hibernate.Sessionconfiguration.getPersistentStore(
                "");
           tx = session.beginTransaction();
           session.saveOrUpdate(hello);
           session.flush();
           tx.commit();
        catch (Exception e) {
           tx.rollback();
           throw e;
        



The second method :

public Collection list()


for this kind of name (list, findAll), PragMatic is also able to propose some code, but this time we have to tune manually.


        net.sf.hibernate.Session session = null;
        java.util.Collection hellos = null;

        try {
           session = (net.sf.hibernate.Sessionconfiguration.getPersistentStore(
                "");
           hellos = session.find("from " + Hello.class.getName());
        finally {
           if (session != null) {
              session.close();
              session = null;
           }


PragMatic try to guess the name of the managed entity, but this time, it's wrong. It try to use an entity name "Hello" but we don't have one, our entity is HelloHistory. The proposed name was deduced from the entity manager class name "HelloEntityMgr".

And we want to add a descendant sort order for our result collection. So we just change this line :

hellos = session.find("from " + Hello.class.getName());


by this one :

hellos = session.find("from " + HelloHistory.class.getName()+" hello order by hello.eventDate desc");



6) updating configuration for factory

We have created an entities manager used by the process class. The navigable end of the relation is named "entitiesmgr" so, PragMatic will be able to generate automatically the creation and initialization of HelloEntitiesMgr from Impl_HelloProcess. We have to complete the configuration description in the model in order for the factory to find the correct implementation.



7) generating code
abs mda:gen.java.local 
8) compilation must be ok
abs lib:local

9) updating model
We change our answer class to add a method to set history list.



We need a method in HelloProcess to retrieve the history to show to user.



10) generating code
abs mda:gen.java.local 
11) adding specific code

in HelloAnswerView, one line in setHistory method :

tpl.setValue(HISTORY, history);



in Impl_HelloProcess, one line in getHistory method :

return entitiesmgr.list();


and a handful more in buildMessage in order to save the current message :

HelloHistory hh = new HelloHistory();
hh.setName(name);
hh.setEventDate(new java.util.Date());
entitiesmgr.save(hh);
return "Hello " + name;


Finally we update the sayHello method in Impl_HelloUI :

HelloAnswerView av = new HelloAnswerView(tplEngine, osw);
av.setMessage(process.buildMessage(name));
av.setHistory(process.getHistory());
av.show();



11) update velocity template HelloAnswerView.vsl

(in directory server/main/conf)

<html>
<body>
The message is : $msg
<p>
<a href="helloservlet">One more time</a>
<p>
<table width="80%">
#foreach($elt in $history)
<tr>
<td>$elt.name</td><td>$elt.eventDate</td>
</tr>
#end
</table>
</body>
</html>


12) add new definition of datasource in tomcat configuration
See How to create a new datasource
The name of the datasource will be tutorial.


13) add name of datasource in configuration
edit file in abs/projects/tutorial1/deployment/localhost/replace.server.properties and add
a line

@@hibernate.datasource@@ = tutorial



14) Tuning mapping definition

The generated mapping hibernate files may be tune manually, for example our ID property for HelloHistory has been declared of "long" type but we have not specified how the unique id value for each object will be generated.
Two options may be discussed, in a truly mda way of working we will generate automatically a model of the mapping mecanism and update it to add more precise informations for final code generation. For now we decide on a more simpler way, just modify the *.hbm.xml file manually and generate a patch to be automatically applied after each new generation.

See http://sharengo.org/wiki/jsp/Wiki?How+to+patch+automatically+mapping+files+after+regeneration

We can modify the content of server/main/conf/org/sharengo/tutorial1/HelloHistory.hbm.xml to :

- specify for hibernate value of uncompleted id triggering calcul of a new id value. In our case default id value is 0.
- specify the algorithm to use to generate a new unique id (we want to use a sequence).


<id
  name="ID"
  type="long"
  unsaved-value="0"
>
<column
  name="I_D" 
  sql-type="BIGINT"
/>
<generator class="native">


We have added the "unsaved-value" attribute to the id element and chenged to value of attribute "class" from "uuid.hex" to "native".

The "native" value with no more precision tell hibernate for our postgresql configuration to look for a sequence name "hibernate_sequence". We have to add it in database.

15) compile and prepare deployment of all project
cd /home/$USER/test/projects/tutorial1
abs lib:all
abs deploy:build.war
16) deploy and test

Complete documentation for hibernate can be found here



HelloWorld again is mentioned on: abs tutorials


VeryQuickWiki Version 2.7.1 | Admin
Contact -