duminică, 19 februarie 2012

Switching to VIM


I recently switched to linux OS and I started using vim for text editing. It seems strange in the beginning because people are not used with using so many keyboard commands for text editing. The good news is things are not that complicated as keyboard commands get more intuitive as one practices a little bit.

I decided to post a list of common used commands such that anybody can begin using vim by having this list close by.

Basic shortcuts that must be memorised - it will help remembering the rest of the commands:
$ - end
^ - start
i - insert
a - append
d - cut
p - paste
y - yanc (copy) 


Note: 
- the enumerated vim commands are used in COMMAND mode !
- commands are case sensitive

VIM commands:

Cursor handling:
Move to the bol  /  eol:            $ / ^
Move to the beginning of next word:    W ( w punctuation sensitive )
Move to the end of next word:        E ( e punctuation sensitive )
Move to the beginning of prev word:    B ( b punctuation sensitive )
Go to start / end of file:         gg / shift+g

Copy / Paste:

Copy a line:                yy
Cut a line:                dd
Paste a line after / before the current line:    p / P
Copy several lines:            shift+V y
Cut several lines:             shift+V d

Copy / Paste from clipboard
Paste from OS clipboard:        "+p
Copy entire file to OS clipboard:    :%y+

Undo:    U

Text insertion:
Insert text before / after the cursor:    i / a
Insert text at the bol / eol:        I / A
Open blank line below / above the cursor    o / O

Text deletions:
delete character at / after cursor    x / X
delete n characters starting at cursor    nx
delete 1 word:                dw
delete current line            dd
delete next n lines(including current)    ndd
delete from cursor to bol / eol        d^ / d$

Text replacement and substitution
Replace letter                                    r[new letter]
Substitute a letter with several:        s[letters] ESC
Substitute n letters with several:      ns[letters] ESC
Change rest of word:                        cw[word] ESC
Change n words:                               ncw[text]
Replace all characters in line           cc
Replace remaining chars in line from cursor    C

Short Legend:
bol        beginning of line
eol        end of line
chars    characters

Enjoy!

miercuri, 1 februarie 2012

When Hibernate criteria join is not possible


Joining with Criteria when no hibernate relationship is available:

This seems to be impossible. But there is a solution that could be applied for some situations:

Having two entities Company and Employee, where Employee.company is mapped to  Company.id, we have to obtain the companies with respect to one or more employees' attributes.


DetachedCriteria employeeCriteria = DetachedCriteria.forClass(Employee.class);
employeeCriteria.setProjection(Property.forName("company"));
employeeCriteria.add(Restrictions.lt("createdOn", date));

Criteria criteria = session.createCriteria(Company.class);
criteria.add(Property.forName("id").in(employeeCriteria));

List<Company> companies = criteria.list();

    The DetachedCriteria is not related to the session so it enables us to create a query outside the scope of the session, and it will be execued at some point using an arbitrary session (in this case it will get executed using the criteria, which is attached to a session).

In this case the DetachedCriteria instance is used  as a subquery; the criterion instance is obtained by using the org.hibernate.criterion.Property class.

Here is the query that hibernate built:

Hibernate: select this_.id as id1_0_, this_.created as created1_0_, this_.name as name1_0_ from companies this_ where this_.id in (select this_.company_id as y0_ from employees this_ where this_.createdOn<?)

One can see that the subquery retrieves the employee's company id as specified by the projection that was set on the employeeCriteria.

Having no hibernate mapping from Company to Entity, using this solution resulted in performing one call to DB.