Question: I am using hibernate 3 using spring 3.5 for a SaaS application. I am expecting upto 10-15 customers , not more. I do not want to implement separate db or schema per customer as its too complicated and costly for a small enterprise like mine. I am currently using a multi-tenant strategy which works fine for a host of small features. Here is the use case where my design fails:

For reporting feature each customer will have a different table for data (because of various reasons like legacy, source of data etc). Table structure differs and so does service/controller behaviors. 

I am currently planning to create separate Controllers, Services (DAOs), etc for each customer, thus mapping each of such customer tables with a separate hibernate class. But this approach is not clean and for every new customer I add (which is not that often though), I would need to add its table, and also code a hibernate entity class mapped to the new table, which is not ideal as it needs coding. Is there a way to manage/map such dynamic tables using hibernate which gets added when a new customer is added ?

Answer: 
You can use [`Array.prototype.reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) function like this

```
var accessString = "sections.general";

console.log(accessString.split(".").reduce(function(previous, current) {
    return previous[current];
}, inputMapping));

```

**Output**

```
Some general section information

```

If your environment doesn't support `reduce`, you can use this recursive version

```
function getNestedItem(currentObject, listOfKeys) {
    if (listOfKeys.length === 0 || !currentObject) {
        return currentObject;
    }
    return getNestedItem(currentObject[listOfKeys[0]], listOfKeys.slice(1));
}
console.log(getNestedItem(inputMapping, "sections.general".split(".")));

```
You don't need to use `eval()` here. You can just use `[]` to get values from an object. Use a temp object to hold the current value, then update it each time you need the next key.

```
function getNode(mapping, name) {
    var n = name.split(".");

    if (n.length === 1) {
        return mapping[name];
    } else {
        var tmp = mapping;
        for (var i = 0; i < n.length; i++) {
            tmp = tmp[n[i]];
        }
        return tmp;
    }
}

```