Thursday, July 31, 2014

Play Framework "Model Class" NOT an Entity Bean registered with this server?

Sometimes you may get PersistenceException while trying to run a Play application.

For example there is a Employee model class.  While running the play app it throws the following exception.

PersistenceException: models.Employee is NOT an Entity Bean registered with this server?


Solution : The simple solution is to annotate the Employee class with @Entity.  Doing this tells the application to treat the Employee model as an Entity.  Here is the code snippet...


import play.db.ebean.Model;
import javax.persistence.*;

@Entity
public class Employee extends Model{ ... }

Thursday, July 10, 2014

Understanding How :: Cons works in Scala

:: which is also pronounced as Cons is sometimes confusing to understand.  In this article let us try to understand how it works.

Before we try to understand Cons you should remember that every operation is actually a method call in Scala.  There is no such thing called operator, although the operation may falsely lead you to think that operands are being operated with an operator.

So What About + and - Operators?

You many think that simple operations such as 5 + 3 are using the + (plus) operator.  So how can we say operators are not there.

In Scala there is no primitive types.  So 5 is nothing but Int(5) and 3 is stored as Int(3).  There is a method named + in the Int class.  When we call 5 + 3 internally it calls (5).+(3), similarly 4 * 3 becomes (4).*(3).

Scala allow you to call method names with spaces and without the . (period) if there is a single parameter to the method name.  Hence (5).+(3) can be called as 5 + 3.

What About :: Cons?

:: is a special method which is used for prepending values.  Which means when you want to add a value to beginning of a list you can used cons or ::

For Example :

var names = List ("Roy","Randy","Amit")
var newNames = "Cindy" :: names
println(newNames)

Output would be

List(Cindy, Roy, Randy, Amit)

So how does it work?  If we keep the previous discussion in mind the :: should be a method which should work on the String "Cindy".  However in reality it works on the List names.

The reason is whenever any method name is invoked it is applied on the left operand in most cases apart from those method which end with a : (colon).  In those cases the method is invoked on the right operand.

In this case this method has a name :: which ends with a : so the method is invoked on the names List.

Just remember "Cindy" :: names is interpreted as names.::("Cindy")

Hopefully this will help you to understand how cons work.

How to Write public static void main in Scala

psvm or public static void main(String agrs[]) is the starting method for any Java new bee.  This method is static as the signature suggests  , hence it can be invoked from the command line.  So if you are starting Scala you may be thinking about the method which is equivalent to this method.

Here is that signature -

def main (args : Arrays[String]) : Unit = {
  //method body
}

Here is an example in a class.

object HelloWorld {
   def main (args : Arrays[String]) : Unit = {
      println("Hello World")
   }
}

As may notice we started keyword object.  This is how you created Singleton object in Scala.  Isn't it simple?  The main method can now be easily mapped to public static void main.  Unit is a datatype which is equivalent to void in java.  It basically means this method does not have return value.

Class Parameter Differences Between Scala And Java

Scala has something called class parameter.  In Java there is no such thing as class parameter but we can initialize a class with constructor arguments which look similar to class parameter in Scala.

Let's have a bit more detailed look.

Scala Class Example

We can define a Scala class as follows -

class Person(name : String) {
    def sayHello(){
      println("Hello "+name)
    }
}

Now to run the class after compiling you can do it the following way -

new Person("John").sayHello()

Output should be

Hello John

However in Java if we want to have send some parameter while instantiating the object it would be similar to following -

public class Person {
    String name ;
    Person(String name){
        this.name=name;
    }
    public void sayHello(){
        System.out.println("Hello"+name);
    }
}

while running we can then run in similar manner as

new Person("John").sayHello()

The output should be same as what we got in Scala.

So What are the Differences?

In Java the parameter passed in the constructor can't be used in any other method unless it is assigned to some field or property.  In Scala it is available for use in any other method of the instance directly.  However the parameter value is only available in this particular instance of the class.

Another difference is in Scala class parameter is defined while defining the class name and we do not need a separate constructor unlike in Java.

How to upgrade the Scala Class Parameters to fields or Properties?

Scala class parameters can be upgraded to fields very easily.  We can do that by adding the keywords val or var.

Here are examples -

class Person(var name : String) {
    def sayHello(){
      println("Hello "+name)
    }
}

class Person(val name : String) {
    def sayHello(){
      println("Hello "+name)
    }
}

If you add the val keyword the parameter becomes immutable field and in the byte code you can see a private field with public getter method.

If you add the var keyword the parameter becomes mutable field and in the byte code you can see a private field with public getter and setter methods.

Tuesday, July 8, 2014

How Does Scala List.map Function Work

When I was learning Scala I came across the List,map method.  It is defined as follows in the documentation ...

final def
map[B](f: (A) ⇒ B): List[B]

[use case] Builds a new collection by applying a function to all elements of this list.

How what does this mean and how can we use it?

Although from the definition it may look cryptic it becomes easier to understand once we have an example.   Let us see how to use List.map in Scala using a simple example -

Let us say you have defined a list of fruits as follows -

var fruits = List("Apple","Orange","Banana");

Now you want to convert each of the fruits into lower case and return another List.  This can be achieved very easily with List.map function.

var lowerCaseFruits = fruits.map(fruit => fruit.toLowerCase)
println(lowerCaseFruits)

The map function is applied on the fruits list and each element is stored in the fruit element one by one. Then the element is converted to lower case using the String lower case function and then stored in another list which is eventually returned back.  In this case the output List is stored in the variable lowerCaseFruits.

There is more fun to List. The output of List.map can be again chained to another List.map.  Here is an example -

var listOfNumber = List(1,2,3)
var nums = listOfNumber.map(number => number * 2).map(number => number * number)
println(nums)

Can you guess what will be the output and why?

What Happens When Scala Set Contains Duplicate Elements

Set is a collection of non duplicate items.  In Scala also the concept is similar.  So what happens when you have a Set defined with duplicate items.

For example : 

    val vege = Set("Radish","Carrot","Carrot","Potato");
    println(vege);

You may expect the compiler to complain since Carrot occurs twice in the Set.  However see the result below on what happens -

Set(Radish, Carrot, Potato)

It simply means that Scala ignores the duplicate entry in the Set and does not complain.

Cool...