Thursday, December 31, 2009

Hibernate: Group By and Count

One of the best features of Hibernate are the Criteria Queries, which allow you to define parametric and easy way to query your data. In this post, I would like to summarize the usage of the class Projections allow you to group your result set by applying a projection to your Criteria query. For example, if you like to "group by" and to "count" the number of instances for a parameter "par", you can use the following lines of code:
Criteria criteria = criteria();

ProjectionList projList = Projections.projectionList();
projList.add(Projections.groupProperty("par"), "parAlias");
projList.add(Projections.rowCount(), "countAlias");
criteria.setProjection(projList);

criteria.setResultTransformer(Transformers.aliasToBean(ObjDto.class));
Some remarks:
  • It is important to underline the usage of the "groupProperty" method to apply the grouping on the parameter "par" and the usage of the method "rowCount()" to count the number of instances (i.e. SQL count(*))
  • It is also fundametal to note the usage of the aliases in both methods: countAlias and parAlias. These aliases are used by the ResultTransformer
  • The Data Transfer Object (DTO) class ObjDto contains only two attributes: countAlias and parAlias
  • Finally, it is important to see the usage of the setResultTransformer to copy the result of the query on our DTO bean class called ObjDto
The Projection class allows you to aggregate and group your Criteria queries in an easy way. Morevoer, they can simplify and reduce your lines of code!

Monday, December 28, 2009

Log4j and DailyRollingFileAppender

Log4j is a cool Java library which helps inserting log entry in the code. One of the best features of this logging library is the usage of the DailyRollingFileAppender for archive purposes. This appender allows you to roll the files with the user choosen frequency.
The configuration is quite simple, according to the manual:
# Default Log - Catch all
log4j.rootLogger=DEBUG, stdout, logfile
# Console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c.%M:%L] - <%m>%n
# File
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.File=${log.dir}/${log.apptype}/log.log
log4j.appender.logfile.DatePattern=.yyyy-MM-dd

As you can see, the log files are rolled with the layout defined by the DatePattern property: .yyyy-MM-dd. This should follow a valid SimpleDateFormat convention. However, in this way the old log files are stored like: log.log.2009-01-01, log.log.2009-01-02 and so on, which means a different extension every day! In order to solve this issue, it is possible to escape the literal with the quotes in the DatePattern property, and insert the extension in the pattern. This means that the DatePattern property can be like this:
log4j.appender.logfile.DatePattern='.'yyyy-MM-dd'.log'

By following this approach, each log file will be archived like log.log.2009-01-01.log, log.log.2009-01-02.log.

Wednesday, December 23, 2009

Windows cannot load the locally stored profile

I was working on a server where there was the following error after each logon:
Windows cannot load the locally stored profile: Insufficient security rights
or a corrupted local file. Windows has logged you in with a temporary profile
any setting you make will not be saved.

Searching on Google, I found that the problem is related to the fact that the user has been deleted and recreated in the system. Therefore, in order to solve it, you need to follow this procedure:
  1. Run "WHOAMI /USER" to determine your SID
  2. Check the ProfileImagePath value in the following registry key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsNT\Cu rrentVersion\ProfileList\
  3. Delete the other SID that is pointing to the same ProfileImagePath

This solved the problem!