sobota, 11 maja 2013

JAVA vs .NET differences in practice

Hello long time I didn't write any post - it's time to change it! Today I would like to show you in practice some differences between .NET and Java. I mean in some primitives data types and other. Few days ago I was involved to some task with hashed users passwords. It was necessary for test Web Services (JAX-WS) whose connect with some other services using hashed password. We have some hash algorithm which was written in .NET and we would like to have the same result in Java but it wasn't working. OK let's have some details. This is some similar example in .NET


From .NET point of view everything is fine, but from Java is not! Why? - In .NET byte range is between 0-255 in Java is between (-127,128). This is 1 fail which I have discovered in code. Solution? - change the type from byte to int in Java and 1 problem fixed up.(But not really...). OK in Java there is a bitwise operator ^ (XOR) so - Convert.ToByte in Java is simple.
But XOR wouldn't work on int type, if I cast int to byte i will lost precision. Damn! There is another valuable information and comparison -Convert.ToBase64String and SHA512 algorithm. What is a problem with SHA512 algorithm in Java? There is to many code (in pure JDK) to write, but if I add just 1 library/jar Apache Commons Codec my code reduces from around 20 lines of code just to 1. But what about ToBase64String? I was searching in google and there was to many code to write the equivalent. In .NET the programmer sent to the method ToBase64String a whole table with bytes, in Java even when I tried get all bytes and "stick" them together using StringBuffer/StringBuilder or many various combinations and approaches and send it to equivalent Base64 method from Apache Commons Codec but without good effects. Time was running up, and I feel some solution in the air, just like a wolf which felt the blood. I didn't give up, what I did is just use online .NET/C# compilator and write the code and run it!

So my final solution in Java was really short and works well. After this I gained a lot in my new Team mates eyes. But they don't know that what kind a "lazy" bustard I am ;)

What is conclusion ? -Sometimes we are wasting time to find solution using another libraries, searching equivalent type in Java/.NET and a solution is just at your fingertips. Sometimes it's better to run any online compiler and run the code and see the result, save the output and use it in our code without wasting time to translate byte> 128 to int and many miracles in Java/.NET

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].*';