Saturday, 20 October 2012

RememberMe service in spring security is not working

In the Spring security they provide 2 ways to use the rememberMe service.

  1. In the rememberMeService Definition[Configuration file], "alwaysRememberMe" property should be true. First time when user tries to access the secure URL it will take user to login page. After first login, Application will not ask user to login again until user hits logout link or cookies has been deleted or expired.
  2. Other way we can provide check box on login screen to give freedom to user to choose, does he/she wants to save login or not.To make it work check box should be specified with name "_spring_security_remember_me" and value="true".Remember me service would not save user's credentials until user hits the checkbox.

Its working for me..

Friday, 19 October 2012

Association, Aggregation, Composition

There are 3 words which confuses every developer. So many time people ask in the interview.

Association keyword show the relationship between more than one entity, class or Object. The Relationship can be one to one , one to many and many to one.
it means two class are associated with each other via some relationship that called association.
Association can be shown by a "Arrow" sign.

Example: 1. Faculty and students are having association.
2. Students and library is having association.

Aggregation is a special form of the association. It shows "has-a" relationship of the object. A Objection has a another object.Like class has student object. If two object having "has-a" relationship then only we can get Aggregation.


Composition is a special form of the aggregation. when one object contains another object and contained object cann't exist without the container object,called composition. Like A student object cann't exist without class object. A relationship between class object and student object called composition.

So basically there is a difference between aggregation and composition is, in the composition contained object cann't exist or alive without container object but in the aggregation it can exist.

Tuesday, 11 September 2012

LinQ in java

In c# there is feature called LinQ, Using LinQ we can execute a query to the Object.
Suppose we have a animal object which contains name of the different animals and respective id.Now I have to find the all the animal whose name contains alphabet 'o'. In C# it is too easy through LinQ, but if you are a java programmer it is little bit difficult and time consuming, we have to write 10-15 line of the code for this functionality.
So make it easy in java i created a jar which having the classes,these class will take care of the code, we just have to write a query that all we have to do. If we use this jar file we same LinQ similar kind of functionality in java. Now i going to show you one example, How we can use the jar file.

I have a class with the name of Animal, containing name of the animal and id.
see the below snap-shot of the class

As the snap shot we have two attribute as name and id. we have a parametrized constructor having two argument name and id.
Now time to copy the jar file into the class path.
Create one more class with any name , i created
In this class we have import static function of the CollectionFilter class which is part of the jar.

Create the animal Objects.

Animal is the object on which we have to execute the query. our aim was to get the animal name which contains alphabet 'o'.

Write below query

Above query will return list of animal object whose name contains alphabet 'o'.
form , where and contains are static method of CollectionFilter class.getName is the method of the Animal class which returns name.
If we write query based on the id then we have to pass getId method name.

We can perform other operations like Equals ,Not,Greater than, Less than and Is Null operations. we also can user Or and ANd operaor in the Query.

Equals Operation -> List a = from(animal).where("getName", eq("cat")).all();

Not Operation ->List a =from(animal).where("getName", not(eq("cat"))).all();

Less Than -> Only work with number.

Greater Than -> Only work with number.

OR Operator -> from(animal).where("getNme", eq("cat")).or("getId", eq("7")).all();

AND Operator->from(animal).where("getName",contains("o")).and("getId",eq("7")).all();

You Can download the jar Click Here

Sunday, 10 June 2012

One to many relationship in hibernate using annotation -2

Today i am going to tell you how to do one to many relationship in hiberantae by creating a intermediate table. Before going through this post first go through the First Post then only you can understand this post. So in the earlier post i expained you how to do one to many relationship without using the intermediate table.
To develop the one to many mapping using intermediate table we have to make few changes in the Student class and course class.

Please make below changes in the student Class-

Here we add one more annotation @JoinTable to make a intermediate table.

For the course class remove the studentId field.

That changes only we have to make.

Wednesday, 6 June 2012

How to find a String is a circular representation of other String

Many Interviewer may ask this question, "How will you find a string is circular of other String." Suppose we have two strings A and B. we have to check or tell that the B is circular of String A or not. A and B can be any string. If we think for a while , we will get the different solutions like using nested loop we can compare each alphabet of B and A. Other way is we can use the linked list. For both approaches time and space complexity would be high. If you told above solution your answer would be right but it is not optimized . Interviewer might be expecting optimize answer, our answer won't be impress to interviewer.

Now we have to think how we can optimize the time and space complexity for a given problem, so our answer can imprint the image of us on mind of interviewer. To do the needful in the optimize way, we have a solution. Solution is

public class CircularString{
public static void main(String[] args) {
String A="ABCDEFG";
String B="FGABCDE";
boolean status= C.contains(B);

In the above example complexity would always be 1. Here C represent ABCDEFGABCDEFG. B conatains "FGABCDE". highlight portion of C equals to the String B. If you tell this solution instead of other solution, it will help you to crack the interview.

Thursday, 31 May 2012

One to many relationship in hibernate using annotation -1

Now a days every company using orm tool to connect with the database. There are so many tools are available in the market like hibernate, ibatis and toplink etc.
In the all the ORM tool hibernate is mostly used in the IT.
In the Hibernate we can also implement data base relation ship like one-to-one, one-to-many,many-to-one. Here i'm going to explain one-to-many relationship using hibernate annotation.

According to the relationship a student can attend any number of courses.
To create this relationship we need to have a STUDENT and COURSE table. The relational model shown below-

We can create this relation in two ways -
  1. we will create a column in the course table which will have student id for that course.
  2. we will create a intermediate table which has course id and student id.

Here we discuss by creating a column in the course(Child Table)table. This column shows the one to many relationship between parent (Student) table and child (Course) table. This column can have same value for multiple records of the course table because a student id can be belongs to the multiple course to show which are courses are applied by a particular student. One more important thing is the studentId should be the primary key of the student table.

First we will a Student class which will be responsible for the student table. In the Student table we have following field like studentId,studentName and we have one more field of Set of course. This field doesn't have any mapped column but this field is required to make one to many relationship. This field have all the course detail of a student for which he/she applied.
Here we used some annotation like @table, @column,@JoinColumn etc. We will discuss later.

In the Student class, we have mapping of different field with the different columns.
  1. Student class mapped to the STUDENT TABLE.
  2. studentId Mapped to STUDENT_ID column.
  3. studentName mapped to student_name column.
  4. courses didn't mapped to any column, but we are using @OneToMany annotation any showing that the join column is STD_ID.

Now We need a Course table. We will create a with the name of Course. Course class is responsible for the COURSE table. COURSE Table we have 2 column course_ID and course_name. our course class will be look like

  1. Course class mapped to the COURSE TABLE.
  2. courseId Mapped to course_ID column.
  3. courseName mapped to course_name column.

As of now we used 4-5 annotations now its time get understand about these annotation
@Table used to map with a class to a Database Table
@Id shows this the id for the table.
@GeneratedValue shows the value of Id should be generate by the database itself.
@Column used to map a field to database column.
@OneToMany shows the one-to-many relationship between two tables.
@JoinColumn which join column used to make the relationship.
@Cascade which action should be taken for the child table depend on the parent table action like delete, update.

Till now we created both table STUDENT and Course. Now we have to write a class which can use hibernate to create the table and insert the record.

After executing above code we will 2 tables in the hibernate as below

We can see that one extra column with the name of STUDENT_ID is exist in the Course Table, if we look at the code of course class there is no field with name of Student_id.In the Student table we have a annotation @joinColumn, this annotation is responsible for this extra column in the course table. There is issue in the above implemention. After getting the course Object through hibernate, we cann't get the which studentId is Responsible for this course record. Other issue is we cann't find the Course object through student Id because while executing a query there is no field in the course class which map to studentId To overcome this problem we have to add below statement in the course class

Saturday, 26 May 2012

How to do field validation using the Acegi security in spring

Hi Friends , Today i am going to explain about the validation of username or password while using the Acegi security in spring. let assume our application is using the form based authentication and user clicks on the submit button without entering the field(username,password) value, Acegi security internally executes the query. Here no point in executing the query.

In the terms of DB transaction it is costly operation. In other word , if in our application some pattern is defined for username and password like username shouldn't contain special character etc.Then this type of validation we could do before the request goes to the acegi security otherwise it would be unusual operation. To stop this unusual operation we can modify our code.

We can check before submitting to the server that the values entered by user are satisfied the required format if no then we can show the login page with appropriate error message. Here using this technique we can save so many db interactions.
For that we have to change the following code

login.htm will be mapped to controller. A delegate Controller will use a Model which having 2 parameter with the name of j_username and j_password it validated both field value based on the define rule once the validation passes then it will navigate to success jsp.
In the success jsp we have a java script , called by onLoad event. in this function we have to write following code

Now it will work fine.

Friday, 13 April 2012

Does the Hashmap allow duplicate key or how Hashmap finds duplicate key ???

Hi Friends

Today i am going to discuss a very interesting topic about HashMap. Java HashMap doesn't allow the the duplicate key but it allows the duplicate value. what does it mean that it doesn't allow duplicate. How it internally checks the key is duplicate, any guess? Here people think that internally HashMap would have checked in the existing key that it already there or not.
Can you think if the size of it big and we want to enter another key which is already exist then process of comparing with each existing key will be too vast and it will take lots of time, but when we tries it not takes much time. Here i am going to make you little bit shocked.
Actually whatever we discussed about duplicate keys, HashMap never does the above process to check the duplicate key. Whenever we use the

the it calls the hashCode function for key. hashCode functions returns the memory a memory location associated with the key and copy the value to this particular location. if use again key with different value then the put replace the old value with new value. For us it looks that its not allowing duplicate key but actually it always point to same memory location for a single key. if we use same key again for adding into the HashMap it overrides the old value with new value.


import java.util.HashMap;

public class CheckDuplicate
public static void main(String[] args) {
HashMap map=new HashMap();
map.put("key", "first");
map.put("key", "second");

Out Put :: second

We can understand from example, if it prevent duplicate then the output should have "first". but its overriding the old value so the out put is "second".