Doctrine orm:convert-mappings errors on primary key

Doctrine error orm:convert-mappings

Doctrine orm error like above? I have been there.

The solution I am providing is not that great solution but it can keep you going for a while.

A tool for generating entities for doctrine orm from the database table without primary key might complain as:

has no primary key. Doctrine does not support reverse engineering from tables that don’t have a primary key.

From the code base search for DatabaseDriver.php file:

Go to DatabaseDriver.php

Search for reverseEngineerMappingFromDatabase method in the given file.

uncomment the the following part as follows

           //if ( ! $table->hasPrimaryKey()) {
              //  throw new MappingException(
                //    "Table " . $table->getName() . " has no primary key. Doctrine does not ".
                  //  "support reverse engineering from tables that don't have a primary key."
                //);
            //}

            //$pkColumns = $table->getPrimaryKey()->getColumns();

Add the following line

$pkColumns = array();

Then search for method getTablePrimaryKeys and comment everything inside it and return empty array.

The above method will remove the has no primary key. Doctrine does not support reverse engineering from tables that don’t have a primary key. error when using entity generation tool.

Check if the variable is defined in symfony twig

In twig, specially for the templates being used by many controllers, there might be variables set by one controller but not by others.

Well one way to address could be to have dummy data on each controller and either to check for that dummy value and do something or just to print it

But, symfony has a way to check if the variable has made it to the template or not

{% if some.variable is defined %} what a world {% endif %}

so the usage of defined can rescue us.

Serve more than one site using virtualhosts on mac OSX

There would be a need for this or that reason to serve more than one site from apache. And this is quite possible using configuration inside the apache

First thing first
go to the apache config, by default it should be

/etch/apache2/apache.conf

And search for

#Include /private/etc/apache2/extra/httpd-vhosts.conf

Which, as you can see, is commented. Hence uncomment that by deleting the # infront of it.

The above task will allow us to mess around the httpd-vhosts.conf file that is in the extras directory.

Lets assume we have a site names siteVH which is residing in the directory /www/sitevh
and lets assume we have another site called siteAnother residing in director /www/siteanother

Now the goal is to access those sites independently. So we will create two different virtual hosts for each one and be able to access them independently


    ServerAdmin youreamil@domain.com
    DocumentRoot "/www/siteVH"
    ServerName sitevh.com
    ServerAlias local.site
    ErrorLog "/private/var/log/apache2/sitevh.log"
    CustomLog "/private/var/log/apache2/sitevh.com-access_log" common
    
        AllowOverride All
        Order allow,deny
        Allow from all
        Options Indexes FollowSymLinks
    

And the we will have the same duplicate values for the other one as well.

Then update the /etc/hosts to let know our local “DNS” :) to know what to do

127.0.0.1 sitevh.com siteanother.com

This shall take care of the whole thing.
One more thing, restart apache

sudo apachectl restart

Mommy look.. Upgrading jQuery breaks live :(

If you have upgraded jQuery to 1.9 then you would have dreaded Uncaught TypeError: undefined is not a function would be barking at you on your javascript console.

The fix
According to the jQuery site, you would need to replace live with on

Say if you have:

$('.selector-class-name').live('click', function(){console.log('
Applications programming is a race between software engineers, who strive to produce idiot-proof programs, and the universe which strives to produce bigger idiots. So far the Universe is winning.
Rick Cook
')});

Then you would have to change this to the new version using on as follows.
*The current on will be passed the event owner as a parameter so, we have to have a way to know the parent of the event owner, in this case the object with class selector-class-name.
If we don’t know the parent we can implement body tag

$('parent-of-object').on('click', '.selector-class-name', function(){
Applications programming is a race between software engineers, who strive to produce idiot-proof programs, and the universe which strives to produce bigger idiots. So far the Universe is winning.
Rick Cook
});

This shall take care of the problem.

JSF App slow with JPA connection

If you are working on Java Persistence API JPA on tomcat or any other web server this would be happening if you have multiple threads going off for connections.

The rule of thumb shall be to have one EntityManagerFactory and get EntityManagers out of it. Hence we would have one factory but multiple products that would take care of closing and managing them selves.

What are the signs:

1. Do you instantiate Persistence.createEntityManagerFactory(“name”) from multiple places?
2. What do you see on Process when you run

ps -aux | grep tomcat

Do you see multiple instances

If either or both of the above have yes, then here is the solution.

The first thing have single instance of ManagerFactory


package com.enderase.persistence;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
* Singlton implementation for EntityManagerFactory
*
* @author Kaleb Woldearegay<kaleb@gullele.com>
*/
public class HibernateUtil {
private static final EntityManagerFactory entityManagerFactory;

static {
try {
entityManagerFactory = Persistence.createEntityManagerFactory("jpa");
} catch (Throwable exception) {
//log your error here
throw new ExceptionInInitializerError(exception);
}
}

public static EntityManagerFactory getEntityManager() {
return entityManagerFactory;
}
}

Then make sure you are taking care of the instances of the EntityManagers that are created from the factory using

EntityManagerFactory entityManagerFactory = HibernateUtil.getEntityManager();
EntityManager em = entityManagerFactory.createEntityManager();

Make sure to close them appropriately after using them

This should pretty much take care of the problem

 

Check if there are three numbers a, b, c giving a total T from array A

I got this question while helping a friend on the course work. It is relatively simple question. But the way how it is approached can make a difference on efficiency.

The question is, given an array of numbers, you are to find if there are three numbers that would total the given number T.

If done in a very naive way, it can soar to o(n^3) like having three loops and checking the sum inside the third loop.. well.. this is a no no..

I have approached it in a log n ( for sorting) and n for (searching) approach..


package algorithm;

import java.util.Arrays;

/**
 * Given an array of integers, find if there are three numbers that would sum up to the 
 * number T
 * 
 * @author http://gullele.com
 *
 */
public class ThreeNumbersSummingT {
	public static void main(String[] args) {
		int[] test = new int[]{1,3,4,5,10,12, 18};
		ThreeNumbersSummingT summt = new ThreeNumbersSummingT();
		
		int[] response = summt.findThreeNumbers(test, 29);
		
		if (response.length > 1) {
			for(int num : response) {
				System.out.println(num);
			}
		} else {
			System.out.println(":( Couldn't find those three gems");
		}
				
	}
	
	public int[] findThreeNumbers(int[] nums, int t) {
		
		int[] indexes = new int[1];
		if (nums.length == 0 || nums.length <= 2) {
			return indexes;
		}
		
		//for primitive this would be quick sort so we have nlogn
		Arrays.sort(nums);
		
		int minIndex =0;
		int maxIndex = nums.length-1;
		int current = 1;
		while (minIndex != maxIndex) {
			if (nums[minIndex] + nums[maxIndex] + nums[current] == t) {
				int[] summingNumbers = new int[3];
				summingNumbers[0] = nums[minIndex];
				summingNumbers[1] = nums[current];
				summingNumbers[2] = nums[maxIndex];
				return summingNumbers;
			}
			
			int lookingFor = t-(nums[minIndex] + nums[maxIndex]);
			//if the number being sought is beyond the max, then jack up the min index
			if (lookingFor >= nums[maxIndex]) {
				minIndex++;
				current = minIndex + 1;
			} else if (nums[minIndex] + nums[maxIndex] + nums[current] < t) {
				current++;
			} else {
				maxIndex--;
				current = minIndex + 1;
			}
			
		}
		
		return indexes;
	}
}