Sunday, September 19, 2010

Generating Java POJO from XSD schema - JAXB and CASTOR

It is a contribution from Parthiban Rajasekaran

Generate Java Files using JAXB
1. Download latest JAXB Binary from https://jaxb.dev.java.net [latest as of now can be downloaded from https://jaxb.dev.java.net/2.2/JAXB2_20091104.jar] and execute this jar to get the JAXB package
2. Create JAXB_HOME with the jaxb extracted location, for example D:\Personal\Downloaded\Sun API\JAXB\jaxb-ri-20091104
3. Append %JAXB_HOME%\bin in the path system variable
4. Execute xjc command from the dtd or xsd location to generate java files
a. To create java from dtd – xjc -d -p -dtd
b. To create java from xsd – xjc -d -p

Hint: Convert DTD to XML - http://www.hitsw.com/xml_utilites/

Generate Java Files from XSD using Castor

set classpath=castor-1.3-codegen.jar;castor-1.3-xml.jar;castor-1.3-xml-schema.jar;castor-1.3-core.jar;commons-logging-1.1.1.jar;%CLASSPATH%;

java org.exolab.castor.builder.SourceGeneratorMain -i message.xsd -package com.bt.hqn.creditclient.buynet.integration.cbean






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: