Monday, March 25, 2013

Hibernate Annotation base (Hibernate 4) demo sample project.



find out jar


hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

       <session-factory>
              <property name="hbm2ddl.auto">create</property>
              <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
              <property name="hibernate.connection.url">jdbc:mysql://localhost/sample</property>
              <property name="hibernate.connection.username">root</property>
              <property name="hibernate.connection.password"></property>
              <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

              <mapping class="com.test.Employee" />
       </session-factory>

</hibernate-configuration>
Test.java
package com.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

public class Test {
public static void main(String[] args) {
       Session session=new AnnotationConfiguration().configure().buildSessionFactory().openSession();
      
       Transaction t=session.beginTransaction();
      
       Employee e1=new Employee();
       e1.setId(1001);
       e1.setFirstName("Himanshu");
       e1.setLastName("Swain");
      
       Employee e2=new Employee();
       e2.setId(1002);
       e2.setFirstName("Sekhar");
       e2.setLastName("Swain");
      
       session.persist(e1);
       session.persist(e2);
      
       t.commit();
       session.close();
       System.out.println("successfully saved");
}
}

Employee.java
package com.test;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name= "sample")
public class Employee {
@Id
private int id;
private String firstName,lastName;

public int getId() {
                return id;
}
public void setId(int id) {
                this.id = id;
}
public String getFirstName() {
                return firstName;
}
public void setFirstName(String firstName) {
                this.firstName = firstName;
}
public String getLastName() {
                return lastName;
}
public void setLastName(String lastName) {
                this.lastName = lastName;
}


}

How to Calculate the Future Investment Value of Investment.

Below java program for calculate future investment value. Here calculate s using the formula futureInvestmentValue =investmentAmount x (...