Friday, 16 October 2015

Add column with foreign key in mysql

Add only a constraint on a column

ALTER TABLE `SCHEMANAME`.`TABLE1` 
ADD CONSTRAINT `FK_TABLE2_COLUMN` FOREIGN KEY (`FK_COLUMN`)  
REFERENCES `SCHEMANAME`.`TABLE2`(`PK_COLUMN`);
 

Adding a column with constraint

ALTER TABLE `SCHEMANAME`.`TABLE1` 
ADD COLUMN `FK_COLUMN` BIGINT(20) NOT NULL, 
ADD CONSTRAINT `FK_TABLE2_COLUMN` FOREIGN KEY (`FK_COLUMN`) 
REFERENCES `SCHEMANAME`.`TABLE2`(`PK_COLUMN`);
 

Monday, 20 July 2015

Dependency Injection and its Spring overview.

Dependency Injection

Java components / classes should be as independent as possible of other Java classes. This increases the possibility to reuse these classes and to test them independently of other classes(Unit Testing). To decouple Java components from other Java components the dependency to a certain other class should get injected into them rather that the class itself creates / finds this object.
A class A has a dependency to class B if class uses class B as a variable.
If dependency injection is used then the class B is given to class A via
  • the constructor of the class A - this is then called construction injection
  • a setter - this is then called setter injection

The general concept between dependency injection is called Inversion of Control. A class should not configure itself but should be configured from outside.
A design based on independent classes / components increases the re-usability and possibility to test the software. For example, if a class A expects a Dao (Data Access object) for receiving the data from a database you can easily create another test object which mocks the database connection and inject this object into A to test A without having an actual database connection.
A software design based on dependency injection is possible with standard Java.
Spring just simplifies the use of dependency injection by providing a standard way of providing the configuration and by managing the reference to the created objects.

Spring Overview

The Spring Framework is a very comprehensive framework.
The fundamental functionality provided by the Spring Container is dependency injection. Spring provides a light-weight container, e.g. the Spring core container, for dependency injection (DI).
This container lets you inject required objects into other objects. This results in a design in which the Java class are not hard-coupled. The injection in Spring is either done via setter injection of via construction injection.
These classes which are managed by Spring must conform to the JavaBean standard.
In the context of Spring classes are also referred to as beans or as spring beans.
The Spring core container:
  • handles the configuration, generally based on annotations or on an XML file (XMLBeanFactory)
  • manages the selected Java classes via the BeanFactory

The core container uses the so-called bean factory to create new objects. New objects are generally created as Singletons if not specified differently.

Thursday, 16 July 2015

Working with Apple Notifications using javapns plugin and Java 7

If you are trying to push notifications to apple devices with jdk 7 and using javapns plugin for pushing messages to Apple Server, You will end up getting the following error by using the p12 file generated using the keychain access in MAC.
javapns.communication.exceptions.InvalidCertificateChainException: Invalid certificate chain (Received fatal alert: certificate_unknown)!  Verify that the keystore you provided was produced according to specs...
    at javapns.notification.PushNotificationManager.sendNotification(PushNotificationManager.java:410)
    at javapns.notification.PushNotificationManager.sendNotification(PushNotificationManager.java:350)
    at javapns.notification.PushNotificationManager.sendNotification(PushNotificationManager.java:320)
    at javapns.Push.sendPayload(Push.java:177)
    at javapns.Push.alert(Push.java:47)
 The simple solution is, just convert APNS .p12 certificates to JKS file with jdk 6 keytool and again convert JKS back to .p12 format with jdk 7 keytool.

Convert .p12 to jks:

keytool -importkeystore -destkeystore C:\Push_jks.jks -srckeystore C:\Push_j6.p12 -srcstoretype PKCS12

Convert jks to .p12:

keytool -importkeystore -srckeystore F:\Push_jks.jks -srcstoretype JKS -deststoretype PKCS12 -destkeystore C:\Push_j7.p12


Please provide the complete path of the files in place of C:\Push_j6.p12, C:\Push_j7.p12,F:\Push_jks.jks

Note : for reference i have used windows file paths in the above example.