piątek, 1 marca 2013

Quering and queries with parameter(s)

Hello!

I would like to show you, how to retrieve object from db using queries with parameter(s).

In this example we are searching a user with name "John". This is HQL example where we used only 1 parameter. Please remember that we are using a properties name from User class (I mean firstName, lastName, id -  you cannot use directly column names from your database). We are using now "labeled paramaters", but we can use questions marks (?), but  remember about type of this parameter and their sequence in query.



This above snippet we have the same result using question mark. Rembember that postion of parameters are 0 indexed.

In example above, we used an indexed typed parameter (?). You should try to set this parameter to String, BigDecimal etc and examine what will be execute of this code. Sometimes query will be executing fine, when we try to find user with ID= 4 and we set a parameter like a String("4")

Query query = session.createQuery("from User u where u.id =?").setString(0, "4");


This is depend on database vendor, but you shouldn't using query in this way.

You can try to build queries with 2 and more parameters it's very simple.


When simply query with parameter is not enough....


Now, let's imagine that you would like to select all of your users with the first letter of their name is M and their  surname begins with S or B.
Believe me there is a Query but it won't be look nice, there is a rescue .... Criteria !

Criteria iterface is very handy, you can find there many SQL equivalent for example:

- like
-between
- in ( Used in collection, but if you try to find String using "S%" it won't work, it takes full String like "John" and try to find a name in your table contains excatly this String, so any SQL wildcard "doesn't pass the exam here..")
-notNull / isNull and many many others!

I mentioned about method Restriction.in.

Let's look at this example:



Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.like("firstName", "M%")).add(Restrictions.in("lastName", new String[] { "S%", "B%" }));


From the logic point of view this Criteria query should return me the same result like above from snippet, but it's not true. Method Restrictions.in  is searching iterating through every record searching exactly fits String : S%  and after that B% (in meaning String as a String not like a SQL Wildcard).

But if you  modify above Criteria query to the following:



Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.like("firstName", "M%")).add(Restrictions.in("id", new Integer[] {6,7,8,9}));

You will get the output.

Maybe you don't know but MySQL doesn't support charlist wild cards, this mean that query like this

SELECT * FROM User
WHERE lastName LIKE '[KTM]%'

Will cause an error, instead of you should use a regexp something like follow:

select * from User where lastName REGEXP '[KTM].*';










piątek, 8 lutego 2013

JBoss Tools & Git - must needed!

Eclipse vs NetBeans (holy war)


I started programming using NetBeans IDE (6.0 as far as I remember), I thought it's the best IDE on the planet (I'm not lazy but I like comfort and utility) my favourite design pattern was "click click... and done!" 
(I hardly ever used InteliJ IDE - so I don't have full comparison between all of them)
My first touch with Eclipse Helios was in 2009 and I said - "I will be never programming in this piece of  s**t !!!"  
Don't you ever say never! In 2012  I have discover Eclipse (Juno) once again - and whoaa! I am fall in love in it! But I hate default Eclipse colors, themes and other, thankfully I installed some plugins, I changed some setting and now my Eclipse has the same colors& themes like NetBeans :) it looks more nicer than "default Eclipse"
But let's go back to the merits.
Eclipse is cool, but his power is in the plugins & features. I fully recommend you to download and install 2 very useful plugins (I will use those plugins in Hibernate tutorials):
  • JBoss Tools
  • GIT 
The best way to install them just open your IDE Eclipse click from toolbar HELP-->Eclipse Marketplace...  enter the caption "GIT" in window which appear, and search.
This should look as follows:


(I have already installed GIT - this is the reason why I have a caption uninstall)

Go ahead and click "Install" after that restart your Eclipse IDE and viola! (new features are available if you choose "Window --> show view --> GIT" (I will mention later in Hibernate tutorial about GIT usability)

It could be good for you to install GIT BASH / GUI for your PC - it is useful for see differences in your repository. You can download GIT from here.
If you are not familiar with GIT - you can very fast to learn more. Just try GIT Tutorial Online

Please do the same with JBoss Tools (Eclipse Marketplace), but please be aware - don't install all stuff from there most of them probably you will never be used. I installed JBoss plugins for Hibernate, Maven, JSF.
That's all you need for now, let's begin Hibernate Tutorial!