Thursday, 16 October 2014

DEFAULT HIBERNATE CONNECTION DETAILS FOR MYSQL & POSTGRESQL

mysql
-------

driver:  com.mysql.jdbc.Driver
diaelect: org.hibernate.dialect.MySQLDialect
database: mysql
username: root
password: ''
url: jdbc:mysql://localhost:3306/mysql

postgres
------------

driver: org.postgresql.Driver
dialect: org.hibernate.dialect.PostgreSQLDialect
database: hibernatedb
username: postgres
password: password
url: jdbc:postgresql://localhost:5432/postgres

JAXB Marshalling -TRANSFORMING OBJECT TO AN XML FILE



Classes used here:

Customer.java
Address.java
JAXBMarshall.java

Customer and Address are just plain old java objects(POJO).

Annotation used in Customer.java:

@javax.xml.bind.annotation.XmlRootElement;
@javax.xml.bind.annotation.XmlAttribute;
@javax.xml.bind.annotation.XmlElement;

We have to annotate either Getter methods or instance fields with the above annotation.

JAXBMarshall is the main class the transforms the Object into an XML file.

Here, First we need to get an instance of JAXBContext using its static method newInstance();

newInstane() has an argument - class name. In our example, it is Customer.

By using the instance of JAXBContext we have to create a Marshaller.

marshaller instance's marshall() method - marshalls the object state.




Customer.java


package com.marshal.example;


import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author dinesh
 */

@XmlRootElement
public class Customer {
    
    private int id;
    
    private String name;
    
    private String mobileNumber;
    
    private List<Address> address;
    public Customer() {
    }

    public Customer(int id, String name, String mobileNumber, List<Address>) {
        this.id = id;
        this.name = name;
        this.mobileNumber = mobileNumber;
        this.address = address;
    }

    @XmlAttribute
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

   @XmlElement
    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }

    @XmlElement
    public List<Address> getAddress()
        return address;
    }

    public void setAddress(List<Address> address) {
        this.address = address;
    }
      
}

Address.java
package com.marshal.example;

/**
 *
 * @author dinesh
 */
public class Address {
 
     private int streetNumber;
     private String city;
     private String state;
     private String country;
     private Long zipcode;

    public Address() {
    }

    public Address(int streetNumber, String city, String state, 
        String country, Long zipcode) {
        this.streetNumber = streetNumber;
        this.city = city;
        this.state = state;
        this.country = country;
        this.zipcode = zipcode;
    }

    public int getStreetNumber() {
        return streetNumber;
    }

    public void setStreetNumber(int streetNumber) {
        this.streetNumber = streetNumber;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public Long getZipcode() {
        return zipcode;
    }

    public void setZipcode(Long zipcode) {
        this.zipcode = zipcode;
    }
          
}
JAXBMarshall.java
package com.marshal.example;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 *
 * @author dinesh
 */
public class JAXBMarshal {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)  throws JAXBException{
        // TODO code application logic here
        
        JAXBContext context = JAXBContext.newInstance(Customer.class);
        
        Marshaller m=context.createMarshaller();
        
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        
        Address homeaddr =new Address(1,"Warangal","Telangana","India",506002L);
        Address offaddr =new Address(1,"Hyderabad","Telangana","India",500044L);
        
        ArrayList list = new ArrayList<>()<address>
        
        list.add(offaddr);
        list.add(homeaddr);
        
        Customer customer = new Customer(1,"Dinesh","9999111199",list);
        
        try {
            m.marshal(customer, new FileOutputStream("/home/dinesh/customer.xml"));
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }
    
}

Output -XML file


<?xml version="1.0" encoding="UTF-8"?>
<customer id="1">
    <address>
        <city>Hyderabad<city>
         <city>Telangana<city>
         <city>Warangal<city>
         <city>1<city>
         <city>500044<city>
    </address>
      <address>
        <city>Hyderabad<city>
         <city>Telangana<city>
         <city>Warangal<city>
         <city>1<city>
         <city>500044<city>
    </address>
    <mobileNumber>9999111199<mobileNumber>
    <name>Dinesh<name>
<customer>