JPA vs ADF-BC(BC4J)

In ADF, for creating Business services related to DB we generally use ADF – BC (EO, VO, AM), but we can also use JPA(Java Persistence API) Entities. Which option to choose is completely your choice as there won’t be any difference if you create data controls out of them in terms of performance. But you can note few points that ADF-BC provides default List of Values and validations options. If you are familiar with using Hibernate (But not with ADF-BC) then you are good to go with using JPA Entities.

JPA Entity:

JPA

You can find the below sample JPA entity created for Departments table.


package model;

import java.io.Serializable;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

@Entity
@NamedQueries({ @NamedQuery(name = "Departments.findAll", query = "select o from Departments o") })
public class Departments implements Serializable {
private static final long serialVersionUID = -1771169464233198257L;

@Id
@Column(name = "DEPARTMENT_ID", nullable = false)
private Integer departmentId;

@Column(name = "DEPARTMENT_NAME", nullable = false, length = 30)
private String departmentName;

@Column(name = "LOCATION_ID")
private Integer locationId;

@ManyToOne
@JoinColumn(name = "MANAGER_ID")
private Employees employees1;

@OneToMany(mappedBy = "departments", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private List<Employees> employeesList1;

public Departments() {
}

public Departments(Integer departmentId, String departmentName, Integer locationId, Employees employees1) {
this.departmentId = departmentId;
this.departmentName = departmentName;
this.locationId = locationId;
this.employees1 = employees1;
}

public Integer getDepartmentId() {
return departmentId;
}

public void setDepartmentId(Integer departmentId) {
this.departmentId = departmentId;
}

public String getDepartmentName() {
return departmentName;
}

public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}

public Integer getLocationId() {
return locationId;
}

public void setLocationId(Integer locationId) {
this.locationId = locationId;
}

&nbsp;

public Employees getEmployees1() {
return employees1;
}

public void setEmployees1(Employees employees1) {
this.employees1 = employees1;
}

public List<Employees> getEmployeesList1() {
return employeesList1;
}

public void setEmployeesList1(List<Employees> employeesList1) {
this.employeesList1 = employeesList1;
}

public Employees addEmployees(Employees employees) {
getEmployeesList1().add(employees);
employees.setDepartments(this);
return employees;
}

public Employees removeEmployees(Employees employees) {
getEmployeesList1().remove(employees);
employees.setDepartments(null);
return employees;
}
}

 

Advertisements

Build Tools (Ant, Maven)

  • In this post I’ll discuss why build tools are required and how they really work in top level.
  • For a specific build tool it expects the project to be in specific structure. E.g: web.xml and lib folder should be present only in WEB-INF folder. Then only it’ll be able to build and deploy. Like that each build tool have their own standards of default project structure. Another example is Maven expects pom.xml to be present in the project in the root directory.

Why Ant or Maven scripts are required?

  • So far we discussed that each build tool expects project to be in specific structure then only it can be able to build and deploy. If your project is not in specific structure which the build tool is expecting, then you can write a script (Ant script, Maven script) by giving the paths of required folders in the script, which makes the script to build and deploy the project.
  • Another reason for writing scripts is that, if you want to deploy the project with out manual process, you can write the script and prepare a batch file to do that job.

Sample Ant script:

you can find sample ANT script from below website:

https://docs.spring.io/docs/Spring-MVC-step-by-step/part1.html

Singleton Implementation with Double Checked Locking in Java

-> Singleton implementation is nothing but creating the object instance only once and reusing the same instance when ever needed, instead creating a new one each time when needed.
-> In the below code instance is of lazy loading initializing type as the instance of class is declared in static variable but which is initialized only when the method getInstacne is invoked. Eager loading will be like declaring and initializing at the same time.like shown below.
private volatile static Singleton instance = new Singleton();

Double checked locking Idiom:

-> Now lets talk about Double checked locking idiom in singleton implementation. Here object is null or not will be checked twice and then object for the class will be created.
-> Why Volatile is used?: In double checked locking idiom, suppose if thread A is entered first null condition and then second null condition, then it will create an object for the class. Now suppose if thread B is entered first null condition, at the same time for thread A object is getting created (but instance creation is not completed yet) then as the null condition fails it’ll just return the same object instance which not completely initialized (half baked object), which leads to unexpected behavior of object.
So when voltaile is used, which makes the object getting published only when it is completely initialized, which avoids above problem.
-> The real problem is that Thread A may assign a memory space for instance before it is finished constructing instance. Thread B will see that assignment and try to use it. This results in Thread B failing because it is using a partially constructed version of instance.

Code:

public class Singleton {
private volatile static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}

 

Few References:

http://javarevisited.blogspot.in/2014/05/double-checked-locking-on-singleton-in-java.html

 

 

Data Types in Java

Primitive Data types:

In Java there are eight (8) primitive data types. For all those primitive data types wrapper classes also available.

If you define the primitive data types as global variables they hold some default values which are given below.

/* Primitive data types and their default values*/
static byte by; //0
static char c; // space
static short sh; //0
static int i; //0
static long l; //0
static float f; //0.0
static double d; //0.0
static boolean b; //false

Note: If you define the primitive data types as local variables they hold nothing and you must initialize them.

Wrapper Classes (Non Primitive Data types):

If you define the wrapper class data types as global variables then they hold some default values which is null

 /* Wrapper Class data types */
static Byte by1; //null
static Character c1; //null
static Short sh1; //null
static Integer i1; //null
static Long l1; //null
static Float f1; //null
static Double d1; //null
static Boolean b1; //null

static BigDecimal bd; //null
static String s; //null

Note: If you define these wrapper class data types as local variables they hold nothing and you must initialize them.  There are few more wrapper classes like Array, List etc.

Eclipse Shortcuts:

The post discuss about keyboard shortcuts for various function in Eclipse IDE which are very useful for rapid development.

Searching a file with its name:  Ctrl + Shift + R

Search a text in all the files: Ctrl + h (File Search tab)

Run java file: [Alt + Shift + X    & J] ,    [Ctrl + F11]

Navigate to the selected text in other places of file:   Ctrl + K

Organize Imports: Ctrl + Shift + O

To Navigate to the start/end of the block:  Ctrl + Shift+ P

To see the list of opened files and to navigate to them:  Ctrl + E

Suggestion when you are typing: Ctrl + Space

To get the list of methods which you can override:  [Alt +  Shift +  S     &  V]

To get the System.out.println();   [syso & Ctrl + Space]

To clear the breakpoints: Run-> Remove all breakpoints/ skip all breakpoints [this option will be shown only when application is running in debug mode]

To open IDE views (like console, variables) : Window -> show view

To know the result of a method: Ctrl+i

 

 

 

Sorting in Java

Sorting the numbers is easy in Java. Collections utility class provides the ‘sort’ method for sorting the numbers. [Don’t confuse with Collection which is an Interface]. To the Collections  Sort method you need to input numbers in List format. Though numbers present in Array also can be converted to List and then can be sorted. Java also provides another utility class ‘Arrays’ which has also Sort method. You can use either class methods for sorting on your convenience.

Collections.sort method expects List as input [array can also be send by converting to list object using Arrays.asList(arrayObj) method]

Arrays.sort method expects Array as input [List can also be send by converting to Array object using listObj.toArray() method]

Eg1: Sorting Numbers in List

List <Integer> numLst1 = new ArrayList<>();
numLst1.add(20);
numLst1.add(10);
numLst1.add(04);
Collections.sort(numLst1);
//Arrays.sort(numLst1.toArray());
int f = 0;
while(f< numLst1.size()){
System.out.println(numLst1.get(f));
f++;
}

o/p:   [4    10     20]

Eg2: Sorting Numbers in Array

Here we are converting Array to List and then sorting.

Integer[] numLst1 = {10, 30, 05};
Arrays.sort(numLst1);
//Collections.sort(Arrays.asList(numLst1));
int f = 0;
while(f<numLst1.length()){
System.out.println(numLst1[f].intValue());
f++;
}

o/p: [5    10     30]

Eg3: Sorting numbers and finding smallest number

Now let’s get into some deep by taking a requirement which returns the smallest number from the Array which should not be 0. Means if the Array contains numbers like [12, 20, 5, 30, 0, 50] then it should return smallest number as ‘5’ (not zero).

Integer[] returnVal = {10, 5, 20, 0};
Arrays.sort(returnVal);
//Collections.sort(Arrays.asList(returnVal));
int f= 0;
int smallValue = 0;
while(f < returnVal.length){
if (returnVal[f].intValue() !=0){
smallValue = returnVal[f].intValue();
break;
}
f++;
}
System.out.println("smallValue:"+smallValue);

o/p:   5

Sorting Numbers in HashSet & HashMap [not with the help of Sort method]

This can be achieved with the help of TreeSet. From TreeSet if you try to move to HashSet again then the insertion order will be lost. So keep the sorted result in TreeMap only or store them into LinkedHashSet where the insertion order is maintained. Similarly for HashMap sorting can be achieved with the help of TreeMap.


HashSet<Integer> hs = new HashSet<>();
hs.add(10);
hs.add(6);
hs.add(20);
hs.add(12);
System.out.println("hs:"+hs);
TreeSet<Integer> ts = new TreeSet<>();
ts.addAll(hs);
System.out.println("ts:"+ts);

o/p:     hs:[20, 6, 10, 12]      ts:[6, 10, 12, 20]

equals() vs contentEquals() vs ==

Why equals method is used?

To compare two string contents [One string can be in stack memory and other can be in Heap memory, still it compares the contents of the strings]

String s1 = “abc”;

String s2 =  “abc”;

String s3 = new String (“abc”);

System.out.println(s1.equals(s2));  // o/p: True  [hashcodes are same in this case]

System.out.println(s1.equals(s3));   // o/p: True [hashcodes are different in this case]

Why ‘==’ is used?

It is an operator which compares the Hashcodes (not the content present inside them).

String s1 = “abc”;

String s2 =  “abc”;

String s3 = new String (“abc”);

System.out.println(s1==s2);  // o/p: True  [hashcodes are same in this case]

System.out.println(s1==s3);   // o/p: False [hashcodes are different in this case]

Why contentEquals is used?

It is used to compare the String object with StringBuffer/StringBuilder/ Char Array objects.

Note:

  • You can’t compare String object with StringBuffer object using equals method.
  • You can compare String object with another String object using equals and contentEquals methods. [both are same, but equals is specific to compare strings only]

 

String s1 =”abc”;

String s2 =new StringBuilder(“abc”);

System.out.println(s1.contentEquals(s2)); // o/p: true [hashcodes are different in this case]