1、建表建序列(所用数据库为Oracle,数据库名为XE,创建用户hibernate,密码为123456)
conn system/manager; create user hibernate identified by 123456; grant connect to hibernate; grant resource to hibernate; conn hibernate/123456; create table tb_Employee( pk_Employee_ID number primary key, name varchar2(64) not null, email varchar2(64) not null, hiredate date not null ); create sequence emp_seq start with 1 increment by 1 minvalue 1 nomaxvalue nocycle nocache;
2、程序结构
3、各个文件
Employee.java
package com.tfj.domain; import java.io.Serializable; import java.util.Date; //这是一个domain对象(实际上就是JavaBean/有些人pojo) //他和Employee对应 public class Employee implements Serializable { private static final long serialVersionUID = 1L; private Integer id; private String name; private String email; private Date hiredate; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public java.util.Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } }
Employee.hbm.xml
<?xml version="1.0"?> <!--该文件要清楚地表述出 类 和 表 的对应关系 --> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2016-1-10 22:39:57 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <!-- name : 表示包中的类名 table 表示 该类和哪个表映射 --> <class name="com.tfj.domain.Employee" table="tb_Employee"> <!-- id元素专门用于指定主键是如何生成,hibernate设计者认为,我们每一个表都应该有一个主键 --> <!-- name:表示类的哪个属性是主键 --> <id name="id" type="java.lang.Integer"> <column name="pk_Employee_ID" /> <!-- 指定主键生成策略 --> <generator class="sequence"> <param name="sequence">emp_seq</param> </generator> </id> <property name="name" type="java.lang.String"> <column name="name" /> </property> <property name="email" type="java.lang.String"> <column name="email" /> </property> <property name="hiredate" type="java.util.Date"> <column name="hiredate" /> </property> </class> </hibernate-mapping>
TestMain.java
package com.tfj.view; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.tfj.domain.Employee; public class TestMain { public static void main(String[] args) { // 添加一个雇员 // 1.得到Configuration Configuration configuration = new Configuration().configure(); // 2.得到SessionFactory(会话工厂,这是一个重量级的类,因此要保证在一个应用程序中只能有一个) SessionFactory sessionFactory = configuration.buildSessionFactory(); // 3. 从SessionFactory中取出一个Session对象(它表示 和数据库的出一次会话) Session session = sessionFactory.openSession(); // 4. 开始一个事务 Transaction transaction = session.beginTransaction(); // 保存一个对象到数据库(持久化一个对象) Employee emp = new Employee(); emp.setName("tufujie"); emp.setEmail("tufujiepuyang@foxmail.com"); emp.setHiredate(new Date()); // 不要给id,因为它是自增的 session.save(emp);// insert into tb_Employee (name, email, hiredate, pk_Employee_ID) values // (?, ?, ?, ?) transaction.commit();
session.close(); } }
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"> <!-- 该文件用于配置连接数据的种类,用户名,密码,ul ,驱动.. 连接池,二级缓存.. 有点类似strus struts-config.xml --> <hibernate-configuration> <session-factory> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property> <property name="connection.username">hibernate</property> <property name="connection.password">123456</property> <!-- 配置显示hibernate生成的 sql ,特别说明,在开发阶段设为true利于调试,在使用项目则设为false--> <property name="show_sql">true</property> <!-- 配置数据库的方言/ --> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <!-- 配置管理的对象映射文件 --> <mapping resource="com/tfj/domain/Employee.hbm.xml"/> </session-factory> </hibernate-configuration>