Saturday, August 9, 2014

Maven + Hibernate + Setter + Getter


/**
 *
 * @author sakib4
 */
public class InvDclStockCategory  implements java.io.Serializable {
 
    private int id;
    private String description;
     private Date creatDate;
     private Date lastUpdateDate;
     private String inactive;

    public InvDclStockCategory() {
    }

   
    public InvDclStockCategory(int id) {
        this.id = id;
    }
    public InvDclStockCategory(int id, String description, Date creatDate,Date lastUpdateDate , String inactive ) {
       this.id = id;
       this.description = description;
       this.creatDate = creatDate;
       this.lastUpdateDate = lastUpdateDate;
       this.inactive = inactive;
    }
  
    public int getId() {
        return this.id;
    }
   
    public void setId(int id) {
        this.id = id;
    }
    public String getDescription() {
        return this.description;
    }
   
    public void setDescription(String description) {
        this.description = description;
    }
   
   
    public void setCreatDate(Date creatDate) {
        this.creatDate = creatDate;
    }
   
    public Date getCreatDate() {
       return this.creatDate = creatDate;
    }

   
    public Date getLastUpdateDate() {
        return this.lastUpdateDate;
    }
   
    public void setLastUpdateDate(Date lastUpdateDate) {
        this.lastUpdateDate = lastUpdateDate;
    }

    public String getInactive() {
        return this.inactive;
    }
   
    public void setInactive(String inactive) {
        this.inactive = inactive;
    }
   
}

Maven + Hibernate + Select & Result List()

Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Query q = session.createQuery(hql);
            List resultList = q.list();
            displayResult(resultList);
            session.getTransaction().commit();

private void displayResult(List resultList) {
        Vector<String> tableHeaders = new Vector<String>();
        Vector tableData = new Vector();
        tableHeaders.add("Id");
        tableHeaders.add("Category Name");
        tableHeaders.add("Status");
        tableHeaders.add("LastUpdate Date");

        for (Object o : resultList) {
            InvDclStockCategory Cat = (InvDclStockCategory) o;
            Vector<Object> oneRow = new Vector<Object>();
            oneRow.add(Cat.getId());
            oneRow.add(Cat.getDescription());
            oneRow.add(Cat.getInactive());
            oneRow.add(Cat.getLastUpdateDate());
         
            tableData.add(oneRow);
        }
        resultTable.setModel(new DefaultTableModel(tableData, tableHeaders));

    }

Maven + Hibernate Session Delete

 Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();     
   
                Student userAdd =  (Student)session.get(Student.class, Id);
             
                session.delete(userAdd);
                session.getTransaction().commit();
                session.close();

Maven + Hibernate Session Update

      Session session = HibernateUtil.getSessionFactory().openSession();
          session.beginTransaction();     
 InvDclStockCategory cat = InvDclStockCategory)session.get(InvDclStockCategory.class, Id);
  
                cat.setDescription(Name);
                cat.setInactive(Status);
                
                session.update(cat);
                session.getTransaction().commit();
                session.close();

Maven + Hibernate Session Save

Session session = HibernateUtil.getSessionFactory().openSession();
          session.beginTransaction();     
 
                InvDclStockCategory cat = new InvDclStockCategory();
  
                cat.setDescription(Name);
                cat.setInactive(Status);
                session.save(cat);
                session.getTransaction().commit();
                session.close();
               

Maven + Hibernate Mappings + Auto Increment Sequence Uses

 <!-- hbm.xml files settings -->

<hibernate-mapping>
    <class name="com.datacraft.mavenproject1.InvDclStockCategory" table="inv_dcl_stock_category" schema="public">
        <id name="id" type="int">
            <column name="id" />
               
             <generator class="sequence">
                 <param name="sequence">inv_dcl_stock_category_id_seq</param>
             </generator>
         
        </id>
        <property name="description" type="string">
            <column name="description" length="255" />
        </property>
      
        <property name="inactive" type="string">
            <column name="inactive" />
        </property>
        <property name="creatDate" type="date">
            <column name="create_date" length="13"  />
        </property>
        <property name="lastUpdateDate" type="date">
            <column name="last_update_date" length="13" />
        </property>
    </class>