Thursday, October 30, 2014

How to Map a list of strings with JPA/Hibernate annotations ?

Yesterday while working on my project, I got one requirement where I need to store list of Array into the database, I checked on Google, went through various site but didn't get much information. Everyone talks about creating new entity and do onetomany relationship BUT I wanted to create a collection of basic types.

Finally I come across @ElementCollection annotation provided by JPA 2.0 which resolves my problem.

Problem :
Input JSON which we need to store in DB:

"schemas": ["urn:scim:schemas:core:1.0", "urn:scim:schemas:extension:enterprise:1.0"]
           Where schemas is Array of String.
Solution :

Just add following annotation in your POJO i,e
@CollectionTable(name = "SCIM_SCHEMAS", joinColumns = @JoinColumn(name = "SCHEMA_ID"))
@Column(name = "SCIM_SCHEMA")
private List<String> schemas;

It will create the table named "SCIM_SCHEMAS" having columns 'SCHEMA_ID' and 'SCIM_SCHEMA'

For details check section Collection of basic types or embeddable objects or feel free to comment :)

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">
<instance id="instance2">
Now  to generate Java Objects we need to create one xsd file(say "connection.xsd") :
<?xml version="1.0"?>
<xs:schema xmlns:xs="">
<xs:element name="adaptorapp" type="AdaptopApp" />
<xs:complexType name="AdaptopApp">
<xs:element name="instance" type="Instance" maxOccurs="unbounded" />
<xs:attribute name="id" type="xs:string" use="required" />
<xs:complexType name="Instance">
<xs:element name="connectiondetail" type="ConnectionDetail" />
<xs:attribute name="id" type="xs:string" use="required" />
<xs:complexType name="ConnectionDetail">
<xs:element name="accesskey" type="xs:string" />
<xs:element name="secretkey" type="xs:string" />
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 "', you can resolve it as :
package com.database.user
@Entity(name= "com.database.user")
public class Meta

@Entity(name= "")
public class Meta

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