Wednesday, June 30, 2010

JPA @Transaction default properties

Lets examine JPA default Transaction attribute(@Transaction) and propagation

When using the @Transactional annotation by itself without any parameters, the propagation mode is set to REQUIRED, the read-only flag is set to false, the transaction isolation level is set to the database default (usually READ_COMMITTED), and the transaction will not roll back on a checked exception.


@Transactional(propagation=Propagation.REQUIRES_NEW)
The REQUIRES_NEW transaction attribute should be used only if the database action in the method being invoked needs to be saved to the database regardless of the outcome of the overlaying transaction. For example, suppose that every stock trade that was attempted had to be recorded in an audit database. This information needs to be persisted whether or not the trade failed because of validation errors, insufficient funds, or some other reason. If you did not use the REQUIRES_NEW attribute on the audit method, the audit record would be rolled back along with the attempted trade. Using the REQUIRES_NEW attribute guarantees that the audit data is saved regardless of the initial transaction's outcome. The main point here is always to use either the MANDATORY or REQUIRED attribute instead of REQUIRES_NEW unless you have a reason to use it for reasons similar those to the audit example.

Friday, May 21, 2010

Java XML Simple Read Example

To Read and extract the elements from the XML file first parse the XML file though the parser and create the Document
Document doc = builder.parse(str);

Once the document is availabe we can fetch the root and elements as follows
Node node = doc.getDocumentElement();
String root = node.getNodeName();

Example is given below

import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;

public class GetRootNode{
public static void main(String[] args) {
try{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter xml file name: ");
String str = bf.readLine();
File file = new File(str);
if (file.exists()){
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = fact.newDocumentBuilder();
Document doc = builder.parse(str);
Node node = doc.getDocumentElement();
String root = node.getNodeName();
System.out.println("Root Node: " + root);
}
else{
System.out.println("File not found!");
}
}
catch(Exception e){}
}
}

Tuesday, May 18, 2010

Hibernate Mapping - inverse="true"

Hibernate Basic Mapping:
Just refreshing our old fried Hibernate Mapping :)

Set -> We know Set holds unique objects. Example for using Set is "Member" can hold N number of "Claim"
Member is the Parent and Claim the child. Here we can use One-To-Many relation with Set as Collection.

inverse="true" -> persistence will be taken care by the other end ie., in this case child
cascade="all" -> Example to explain cascade option.  when you are deleting an parent it will automatically delete the child.





    
    

Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
p.addChild(c);
session.flush();

Monday, May 17, 2010

JSF aj4 ajaxsingle=true - Examine

When ajaxsingle=true is used in the element then, while ajax submission only that particular element will be submitted instead of whole form.

Example when we have two drop down Country and state. Consider state is mandatory field and state is populated by a4jsupport of country drop down. If we are not making ajaxsingle=true in country drop down then required validation error will be thrown while selecting the country even before user selecting the state.

JSF bypassupdates - Examine

When we use bypassupdates we can able to skip the model update phase alone while all other 5 phases among 6 will be executed by JSF framework.

JSF Phases:
1. Restore View Phase
2. Apply Request Values Phase
3. Covert and Validations Phase
4. Update Model Values Phase
5. Invoke Application Phase
6. Render Response Phase

When bypassupdates is defined, then JSF will skip execution of Phase 4

JSF immediate=true - Examine

When setting immediate=true in JSF command button we can able to skip validation and model update phase. This is helpful while implementing the cancel button functionality

JSF Phases:
1. Restore View Phase
2. Apply Request Values Phase
3. Covert and Validations Phase
4. Update Model Values Phase
5. Invoke Application Phase
6. Render Response Phase

When immediate=true is defined then JSF will skip execution of Phase 3 and 4 ie., convertion, validation and model update wont happen.

Example:

Tuesday, April 27, 2010

JSF Page not submit issue

While using JSF developers may come across problem while submitting the page. Page may not be getting submitted. Following are finding for JSF not submission

1. Component in the jsf page may be seting as null. Check which component is null and fix it. Always initialize the variable in JSF backing bean.

2. Dropdown index value (SelectItem id) should be string and not integer!! So always while setting the List key value make sure it is string.

eg:
//Valid
int i = 0;
SelectItem item = new SelectItem(i+"", rvStatus.getDescription());


//Not Valid
int i = 0;
SelectItem item = new SelectItem(i, rvStatus.getDescription());

3. Rare cases - while displaying the multiple panel based on the render true or false in jsp may leads to break in submiting the button. This can be fixed by handling the render logic in backing bean by binding the panel.


There could be some more reason behind these please share it in the comment. Thanks