Overriding hashCode and equals

Among several reasons and different situations, overriding hashCode and equals is important when using Hibernate and storing an object in a List, Map or a Set (nicely explained in this Hibernate doc). However, I don’t want to dig into that issue. I would like to show how to override this methods using Apache Commons.

When overriding equals, simply check whether it is instance of the Entity and then create a new EqualsBuilder and append as many fields as required to build a unique business key. Then return isEquals.

@Override
public boolean equals(Object obj) {
if (obj instanceof Entity) {
Entity other = (Entity) obj;
EqualsBuilder builder = new EqualsBuilder();
builder.append(getName(), other.getName());
// append as many as required to build a unique business key
return builder.isEquals();
}
return false;
}

When overriding hashCode, simply create a new HashCodeBuilder instance and then append as many fields as required, but be consistent with equals.

@Override
public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder();
builder.append(getName());
// be consistent with equals
return builder.hashCode();
}

Cheers!

Comments

Unable to locate Spring NamespaceHandler for XML schema namespace

I have seen this Exception in several forums with no solutions. Actually, I could not find solutions at all. It happened to me as well:

21:46:18,850 ERROR [ContextLoader] Context initialization failed org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx]
Offending resource: class path resource [applicationContext.xml]

What is going on? Spring defines several namespace handlers by default in spring.handlers file. Among them, we have http\://www.springframework.org/schema/tx=org.springframework.transaction.config.TxNamespaceHandler. TxNamespaceHandler comes shipped in spring-tx.jar. Let’s include it in the Maven POM:

groupId: org.springframework
artifactId: spring-tx
version: 2.5.1

So, when I imported this dependency, the exception was resolved.

For more information, please refer to this post in InfoQ: Spring 2.0: What’s New and Why it Matters.

Comments

About Trading system aid

openBiz Solutions Trading System Aid is a proof of concept of a full stack of open source technologies encompassing from Infrastructure to Java frameworks. It is using the following Java Open Source Frameworks:

  • Spring 2.5 for presentation (Spring MVC), transactions (Spring AOP with HibernateTransactionManager), persistence integration with Hibernate 3.2 and security using Acegi.
  • Use of EHCache for caching, Apache Commons provides reusable components and Log4j logging capabilities.
  • In the presentation layer, Sitemesh provides web­page layout and decoration, DisplayTags gives reusable presentation components and Itext PDF services.
  • The inclusion of Yahoo UI APIs intends to supply some reusable components in the front end side.
  • In the testing world, JUnit and JMock give unit testing another dimension.

The source code could be found and downloaded from the Trading System Aid Google Code project. The project could be built using Maven2.

Comments

Spring and LDAP

I came across this interesting article regarding Spring and LDAP configuration using the Spring LDAP Template. Worth reading and keeping just in case:

http://springtips.blogspot.com/2008/06/spring-ldap.html

Comments

Creation methods and Factory methods

I was reading ‘Refactoring to Patterns’ by Joshua Kerievsky and I came across with the ‘Replace Constructors with Creation Methods’ refactoring. I would like to share the distinction made between creation and factory methods cause most of the time any method creating an object is called factory method and it is not always like that.

I agree with the distinction proposed in the book:
(1) Any method creating instances is a Creation Method (static or non static)
(2) Then, all factory methods are creation methods
(3) However, not all methods that are creating instances happen to be factories

For example, in Effective Java by Joshua Bloch, he mentions in Item 1 Consider providing static factory methods instead of constructors showing as an example the static valueOf from Boolean class. So, taking into account the previous difference this would not be called a factory method but a creation method.

Let’s have a look to the definition of the factory method pattern from wikipedia for example: “The factory method pattern … it deals with the problem of creating objects (products) without specifying the exact class of object that will be created.”

As we can read, the factory method doesn’t know which object is going to be created, in the signature we could have an Abstract class and return subclasses or interfaces and return implementations.

What is your opinion about this issue?

Comments

SpringSource Application Platform

The other day I was talking about the new Spring platform with a friend and we were trying to get the novelty out of this new platform. Let’s have a look to the “new stuff”:
- Modularity. As mentioned in SpringSource Application Platform released… back to basics?, modularity has always been a way to minimize complexity in systems. Nothing new here.
- Pluggable Modules. The idea of having a sort of microkernel and adding modules it is not new, for example Jboss does this with pluggable MBeans.
- Profiles. Again taking Jboss as example, we have different configurations depending on the type of server that we want. What is more, Tomcat is running as a pluggable MBean.
So far nothing new… However, the inclusion of the OSGi platform specification (based on Equinox implementation) provides some nice-to-have features:
- OSGi bundles. Any OSGi-compliant bundle can be deployed directly into the Platform
- Dependencies resolution. OSGi-compliant bundles can take full advantage of on-the-fly provisioning for any dependencies referred to by Import-Package and Require-Bundle. Third-party dependencies will be installed on-the-fly as needed. So you could specify that a bundle needs other bundle or to import a library.

To find out more about OSGi technology. And talking about web apps and OSGi.

So… what do you think? Since I haven’t spend much time researching… what is your opinion regarding the new features? pros, cons…

Comments

What is the future of MySQL Server?

After Sun acquired MySQL, they had a look what is the current status and what needs to be achieved to improve it. Michael Widenius prepared a nice presentation about some of the features that are not working properly, how to fix it and when could they be achieved…. in one phare… the future direction of MySQL.

Some of the issues that need to be addressed are:

  • Threads. Thread handling (pool of threads)
  • Memory as a resource.
  • Metadata. Have you ever tried to alter a table while the database is under heavy load?
  • Privileges. No roles, lack of pluggable authentication methods
  • Pluggable Storage Engines.
  • Parser improvements.
  • Base code modularity. It is difficult to modify, understand and lack of documentation. Is this a general issue of many open source projects? (another day we could have a look to this one)
  • Stored procedures/triggers improvements.
  • Replication. It simply has many problems. I used this feature to replicate approx 20 machines and it is unreliable. Lot of work needs to be done.
  • Table names need to be in lower case in file names.
  • Open source project feedback, give commit and decision power to developers
  • Release policy

Comments

SpringSource Application Platform released… back to basics?

“Making it modular changes the game”, Rod Johnson said.
“Modularization is a conventional and quite useful mechanism for reducing complexity that transcends programming languages, environments, and even paradigms.”, Patterns, Models, and Application Development by by Julio Sanchez.

So… back to basics… lets get modularization and cohesion inside each module. Decreasing cohesion between modules… sounds easy.

What else did he say?
” Modularity includes in development the ability to quickly start up the server and have faster round-tripping, seeing modifications quickly reflected on the server. It allows creation of artifacts, and the ability to break applications into modules. If you need to deliver patches you can modify just parts of the server without having to re-start.

This is very important in a world where virtualization is becoming increasingly important. Also the ability to modularize applications means you have the ability to re-release and update business logic without taking the application down in production.”

So these are the basic concepts of the solution proposed. In further posts I will try to analyze the solution itself.

Comments (1)

Moving to Spring Security 2.0

Matt Raible wrote an useful tutorial about upgrading to Spring Security 2.0, former Acegi. Whenever I have a few hours, I am going to update my try-it-all-app to “feel the changes”.
To check the changeLog, announcements, downloads.

Comments

Designing for Testability: TestNG and JUnit

TestNG and JUnit are 2 of the most popular testing frameworks in the market at the moment. I found this article comparing both frameworks interesting and would like to share with all of you. Thanks to Lijin.
On top of that, I would like to mention a nice presentation about TestNG posted in InfoQ called Designing for Testability.

Comments

« Previous entries