1、
我封装的 操作Hibernate的类:
1 package hibernateOper; 2 3 import org.hibernate.SessionFactory; 4 import org.hibernate.cfg.Configuration; 5 6 @SuppressWarnings("deprecation") 7 final public class ThibernateOper 8 { // ZC: 上面的"final",让别人无法再继承该类 9 public ThibernateOper()10 {}11 12 // 一般连接一个数据库,只用一个SessionFactory。13 private static SessionFactory FsessionFactory = null;14 static // 静态块,只会执行一次15 {16 // Configuration.configure() 中可以参入参数[hibernate.cfg.xml的文件名和路径]17 FsessionFactory = (new Configuration()).configure().buildSessionFactory();18 }19 20 public static SessionFactory GetSessionFactory()21 {22 return FsessionFactory;23 }24 25 public static void main(String[] args)26 {27 }28 }
2、
对Oracle的简单操作:
1 package test; 2 3 import java.util.Date; 4 5 import hibernateOper.ThibernateOper; 6 7 import org.hibernate.Session; 8 import org.hibernate.SessionFactory; 9 import org.hibernate.Transaction;10 11 import domain.Employee;12 13 public class TTest14 {15 public static void SimpleInsert()16 {17 SessionFactory sf = ThibernateOper.GetSessionFactory();18 Session session = sf.openSession();19 Transaction ts = session.beginTransaction();20 21 try22 {23 Employee e = new Employee();24 e.setName("zz");25 e.setEmail("zzz@163.com");26 e.setHiredate(new Date());27 28 session.save(e);29 ts.commit();30 session.close();31 }32 catch(Exception ex)33 {34 ts.rollback();35 if ( (session != null)&&(session.isOpen()) )36 session.close();37 ex.printStackTrace();38 }39 }40 41 public static void SimpleUpdate()42 {43 SessionFactory sf = ThibernateOper.GetSessionFactory();44 Session session = sf.openSession();45 Transaction ts = session.beginTransaction();46 47 try48 {49 // 强转50 Employee e = (Employee)session.load(Employee.class, 1);51 e.setName("zxcvb");52 53 ts.commit();54 session.close();55 }56 catch(Exception ex)57 {58 ts.rollback();59 if ( (session != null)&&(session.isOpen()) )60 session.close();61 ex.printStackTrace();62 }63 }64 65 public static void SimpleDelete()66 {67 SessionFactory sf = ThibernateOper.GetSessionFactory();68 Session session = sf.openSession();69 Transaction ts = session.beginTransaction();70 71 try72 {73 // 强转74 Employee e = (Employee)session.load(Employee.class, 2);75 e.setName("zxcvb");76 77 session.delete(e);78 ts.commit();79 session.close();80 }81 catch(Exception ex)82 {83 ts.rollback();84 if ( (session != null)&&(session.isOpen()) )85 session.close();86 ex.printStackTrace();87 }88 }89 90 public static void main(String[] args)91 {92 //SimpleInsert();93 //SimpleUpdate();94 SimpleDelete();95 }96 97 }
3、
hibernate.cfg.xml
1 2 5 67 8 9 28 2910 org.hibernate.dialect.Oracle10gDialect11 1213 jdbc:oracle:thin:@localhost:1521:orcl14 15scott 16tiger 1718 oracle.jdbc.driver.OracleDriver19 2021 OracleLocal22 23true 24true 2526 27
4、
Employee.hbm.xml
1 2 4 78 9 2810 1811 14 15 emp_seq16 1719 2120 22 2423 25 2726
5、
Employee.java
1 package domain; 2 3 import java.math.BigDecimal; 4 import java.util.Date; 5 6 /** 7 * Employee entity. @author MyEclipse Persistence Tools 8 */ 9 10 public class Employee implements java.io.Serializable {11 12 // Fields13 14 private static final long serialVersionUID = 1L;15 16 //private BigDecimal id;17 private Integer id;18 private String name;19 private String email;20 private Date hiredate;21 22 // Constructors23 24 /** default constructor */25 public Employee() {26 }27 28 /** full constructor */29 public Employee(Integer id, String name, String email, Date hiredate) {30 this.id = id;31 this.name = name;32 this.email = email;33 this.hiredate = hiredate;34 }35 36 // Property accessors37 38 public Integer getId() {39 return this.id;40 }41 42 public void setId(Integer id) {43 this.id = id;44 }45 46 public String getName() {47 return this.name;48 }49 50 public void setName(String name) {51 this.name = name;52 }53 54 public String getEmail() {55 return this.email;56 }57 58 public void setEmail(String email) {59 this.email = email;60 }61 62 public Date getHiredate() {63 return this.hiredate;64 }65 66 public void setHiredate(Date hiredate) {67 this.hiredate = hiredate;68 }69 70 }
6、