Thursday, August 23, 2012

Resolving Maven dependencies in eclipse

In order to resolve Maven dependencies in eclipse variable M2_REPO should be added to the classpath variables of the eclipse.

We can check the list classpath variables in eclipse at

Window ->Preferences->Java->Build Path ->Classpath Variables

In this view new classpath variable M2_REPO can be added directly. It should point to the location of Maven repository and not the home location of Maven



Other way is to do through console.

Run command:

mvn -Declipse.workspace="Your Workspace" eclipse:configure-workspace

It will add the variable in eclipse workspace.

mvn -Declipse.workspace="C:\Users\aggara4\SpringWorkSpace" eclipse:configure-workspace


[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building SampleWebProject Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-eclipse-plugin:2.9:configure-workspace (default-cli) @ SampleWebProject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.015s
[INFO] Finished at: Thu Aug 23 13:47:12 IST 2012
[INFO] Final Memory: 5M/9M
[INFO] ------------------------------------------------------------------------



Monday, February 20, 2012

Creating Foreign Key RelationShip in Hibernate

I am just starting exploring Hibernate in detail. I always wanted to see how just writing pojo classes make DB operations fun.

For my recent exploration i wanted to create Foreign Key relation between two tables. Here is a simple approach i tired:

Create two tables : Person and Person_Email.
As one person can have multiple emails, so these two tables have a one-to-many relation which means one entity of Person can have amy entities of Person_Email dependent on it.

Here is the schema of two tables ( i am using MYSQL DB as its a freeware).

Person:

PERSON_ID               int(10),
AGE                            smallint(6),
FIRSTNAME              varchar(10),
LASTNAME               varchar(10)

Person_Email:

PERSON_ID               int(10),

EMAIL_ADDRESS    varchar(255) 

Here is the corresponding POJO class. Note we dont have any POJO class for Person_Email table, as i will be taken care in Person.hbm.xml which we will see later

public class Person {


private long id;
private int age;
private String firstName;
private String lastName;
private Set emails;

......
......

public Set getEmails() {
    return emails;
}


public void setEmails(Set emails) {
   this.emails = emails;
}

}

Here I have ommitted the getters and setters of fields of Person tables. One thing to notice is that we have defined a Set of emails. In normal java concept, it means that Person class will have a Set object which holds all the email addresses associated with particular peron entity.

Below is the mapping class of Person Table:



<hibernate-mapping package="com.hibernate.tutorial.bean">

        <class name="Person" table="PERSON">
                <id column="PERSON_ID" name="id"></id>
                       <generator class="increment">
               
                <property name="firstName">
                <property name="lastName">
               
                <set name="emails" table="PERSON_EMAIL"></set>
                        <key column="PERSON_ID"></key>
                        <element column="email_address" type="string"></element>
....
....
....      


In mapping file we will define the relationship between two tables.

<set name="emails" table="PERSON_EMAIL"></set>

It shows that instance property emails defined in Person class is related to table Person_Email.

<key column="PERSON_ID"></key> 

Key property shows the coolumn of Person class which is used to create the relation between two entities. We can see that PERSON_ID is a foreign key in Person_Email table.

<element column="email_address" type="string"></element>


Element property shows the rest of the columns present in Person_email table and the type of those columns.

Below is the test code to see how the values will be inserted in both the tables.

Person person = (Person)session.load(Person.class, 1001);
person.getEmails().add(xyz@gmail.com);
session.save(person);

Here i have loaded the existing person entity from database and then adding the email address to the Set.

session.save will save the person entity and because person_email table is mapped to person table (as defined in mapping file), so new row will be inserted in Person_Email table

Wednesday, January 4, 2012

Convert XML to Java Objects

Starting from java 5, JDK has inbuilt support for JAXB. JAXB (Java Architecture for XML Binding) is an efficient way of converting xml to Java objects.

JDK provides a class javax.xml.bind.JAXBContext to create java objects from xml.

Below is the code to demonstrate it:


JAXBContext jaxbContext = JAXBContext.newInstance("com.test.jaxb.xngevents.dtd.cable:"
+"com.test.jaxb.xngevents.dtd.equipment");

Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();

com.test.jaxb.xngevents.dtd.equipment.XngEvent xngEvent = (com.test.jaxb.xngevents.dtd.equipment.XngEvent)unMarshaller.unmarshal(new File("Q1.xml"));




JAXBContext.newInstance method takes list of packages separated by colon. These packages must contain ObjectFactory class or jaxb.index file. When an xsd is converted to JAXB object then ObjectFactory class is automatically created. This means that classes contained in the packages must be schema derived classes. If the classes are not derived then exception like below will be thrown:


javax.xml.bind.JAXBException: doesnt contain ObjectFactory.class or jaxb.index


There is one more overloaded newInstance method in JAXBContext which takes the Class arguments:


Once the JAXBContext is initialized, Unmarshaller is created. Unmarshaller.unmarshal will read the xml file or xml as String and create the java objects.