package str;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
public class ClientForSave {
public static void main(String[] args)
{
AnnotationConfiguration cfg=new AnnotationConfiguration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Product p=new Product();
p.setProductId(303);
p.setProName("choclate");
p.setPrice(8000);
Transaction tx = session.beginTransaction();
session.save(p);
System.out.println("Object saved successfully using annotations.....!!");
tx.commit();
session.close();
factory.close();
}
}
------------------------------------------------------------------------------------------------------------------------
Product Class file
Product Class file
package str;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "student_table")
public class Product{
@Id
@Column(name="proid")
private int productId;
@Column(name="proName", length=10)
private String proName;
@Column(name="price")
private double price;
public void setProductId(int productId)
{
this.productId = productId;
}
public int getProductId()
{
return productId;
}
public void setProName(String proName)
{
this.proName = proName;
}
public String getProName()
{
return proName;
}
public void setPrice(double price)
{
this.price = price;
}
public double getPrice()
{
return price;
}
0 Comments