Thursday, November 28, 2013

What are the essential skills of a Java/J2EE or Enterprise Java developer?

According to me if you are seeking to be a really effective JEE Developer, these are the following skills I would recommend.
Technical

  • Knowledge of OOPs concepts.
  • Basic knowledge of how JVM works and concepts like hashing.
  • At least one framework( Struts or Spring).
  • JSP and one scripting language(Javascript,/JQuery). I would advise you to get a good grip on HTML/XHTML fundamentals too. Clients nowadays are very particular about UI design.
  • EJB( knowledge of CORBA would be a bonus).
  • At least one application server( Weblogic/Websphere/JBoss) you should be having some knowledge about.
  • Web Services, considering many organizations are switching to it, as a better alternative to EJB's.
  • One ORM tool like Hibernate.
  • Knowledge of one configuration management tool like CVS/SVN/Mantis/Accurev.
  • Knowledge of any build tool like ANT/Maven.
  • Unit testing tools like JUnit.
  • Good idea about open source tools available on the Net.
  • Knowledge of design patterns.

Overall, a really good JEE developer should be having a holistic knowledge of the whole application development process. The good old days, when candidates who just knew how to program Hello World in Java, were recruited, are gone.

Non Technical 

  • Communication skills is a must, you will be interacting with clients, your managers, QA team, and effective communication will go a long way, especially in a networked environment.
  • Good presentation skills, these would come in handy, when you seek to do a walk through with the client or  explain a new tool to the team.
/Deepak Pandey

Wednesday, September 25, 2013

Fibnocci series easiest way...

public static void main(String[] args) {
  int f = 0;
  int g = 1;

  for(int i = 1; i <= 10; i++)
  {
    System.out.print(f + " ");
    f = f + g;
    g = f - g;
  } 

  System.out.println();
}

Easiest way to reverse string in java and check for palindrome...

class RevString
{
    static String reverse(String str) {
       char[] c = str.toCharArray();
       char[] r = new char[c.length];
       int end = c.length - 1;
    
        for (int n = 0; n <= end; n++) {
           r[n] = c[end - n];
            }
           return new String(r);
        }

 bool isPalindrome(string str) {
  int end= str.length();
  for (int i=0;i<(end/ 2)+ 1;++i) {   
      if (str.charAt(i) != str.charAt(end- i - 1)) { 
         return false;
     }
  }

  return true;
}
 public static void main (String[] args)
 {
  System.out.println(RevString.reverse("hello"));
System.out.println(RevString.isPalindrome("liril"));
 }
}

Tuesday, July 23, 2013

Hibernate better understanding :: Object-relational mapping in JPA


via:: Nikojava
The Java Persistence API is the standard framework for persistence in both Java SE and Java EE platforms. It is the outcome of the collaborative work of the industry’s leading vendors in object-relational mapping, EJB and JDO, including a remarkable contribution from the open source community.
In this post we summarize, one-by-one, the essential relationships between entities. We examine each relationship from the prespectives of “Domain Model” and “Relation Model”, and provide the associated construction in JPA. Finally, for each relationship we present an example from the real world.

The relationships are divided in two categories. “For daily use” presents the correct way to use common relationships in our application development activities. On the other hand, “Containing pitfalls” identify common mistakes.
For daily use:
  1. One-to-One (one direction)
  2. One-to-One (both directions)
  3. Many-to-One (one direction)
  4. Many-to-One (both directions)
  5. One-to-Many (one direction)
  6. One-to-Many (both directions)
  7. Many-to-Many (both directions)
Containing pitfalls:
  1. Many-to-Many (one direction)

One-to-One (one direction)

Domain model of an one-to-one unidirectional relationship
Domain model of an one-to-one unidirectional relationship
@Entity
class A {
   @Id int id;

   @OneToOne
   B b;
}

@Entity
class B {
   @Id int id;
}
Relation model of an one-to-one unidirectional relationship
Relation model of an one-to-one unidirectional relationship

Example 1, One-to-One unidirectional relationship in JPA

An employee has one desk.
@Entity
public class Employee implements Serializable {

   @Id 
   private int id;

   private String name;

   @OneToOne
   private Desk desk;

}

@Entity
public class Desk implements Serializable {

   @Id 
   private int id;

   private String position;  

}
Physical description of an one-to-one unidirectional relationship
Physical description of an one-to-one unidirectional relationship
ER diagram of an one-to-one unidirectional relationship
ER diagram of an one-to-one unidirectional relationship

One-to-One (both directions)

Domain model of an one-to-one birectional relationship
Domain model of an one-to-one birectional relationship
In this case we would like to keep a reference between both sides.
@Entity
class A {
   @Id int id;

   @OneToOne
   B b;
}

@Entity
class B {
   @Id int id;

   @OneToOne(mappedBy="b")
   A a;
}
Relation model of an one-to-one bidirectional relationship
Relation model of an one-to-one bidirectional relationship
Since the relation concerns both directions, we may change the owning side.
@Entity
class A {
   @Id int id;

   @OneToOne(mappedBy="a")
   B b;
}

@Entity
class B {
   @Id int id;

   @OneToOne
   A a;
}
Relation model of an one-to-one bidirectional relationship
Relation model of an one-to-one bidirectional relationship

Example 2, One-to-One bidirectional relationship in JPA

A husband has exactly one wife. A wife has exactly one husband.
@Entity
public class Husband implements Serializable {

   @Id
   private int id;

   private String name;

   @OneToOne
   private Wife wife;

}

@Entity
public class Wife implements Serializable {

   @Id
   private int id;

   private String name;

   @OneToOne(mappedBy="wife")
   private Husband husband;

}
We assume husband is the owning side, so he will hold the foreign key.
Physical description of bidirectional one-to-one relationship
Physical description of an one-to-one bidirectional relationship
ER diagram of an one-to-one bidirection relationship
ER diagram of a bidirection one-to-one relationship
Now we can ask the wife “Who is your husband?”, as well as, ask the husband “Who is your wife?”.

Many-to-One (one direction)

Domain model of a many-to-one unidirectional relationship
Domain model of a many-to-one unidirectional relationship
@Entity
class A {
   @Id int id;

   @ManyToOne
   B b;
}

@Entity
class B {
   @Id int id;
}
Relation model of a many-to-one unidirectional relationship
Relation model of a many-to-one unidirectional relationship

Example 3, Many-to-One unidirectional relationship in JPA

There are millions of music fans out there. Each fan has his favorite singer. Of course, a signer is not able to keep detailed records of his fans.
@Entity
public class Fan implements Serializable {

   @Id
   private int id;

   private String name;

   @ManyToOne
   private Singer favoriteSinger;

}

@Entity
public class Singer implements Serializable {

   @Id
   private int id;

   private String name;

}
As a result, the foreign key goes to the fan.
Physical description of a many-to-one unidirectional relationship
Physical description of a many-to-one unidirectional relationship
ER diagram of a many-to-one unidirectional relationship
ER diagram of a many-to-one unidirectional relationship
Now we may ask any fan “Who is your favorite signer?”.

Many-to-One (both directions)

Domain model of a many-to-one bidirectional relationship
Domain model of a many-to-one bidirectional relationship
@Entity
class A {
   @Id int id;

   @ManyToOne
   B b;
}

@Entity
class B {
   @Id int id;

   @OneToMany(mappedBy="b")
   Collection<A> listOfA;
}
Relation model of a many-to-one bidirectional relationship
Relation model of a many-to-one bidirectional relationship

Example 4, Many-to-One bidirectional relationship in JPA

The children of a father: A child knows his father and the father knows his children.
@Entity
public class Child implements Serializable {

   @Id
   private int id;

   private String name;

   @ManyToOne
   private Father father;

}

@Entity
public class Father implements Serializable {

   @Id
   private int id;

   private String surname;

   @OneToMany(mappedBy="father")
   private Collection<Child> children;

}
Physical description of a many-to-one bidirectional relationship
Physical description of a many-to-one bidirectional relationship
ER diagram of a many-to-one bidirectional relationship
ER diagram of a many-to-one bidirectional relationship

One-to-Many (one direction)

Domain model of a one-to-many unidirectional relationship
Domain model of a one-to-many unidirectional relationship
It is the case when an entity has a set of characteristics.
@Entity
class A {
   @Id int id;

   // A join table is assumed.
   @OneToMany
   Collection<B> listOfB;
}

@Entity
class B {
   @Id int id;
}
Relation model of a one-to-many unidirectional relationship
Relation model of a one-to-many unidirectional relationship
Thus, when declaring an OneToMany annotation without a mappedBy element, then a join table is assumed.

Example 5, One-to-Many unidirectional relationship in JPA

A library which provides facilities.
@Entity
public class Library implements Serializable {

   @Id
   private int id;

   private String name;

   @OneToMany
   private Collection<Facility> facilities;

}

@Entity
public class Facility implements Serializable {

   @Id
   private String code;

   private String description;

}
Physical description of a one-to-many unidirectional relationship
Physical description of a one-to-many unidirectional relationship
ER diagram of an one-to-many unidirectional relationship
ER diagram of an one-to-many unidirectional relationship
Now we can ask a library “What facilities do you provide?”. The answer could be “Scanning, Printing and Photocopying”. This set of facilities may be provided by one or more libraries at the same time.
If using a join table is not what you want, then you should use the next relationship.

One-to-Many (both directions)

It is exactly the same with Many-to-One (both directions), looking via a mirror.
Domain model of a one-to-many bidirectional relationship
Domain model of a one-to-many bidirectional relationship
@Entity
class A {
   @Id int id;

   @OneToMany(mappedBy="a")
   Collection<B> listOfB;
}

@Entity
class B {
   @Id int id;

   @ManyToOne
   A a;
}
Relation model of an one-to-many bidirectional relationship
Relation model of an one-to-many bidirectional relationship

Example 6, One-to-Many bidirectional relationship in JPA

A manager who manages projects.
@Entity
public class Manager implements Serializable {

   @Id
   private int id;

   private String name;

   @OneToMany(mappedBy="manager")
   private Collection<Project> projects;

}

@Entity
public class Project implements Serializable {

   @Id
   private int id;

   private String title;

   @ManyToOne
   private Manager manager;

}
Physical description of a one-to-many bidirectional relationship
Physical description of a one-to-many bidirectional relationship
ER diagram of an one-to-many bidirectional relationship
ER diagram of an one-to-many bidirectional relationship

Many-to-Many (both directions)

Domain model of a many-to-many bidirectional relationship
Domain model of a many-to-many bidirectional relationship
@Entity
class A {
   @Id int id;

   @ManyToMany
   Collection<B> listOfB;
}

@Entity
class B {
   @Id
   int id;

   @ManyToMany(mappedBy="listOfB")
   Collection listOfA; } 
Of course, a join table is used.
Relation model of a many-to-many bidirectional relationship
Relation model of a many-to-many bidirectional relationship
Note: In a birectional many-to-many relationship we should alwaysinclude the mappedBy element to either of the sides. This ensures that exactly one join table is used.

Example 7, Many-to-Many bidirectional relationship in JPA

An engineer works in many projects. A project occupies many engineers. This is a classic many-to-many relationship. Moreover it expands to both directions, as:
  • An engineer should in which projects he is working.
  • The project should know the engineers it occupies.
@Entity
public class Engineer implements Serializable {

   @Id
   private int id;

   private String name;

   @ManyToMany
   @JoinTable(name="ENGINEER_PROJECT",  
              joinColumns=@JoinColumn(name="ENGINEER_ID"), 
              inverseJoinColumns=@JoinColumn(name="PROJECT_ID"))
   private Collection<Project> projects;

}

@Entity
public class Project implements Serializable {

   @Id
   private int id;

   private String title;

   @ManyToMany(mappedBy="projects")
   private Collection<Engineer> engineers;

}
Physical description of a many-to-many bidirectional relationship
Physical description of a many-to-many bidirectional relationship
ER diagram of a many-to-many bidirectional relationship
ER diagram of a many-to-many bidirectional relationship

Pitfall: Many-to-Many (one direction)

Not specifying the mappedBy element in a many-to-many relationship, assumes two join tables and should be avoided.
@Entity
class A {
   @Id int id;

   @ManyToMany
   Collection<B> listOfB;
}

@Entity
class B {
   @Id int id;

   @ManyToMany
   Collection<A> listOfA;
}
The same happens when the ManyToMany annotation is ommitted on one side.
@Entity
class A {
   @Id int id;

   @ManyToMany
   Collection<B> listOfB;
}

@Entity
class B {
   @Id int id;

   Collection<A> listOfA;
}
Relation model of two many-to-many unidirectional relationship
Relation model of two many-to-many unidirectional relationship
To avoid this pitfall, I would follow the instructions of Many-to-Many (both directions) relationship.

Creating mirror of BST