Git : How to fix issues in a merged branch

Sometime back we faced a peculiar problem in our project where we have to fix some issue introduced due to a merged branch. To explain this problem first I’ve to explain the way we manage branches. As we entered in the maintenance mode of project there was a dire need of fixing issues and working on change requests in isolation. Whenever we need to fix an issue we cut a issue-branch out of main branch fix that issue in isolation and after verifying and making sure the issue is fixed we merge that issue-branch back in main branch. The issue we faced with the approach is that after merging an issue branch with the main branch sometimes we came across some regression bugs due to the issue branch. There were two possible solutions to overcome the problem.

First solution is to fix the issue in issue branch and merge the issue branch again with master. This solution can work but the problem with this approach is that till the time you haven’t fixed the issue introduced due to issue branch and merged it back with master branch you can not create a new issue-branch and much bigger problem is you can’t release.

The second solution is if we can somehow revert the merge of issue branch with main branch and then fix the issue in issue-branch, after that merging the fixed issue branch with the main branch. This approach seems to be straight forward and more logical. Git comes up with a cool command git-revert which can revert existing commits and even revert merge of another branch. I’ll talk about the solution in the next blog 😉

My experience with Git

Few days ago I came to know about git, so to give it a try I created an account on github. To those who doesn’t know much about it GIT is a fast version control system :). Since I’m using windows I’ve to download a git client, so the first google result that came on typing “git window” was “msysgit”. Msysgit comes with 2 options “gitbash” a command line utility and git ui so you can use any of them, for initial learning perspective I preferred GitBash.

I’ll not explain the concepts of git as there is lot of information available on the net
http://en.wikipedia.org/wiki/Git
http://git-scm.com/

So first I’ve to create a remote repository. GitHub provides online project hosting using git, they have different plans as according to your needs. I’ve created a free account so I can create public repostiories, which means anybody can checkout the code and you can allow people to modify the code. If you want to create private repository then you have to create a paid account, they have multiple plans as per your need https://github.com/plans. So my user at github is sandy724.

In next blog I will discuss how I’ve used GitBash for synching up the code with remote repository

Equals implementation for Lazy loaded objects in Hibernate

Most of you already know about the standard way to implement equals and hashCode for a class. As an example lets consider a User class


public class User {
private Long id;
private String name;
private String address;
}

If we consider Id as unique identifier for logical equality of users then the equals and hashCode method can be written over “id”, as given below


@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}


@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}

Now lets assume User is an entity thats managed by hibernate and is lazily loaded. So actually you will be interacting with the proxy(HibernateProxy) of User and not the actual User class.

Now in this the equals implementation of User class will fail. If we check for equality of normal instance of User class with the proxy instance of User the equals method will return false. The reason for failure is the class check i.e


if (getClass() != obj.getClass())
return false;

Since the class of Proxy user object is not User but a HibernateProxy class.

To overcome this problem hibernate comes with a utility class HibernateProxyHelper which is used to get the actual class if the object passed to it is of type HibernateProzy. The above piece of code can be replaced with


if (  HibernateProxyHelper.getClassWithoutInitializingProxy(obj) != getClass() ) {
return false;
}

Another problem with the implementation of equals in case of proxy objects is we cannot directly access the fields i.e


if (!id.equals(other.id))
return false;

as proxy objects can access only the methods of the underlying object so instead of directly accessing the field we have to use the getter method


if (!id.equals(other.getId()))
return false;

So the updated equals implemention that should work perfectly fine in case of proxy objects also will be as given below


public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (  HibernateProxyHelper.getClassWithoutInitializingProxy(obj) != getClass() ) {
return false;
}
User other = (User) obj;
if (id == null) {
if (other.getId() != null)
return false;
} else if (!id.equals(other.getId()))
return false;
return true;
}

XStream Int parsing issue

This blog is about issue in parsing int data from an xml.
While working with XStream I experienced a problem, if the xml comprises an int data beginning from 0 XStream was throwing an Exception ConversionException. On digging deeper I patterned out that the converter(IntConverter) used to convert the string value into corresponding integer value was causing problem. If the string had 0 as prefix then that string is treated as an octal value and it try to decode it as an octal value. That’s why when a value such as 089 is processed by this converter, the converter processed that value as an octal and tried to decode it and since 8 & 9 are not in octal base that’s why converter was throwing this exception. To get the better of this problem you can either have the numeric values in xml without following 0s or you can write your own converter.

Template design pattern in action part1

So finally I got time to complete the blog 🙂

I’ll discuss about how we can solve the problem using templating design pattern. So what we have done, we have written a Template class “CacheTemplate” having a method getCachedData.
getCachedData takes 2 parameters
key: Key for which we have data cached
cacheCallback: If we don’t have data cached then we will call this cacheCallback to get the data and store it into cache.


public class CacheTemplate {
private CacheProvider cacheProvider;
public  T getCachedData(String key,
CacheCallback cacheCallback) {
T data = (T) cacheProvider.get(key);
if (data == null) {
data = cacheCallback.cacheOperation();
cacheProvider.put(key, data);
}
return data;
}
}

Now taking forward the example taken in previos blog, let us apply this template to the “getPersons” method


public List getPersons() {

return cacheTemplate.getCachedData("persons", new CacheCallback>() {
@Override
public List cacheOperation() {
_getPersons();
}
}
}

private List _getPersons() {
persons = //Business logic to get persons;
return persons;
}

Now if you compare the current implementation with the previous implementation and check the concerns we had they all are resolved.
1.) Now our business logic of retrieving the persons is at one place.
2.) Now we have a generic implementation of managing cache.