Posts

Showing posts from 2014

How to extract a JSON value from a BASH script

Requirement : I was working on shell script where I need to get Id value from JSON and I tried so many thing but unable to get it without using any other tools. So, After trying so many failure attempt, I end up with this site (A big thanks to you) which resolved my issue. Here is how I fetched value from JSON, JSON = <YOUR_JSON> PROPERTY_TO_FETCH="<VALUE>" (In my case, it was "id") ID = `echo $JSON| sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $PROPERTY_TO_FETCH` echo ${ID ##*|} Which will print the value of 'id' from JSON.

How to execute maven goal on parent module but not on childer

I am working on multi module maven project and If I define any plugin in the parent pom.xml file, It get executed for all the child build as well. In simple language, I wanted to use one plugin in parent pom.xml file which read properties file from the base directory but whenever I executes it, all the sub pom.xml tries to read the properties file from their base directory and its ends up in maven failed. To resolve the above issue, We just need to add "inherit' attributes in the plugin. Example : <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <version>1.0-alpha-2</version> <inherited>false</inherited> <executions> <execution> <phase>initialize</phase> <goals> <goal>read-project-properties</goal> </goals> <configuration> <files> ...

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

Image
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 @ElementCollection @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 'SCHE...

How to create Java Classes using xjc from XML file

Image
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...

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 :)

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 Eclipse → Windows → Preferences → Ant → Runtime → Ant Home and Select the downloaded folder. Now your Eclipse will use the latest version of ANT :)

How to exclude properties file from the Jar file

Problem Statement : Suppose your are working on Java Project which uses Maven structure where your source code is under “ /src/main/java” and all your configuration files like .properties file or xml file are under “ /src/main/resources” and as a final build your final package is a jar but you want without few files from your classpath. Be default Maven plugin adds all the file which comes under resources in jar file. Solution : You can achieve the above task by using maven-jar-plugin as follows : < plugin >     < groupId > org.apache.maven.plugins </ groupId >     < artifactId > maven-jar-plugin </ artifactId >     < version > 2.3 </ version >     < configuration >         < excludes >             < exclude > **/otp.properties </ exclude >         </ excludes >   ...

maven-dependency-plugin (goals “copy-dependencies”, “unpack”) is not supported by m2e

Recently While importing one Maven project into Eclipse(Juno, m2e plug-ins is already installed in Eclipse). I was getting one error in project : maven-dependency-plugin (goals “copy-dependencies”, “unpack”) is not supported by m2e Reason: Eclipse m2e does not support execution, by copying the below code in the build tag resolve the issue. Example: <build> <COPY_CODE_HERE> </build> Code : < pluginManagement > < plugins > <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself. --> < plugin > < groupId > org.eclipse.m2e </ groupId > < artifactId > lifecycle-mapping </ artifactId > < version > 1.0.0 </ version > < configuration > < lifecycleMappingMetadata > < pluginExecutions > < pluginExecution > <...

How to upgrade or downgrade data schema ?

Image
I ll continue with this tutorial to show how to upgrade or downgrade the database schema. Suppose we need to add one more field ManagerAddress in our existing Managers tables, To do this we need to use one feature called Migrations . It allows u s to have an ordered set of steps that describe how to upgrade (and downgrade) our database schema. Each of these steps, known as a migration, contains some code that describes the changes to be applied. Tools → NuGet Package Manager → Package Manager Console Run the Enable-Migrations command in Package Manager Console The two new file get created under Migrations folder : Configuration.cs : It contains the setting that Migrations will use for migrating ManagingContext. <timestamp>_InitialCreate.cs  – This is your first migration, it represents the changes that have already been applied to the database to take it from being an empty database to one that includes the Manager and Employee t...

Entity Framework : How to generate model using Entity Framework Design

Image
This is a step-by-step walkthrough which will generate the Model using Entity Framework Designer and then generate a database schema from the model. The model is stored in a an EDMX file Pre-Requisities We need to have Visual Studio 2013 installed to complete this walkthrough.(You can use older version too like 2010 or 2012 to complete this tutorial.) Apart from that, You will also need to have NuGet installed on your Visual Studio. 1. Create new Application Steps : Create new Application Open Visual Studio, Click File → New → Project Click Console Application under templates → Visual C# → Windows Enter Name as ModelFirstApplication and Select OK 2. Create Model To create model, I will use Entity Framework Designer In Solution Explorer , Right Click on your Project and then Add New Item... Select Data from the left menu and then ADO.NET Entity Data Model Enter Name as Managing Context and click OK Now Se...

Entity Framework : How to create code using Existing Database

Image
This is a step-by-step walkthrough which will use existing database to create the model. We will create one console Application which will generate the model using existing databases. Pre-Requisities We need to have Visual Studio 2013 installed to complete this walkthrough.(You can use older version too like 2010 or 2012 to complete this tutorial.) Apart from that, You will also need to have NuGet installed on your Visual Studio and the 6.1 or later of the Entity Framework Tools . 1. Connect to an Existing Database Check here to see how can you connect to existing database. I have already created one database in my previous blog . I ll use the same database( CreateNewDatabase.ManagingContext ) to generate the model. 2. Create the Application Open Visual Studio, Click File → New → Project Click Console Application under templates → Visual C# → Windows Enter Name as CreateModelUsingExistingDatabase and Select OK 3. Reverse ...