Category Archives: developing

m2e

m2e is a really cool plugin for eclipse to run maven commands… i think it is mandatory for developing maven project with eclipse… and normally the installation is easy… but due to missing requirements i got really pissed and have still didnt figured it out…

but today is a good day… http://dev.eclipse.org/mhonarc/lists/m2e-users/msg01880.html

ubuntu on win7

so finally i started to develop at home again… my main focus is to create small simple projects to improve my daily, private work and learn some new things… primarily i need to learn linux things…

at work i am developing on WinXP for unix systems… so without unix knowledge, its kinda stupid… so i decided to learn these things at home…

so i have setup an Ubuntu on a VMPlayer on my Windows7 PC… and it works 🙂

dont try VirtualPC… with the correct manual its kinda easy to install it, but all the manuals contains sort of hacks… and ubuntu doesnt run really useful… in my opinion… with VMPlayer it was much better and simpler to install and mainly the only problem was the Gnome Shell, which didnt work… 🙁 but this is a project for the future 😛

cygwin with scp

We work on a windows pc on a java application. Windows because our company just provied us with these computers and installing a linux system is highly forbidden… but still our java applications will be deployed on linux machines… a few of us developer have 2nd pc for development, but i m not one of these lucky guys… ok vm ware is a solution, but i m kinda lazy… and currently see no downside on developing on windows… ok there is one… deployment…

we have a deploy script 🙂 .sh 🙁 and cygwin out of the box has no scp intalled… following manual explains, how this can be fixed 🙂  http://www.question-defense.com/2010/01/07/how-to-install-scp-and-ssh-on-windows-7-using-cygwin

jpa nativeQueries

Sometimes JPQL just isnt enough… JPQL is rendered differently then expected, ie nested selects dont work… so native SQL queries need to be done…

main problem is the return value. It is possible to define SqlResultSetMappings, but these where kinda confusing. had problems with primary keys… so without these structures, a typeless list of object-array is returned:

   final List result = entityManager.createNativeQuery(“SELECT a, b FROM table”).getResultList(); 

for each row an Object[] is put into the list.The Array contains Objects of a and b.

logging eclipselink

the big downside of JPA is in my opinion the JPQL… normaly it is pretty, pretty,pretty good… when only doing simple stuff. As soon as it get complex, where i first try it in SQL and then rewrite it in JPQL it gets bad… as long as no error has occured, it didnt output the from-JPQL-rendered-SQL… and i actually tried to debug it, tried to access it in java… but didnt tried to change the config…

<property name=”eclipselink.logging.level.sql” value=”FINE” />
<property name=”eclipselink.logging.parameters” value=”true” />

these two lines enable the logging of SQL… … bah…

SQL having

I needed to select an object-id of a row where a value reached a max value. Kinda confusing, so example:

SELECT id, MAX(value) FROM table
GROUP BY id

simple… and now i needed to check, if the value is bigger then something… lets say 5:

SELECT id FROM (
       SELECT id, MAX(value) AS bigValue FROM table
       GROUP BY id)
WHERE bigValue > 5

and yes. A join would be fine too… but actually i needed this query in JPA. and JPQL can’t handle nested selects… so a different solution was needed: HAVING! having is a select on a group…

SELECT id FROM table
GROUP BY id
HAVING MAX(value) > 5

cool 🙂 JPA didnt handle the HAVING… 🙁

logging in java

since the last time i developed java(like 3 or 4 years ago) logging didn’t change very much… in my opinion… out project is using log4j… or thats what i thougth… there is a xml config file, which tells the logger where to put which level how… and actually we still use log4j… but its different… its slf4j, which a generic framework, that uses loggers… so whatever we use… we always talk with slf4j and configuration defines the effectiv log… chaning the logging framework is easy and has no impact to the code… on an other project we use logback… which i first didn’t realize… which is, in my opinion a really good thing…

so logging did change, but for me as a developer, it actually didnt… it still feels like the same… and thats not because slf4j, log4j and logback is from the same developer team…

jpa type safe entityManager calls

testing our jpa daos always worked successful… but i think this was mainly, because we filled during test the data on the database and got the data and tested it in the same transaction. therefore the data might be still stored in cache and never an actuall db call was made… this needs to be analysed and tested in near future… but thats a different blog entry… hopefully…

this blog entry is about the problem, which occures, when the db actuall is called 🙂

We have a GenericDao and all find all calls worked… and we have object based daos… following example, with an random object… lets take Person:

    public List<Person> getPersonByName(final String name) {
        @SuppressWarnings(“unchecked”)
        final List<Person> list = (List<Person>) entityManager
                .createNamedQuery(Person.GET_PERSON_BY_NAME)
                .setParameter(“name”, name)
                .getResultList();
        return list;
    }

This resulted in an exception… dont have the Exception here, but was something with an Object[] missmatch… and something about an non initialitzed EntityManager… the problem was the @SuppressWarnings(“unchecked”): or the warrning, which we just ignored… getResultList() returns a List… and casting List to List<Person> just doesnt work… whats the solution? Simple… just tell the named query, that kind of type it is… so it returns an List<Person> instead of the List…

 .createNamedQuery(Person.GET_PERSON_BY_NAME, Person.class)

and the @SuppressWarnings(“unchecked”) isnt used anymore 🙂

mockito matchers

mocking stuff with mockito is cool… take a function, setup expected input values and define a return result… easy when the input value is fix… and can not be bigger then…

ok what was my problem… i had a simple function, which i needed to mock: (a little more complex, with acutally complex objects and enums, but it would have in a problem with following aswell)

int methodName(String s, int i);

the problem was that the return int is similar then the i is greater then or equal a value… lets say 3 (which actually was the case in my case, but who cares…)… so the mocks:

Mockito.when(this.myMock.methodName(“Hello”, 1)).thenReturn(7);
Mockito.when(this.myMock.methodName(“Hello”, 2)).thenReturn(23);

Greater then is:

Mockito.when(this.myMock.methodName(Mockito.eq(“Hello”), AdditionalMathcer.geq(3))).thenReturn(63456);

The problem or the confusing thing is, when you add an Matcher, all parameters need to have a Matcher… :/

date and calendar

java.util.date was a really  logic thing for me… but now, all cool functions on Date are deprecated… crap… ok with Calendar it makes object-oriented-wise more sense… but how the hell do i get from an Date to the Calendar? Calendar offers only an getInstance() function, which actually sets the Calendar to current datetime… not really the thing i wanted…

Calender cal = Calender.getInstance();
cal.setTime(date);

is the solution… crap…