August 18 2015

Array of objects in java

Array of objects in java

Arrays are objects that can store multiple variables of same data type. The element of the array can be accessed using index number. The index of the array will always start from zero. If array’s size 5, the index will be from 0 to 4.

Array can hold either primitives or object references.If array is initialized the elements will get their default values, primitives will get the default values as per the data type whereas object references will get null.

 Array of Primitives

In the below example, marks is an array of primitives(int). Values can be initialized using index.

int[] marks = new int[5];

marks[1] = 10;

Array of Object References

In the below example, empArray is an array of Employee Object References. The Employee object reference has to be assigned to the array using index. Continue reading

August 12 2015

Jsf datatable row index

Jsf datatable row index

Jsf datatable row index is used to display the row numbers for each row. This can be done by using binding attribute of datatable. Bind the table to the view itself instead of a bean.

<h:dataTable binding=”#{student}”  .. >

Add a column with the following code

<h:column>

#{student.rowIndex + 1}

</h:column>

Example Continue reading

Category: Jsf | LEAVE A COMMENT
August 7 2015

Jsf validator method

Jsf validator method

Jsf validation can be done in three ways using standard validation tags, validator methods in backing beans and validator classes. Validator method and validator classes are used to write application specific or custom validations.

Validator methods are created when the validation is specific to the page. The validator methods are created in the backing beans and it should have the following syntax

public void methodname(FacesContext facesContext,UIComponent component,

Object objectToValidate) throws ValidatorException

If component’s value is not according to the expected, throw a new ValidatorException instance. If an exception is thrown, Jsf will automatically add the FacesMessage instance to the message queue. Continue reading

Category: Jsf | LEAVE A COMMENT
August 6 2015

Jsf custom validator

Jsf custom validator

A custom validator in Jsf can be created by implementing javax.faces.validator.Validator interface which has a one method (validate).

public void validate(FacesContext context, UIComponent component,Object value) throws ValidatorException;

The class has to implement validate method. If component’s value is not according to the expected, throw a new ValidatorException instance. If an exception is thrown, Jsf will automatically add the FacesMessage instance to the message queue.

The validator has to be registered and a unique identifier has to be assigned to it. Validator can be registered by adding it in faces-config.xml or by adding @FacesValidator annotation to the Validator class.

The validator can be applied to the component using <f:validator> by passing the validator unique identifier as parameter. Continue reading

Category: Jsf | LEAVE A COMMENT
August 5 2015

Compare two dates in java

Compare two dates in java

Dates can be compared using different ways

1. Comparing using compareTo method

public int compareTo(Date date)

The java.util.Date.compareTo(Date date) method compares two Dates. compareTo method returns 0 if this date is equal to argument date, positive value (>0) if this date is after the argument date and negative value (<0) if this date is before the argument date.

Example Continue reading

August 5 2015

Java date difference in days

Java date difference in days

Convert the dates into milliseconds using the getTime() method and calculate the difference (subtract) between them.

long diffMs = date2.getTime() – date1.getTime();

diffMs will have the difference between dates in milliseconds. diffMs can be converted to days manually or using java.util.concurrent.TimeUnit

long diffDays = diffMs / (24 * 60 * 60 * 1000);

or

long diffDays = TimeUnit.DAYS.convert(diffMs, TimeUnit.MILLISECONDS)) Continue reading

July 29 2015

Jsf rendered attribute example

Jsf rendered attribute example

The rendered attribute which uses Boolean EL expression indicates whether a component is currently visible or not. The property is useful when you want to hide components for specific users or based on condition. The default value of rendered attribute is true.

Example:

<h:outputText value=”mapping” rendered=”Boolean EL expression” />

rendered Display the component
true Yes
false No

 Example Continue reading

Category: Jsf | LEAVE A COMMENT
July 13 2015

Hibernate cascade delete

Hibernate cascade delete

When cascade=”delete” is declared on association, hibernate deletes all the persistent instances of the association (children) whenever the delete operation is performed on object. If you don’t not use cascade delete, you have to delete all the child objects one by one explicitly before deleting the parent object.

Hibernate cascade delete example

In the below example, ’CascadeType.DELETE’ is declared for the association. Whenever delete operation is performed on Student object, all the associated StudentMarks records will be deleted along with Student record.

select * from student;

STUDENT_ID STUDENT_NAME FATHER_NAME DATE_OF_BIRTH COURSE
105 PAUL GEORGE 5/22/2015 MS

select * from student_marks; Continue reading

July 13 2015

Hibernate cascade delete orphan

Hibernate cascade delete orphan

Deletes the persistent instance that has been removed (from collection) or dereferenced (object).The orphan delete allows us to delete the child entity by removing or dereferencing it from parent instead of deleting the child entity’s directly.

Hibernate cascade delete orphan Example

select * from student;

STUDENT_ID STUDENT_NAME FATHER_NAME DATE_OF_BIRTH COURSE
105 PAUL GEORGE 5/22/2015 MS

select * from student_marks; Continue reading