hibernate crud anotation example:
-------------------------------------------------------------
Department .java
-------------------
package madhav;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Department {
@Id
private int deptno;
private String dname;
private String loc;
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Department {
@Id
private int deptno;
private String dname;
private String loc;
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
hibernate.cfg.xml
-------------------
DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mysql</property>
<property name="connection.username">root</property>
<property name="connection.password">mysql</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="madhav.Department" />
</session-factory></
hibernate-configuration>
<!-- Generated by MyEclipse Hibernate Tools. -->
<
hibernate-configuration>
hibernate-configuration>
----------------
package madhav;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;
public class TestCase {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// Create the SessionFactory from hibernate.cfg.xml
SessionFactory sf= new
AnnotationConfiguration().configure().buildSessionFactory();
Session s=sf.openSession();
Transaction tx=s.beginTransaction();
Department d=new Department();
d.setDeptno(8);
d.setDname("hugo");
d.setLoc("hyd");
s.save(d);
tx.commit();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
}
}
}
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;
public class TestCase {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// Create the SessionFactory from hibernate.cfg.xml
SessionFactory sf= new
AnnotationConfiguration().configure().buildSessionFactory();
Session s=sf.openSession();
Transaction tx=s.beginTransaction();
Department d=new Department();
d.setDeptno(8);
d.setDname("hugo");
d.setLoc("hyd");
s.save(d);
tx.commit();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
}
}
}
No comments:
Post a Comment