Thursday, October 16, 2014

How to create Java Classes using xjc from XML file

This tutorial will demonstrate how can you generate Java Objects from XML documents using xjc tool. The tool “xjc” is used to generate the annotated Java classes from the XSD schema

Suppose I am using below XML file to generate Java Objects i,e
<?xml version="1.0" encoding="UTF-8"?>
<adaptorapp id="application">
<instance id="instance1">
<connectiondetails>
<accesskey>abcde</accesskey>
<secretkey>fghij</secretkey>
</connectiondetails>
</instance>
<instance id="instance2">
<connectiondetails>
<accesskey>klmnop</accesskey>
<secretkey>qrstuvwxyz</secretkey>
</connectiondetails>
</instance>
</adaptorapp>
Now  to generate Java Objects we need to create one xsd file(say "connection.xsd") :
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="adaptorapp" type="AdaptopApp" />
<xs:complexType name="AdaptopApp">
<xs:sequence>
<xs:element name="instance" type="Instance" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="Instance">
<xs:sequence>
<xs:element name="connectiondetail" type="ConnectionDetail" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="ConnectionDetail">
<xs:sequence>
<xs:element name="accesskey" type="xs:string" />
<xs:element name="secretkey" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
I have written XSD Schema based on my XML file, You can write something similar according to your requirement.

Lets use the 'xjc" tool to generate corresponding Java classes
                xjc.exe connection.xsd
By default, It will generates the Java classes in a directory named "generated".


Please let me know your feedback/queries as comments.

Thursday, October 9, 2014

Hibernate Error - Caused by: org.hibernate.DuplicateMappingException: duplicate import:

In hibernate, You cannot have two classes with the same name in the same or different packages else you will get an error at runtime like


"Caused by: org.hibernate.DuplicateMappingException: duplicate import: ....(try using auto-import="false")"


I have resolved this issue by adding property name on the Entity annotation.
Let us suppose I have Meta class in two different packages "com.database.user" and "com.database.group', you can resolve it as :
package com.database.user
@Entity(name= "com.database.user")
@Table(name="USER_META")
public class Meta

package com.database.group
@Entity(name= "com.database.group")
@Table(name="GROUP_META")
public class Meta

There must be some other ways too to resolve this issue, So please feel free to share it :)





Wednesday, August 13, 2014

How to upgrade ANT in Eclipse ?

To upgrade the ANT into the Eclipse, First you need to download the latest version of ANT anywhere on you machine.
Once you are done with the download then go to
EclipseWindowsPreferencesAntRuntimeAnt Home and Select the downloaded folder.

Now your Eclipse will use the latest version of ANT :)

How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...