TestGson.java Source code

Java tutorial

Introduction

Here is the source code for TestGson.java

Source

import com.google.gson.Gson;
import control.dco.StaffDCO;
import java.rmi.RemoteException;
import model.entity.Staff;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import prj2.lw.bean.StaffBean;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Wing
 */
public class TestGson {

    public static void main(String[] args) throws RemoteException {
        StaffDCO dco = new StaffDCO();
        Gson gson = new Gson();
        Session ses = utils.HibernateUtils.getSessionFactory().getCurrentSession();
        try {
            ses.beginTransaction();
            Criteria crit = ses.createCriteria(Staff.class);
            crit.add(Restrictions.eq("id", 1));
            Staff s = (Staff) crit.uniqueResult();
            ses.getTransaction().commit();
            StaffBean bean = dco.toStaffBean(s);
            String v = gson.toJson(bean, StaffBean.class);
            System.out.println(v);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}