说明:数据库:Oracle10g;连接池:c3p0
结构:
一、配置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"> <!-- c3p0连接池 --> <hibernate-configuration> <session-factory> <!-- 显示实际操作数据库时的SQL --> <property name="show_sql">true</property> <!-- SQL方言,这边设定的是Oracle --> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!--驱动程序:Oracle数据库的配置 --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <!-- JDBC URL --> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:myorcl</property> <!-- 数据库用户名 --> <property name="connection.username">scott</property> <!-- 数据库密码 --> <property name="connection.password">root</property> <!-- 指定连接池里最小连接数 --> <property name="hibernate.cp30.minsize">5</property> <!-- 指定连接池里的最大连接数 --> <property name="hibernate.c3p0.maxsize">20</property> <!-- 指定连接池里的超时时常 --> <property name="hibernate.cp30.timeout">1800</property> <!-- 指定连接池里最大缓存多少个Statement对象 --> <property name="hibernate.cp30.max_statements">50</property> <!-- 根据需要自动创建数据库表 --> <property name="hbm2ddl.auto">update</property> <!-- 对象与数据库表格映像文件 --> <mapping resource="com/chen/vo/News.hbm.xml"/> </session-factory> </hibernate-configuration>
二、配置News.hbm.xml
说明:由于连接的数据库是Oracle,所有这里指定了序列:seq_news,如果设置<generator class="native"/>,则在插入数据时Hibernate默认会去查找Oracle中的hibernate_sequence序列(默认序列)。
<?xml version="1.0" encoding="utf-8"?> <!-- 指定Hibernate映射文件的DTD信息 --> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- hibernate-mapping是映射文件的根源素 --> <hibernate-mapping> <!-- 每个class对应一个持久化对象 --> <class name="com.chen.vo.News" table="news_table"> <!-- id元素定义持久化类的标识属性 --> <id name="id"> <generator class="sequence"> <param name="sequence">seq_news</param> </generator> </id> <!-- property元素定义常规属性 --> <property name="title"/> <property name="content"/> </class> </hibernate-mapping>
三、创建vo
package com.chen.vo; public class News { //消息类的标识属性 private Integer id; //消息标题 private String title; //消息内容 private String content; //id属性的setter和getter方法 public void setId(Integer id) { this.id=id; } public Integer getId() { return this.id; } //title属性的setter和getter方法 public void setTitle(String title) { this.title=title; } public String getTitle() { return this.title; } //content 属性的setter和getter方法 public void setContent(String content) { this.content=content; } public String getContent() { return this.content; } }
四、创建测试类
package com.chen.vo; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; public class NewsManager { public static void main(String[] args) throws Exception { //实例化configuration Configuration conf =new Configuration().configure(); //以Configuration创建SessionFactory.注:buildSessionFactory方法已过时,不过仍可以获取SessionFactory。 // SessionFactory sf=conf.buildSessionFactory(); //采用ServiceRegistry获取SessionFactory StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder(); ServiceRegistry serviceRegistry = serviceRegistryBuilder.applySettings(conf.getProperties()).build(); SessionFactory sf = conf.buildSessionFactory(serviceRegistry); //创建session Session sess = sf.openSession(); //开始事物 Transaction tx = sess.beginTransaction(); //创建消息实例 News n=new News(); //设置消息标题和消息内容 /** * Oracle插入数据说明: * 1.在News.hbm.xml中设置<generator class="native"/> * 连接Oracle,默认序列(Default.sequence)为hibernate_sequence * 使用native时Hibernate默认会去查找Oracle中的hibernate_sequence序列,所以插入数据时无需设置id。 * 2.在News.hbm.xml中设置指定序列: * <generator class="sequence"> <param name="sequence">seq_news</param> </generator> * Hibernate会按照序列seq_news创建新数据。 */ n.setTitle("title2"); n.setContent("content2"); //保存消息 sess.save(n); //提交事务 tx.commit(); //关闭Session sess.close(); sf.close(); } }
测试结果:
附:Oracle创建序列seq_news及相应的自动增长触发器
create sequence seq_news; CREATE OR REPLACE TRIGGER "SCOTT"."trigger_news" before insert on "SCOTT"."NEWS_TABLE" for each row begin if inserting then if :NEW."ID" is null then select seq_news.nextval into :NEW."ID" from dual; end if; end if; end; / ALTER TRIGGER "SCOTT"."trigger_news" ENABLE;
时间: 2024-12-13 16:00:04