step 1)工具下载
1.1)下载hibernate-3.0
1.2)下载ojdbc14_g.jar(oracle 9i的驱动)
1.3)在eclipse环境下建立两个库(windows-->preferences-->java-->buildpath-->user library) hibernate(hibernate-3.0/lib下所有jar),jdbc(包括ojdbc14_g.jar)
step2)具体开发
建立一个 项目 ,引入刚才建立的两个库
2.1)
建一个src源文件目录,输出路径设为classes,在src下建立hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 是否将运行期生成的SQL输出到日志以供调试 -->
<property name="show_sql">true</property>
<!-- SQL方言,这里设定的是MySQL -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<!-- JDBC驱动程序 -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!-- JDBC URL, "?useUnicode=true&characterEncoding=GBK" 表示使用GBK进行编码 -->
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:oracle9i
</property>
<!-- 数据库用户名 -->
<property name="connection.username">pwlazy</property>
<!-- 数据库密码 -->
<property name="connection.password">770216</property>
<!-- 指定User的映射文件 -->
<mapping resource="hello/message.hbm.xml"/>
</session-factory>
</hibernate-configuration>
注意dtd一定要写对,否则会抛错!
2.2)
在数据库建立一个messages表
-- Create table
create table MESSAGES
(
ID NUMBER not null,
NEXT_ID NUMBER,
TEXT VARCHAR2(20)
)
tablespace OEM_REPOSITORY
pctfree 10
pctused 40
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table MESSAGES
add constraint KEY primary key (ID)
using index
tablespace OEM_REPOSITORY
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
2.3)写POJO,与该表相关
package hello;
/**
* @author Administrator
*
*
*/
public class Message {
private Long id;
private String text;
private Message nextMessage;
private Message() {}
public Message(String text) {
this.text = text;
}
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Message getNextMessage() {
return nextMessage;
}
public void setNextMessage(Message nextMessage) {
this.nextMessage = nextMessage;
}
}
2.4)写POJO的映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="hello.Message" table="MESSAGES">
<id name="id"
column="ID">
<generator class="increment"/>
</id>
<property
name="text"
column="TEXT"/>
<many-to-one
name="nextMessage"
cascade="all"
column="NEXT_ID"/>
</class>
</hibernate-mapping>
注意dtd一定要写对,否则会抛错!
2.5)写客户代码
package hello;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/**
* @author Administrator
*
*
*/
public class Test {
public static void main(String[] args) {
try {
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Message message = new Message("Hello");
session.save(message);
tx.commit();
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
即望数据库中插入一条记录
分享到:
相关推荐
NULL 博文链接:https://huangminwen.iteye.com/blog/1312068
hibernate的helloworld版。下载下来可以直接运行。
Hibernate教程01_Hibernate的HelloWorld及基本配置 http://blog.csdn.net/e421083458/article/details/8794127 该源码为Hibernate教程配套源码
Hibernate 4——Hello World
hibernate-helloworld-native
一个简单的hibernate helloworld 下载下来就能用
用Spring MVC+hibernate 写的一个HelloWorld小实例,加入了一个简单验证。
如果你没学过hibernate,但是又想看一下它的最简单形式。这个应该适合你。
版本差异太难搞了,找半天才找到错误,主要是包冲突 NoSuchMethodError: org.jboss....NoClassDefFoundError: org/hibernate/cache/spi/RegionFactory org.hibernate.cache.spi.RegionFactory 记录下来,以备不时
Hibernate 4.0版helloworld,项目需要Maven管理库依赖,项目包含Oracle建表语句
Hibernate_HelloWorld 使用Netbeans和SQL Server的HelloWorld Hibernate
Hibernate01-简介和HelloWorld 例子代码,实现了简单新增,测试类需要引进junit4
ssh_helloworld(初学参考),初学者学习的例子,供学习参考
hibernate 使用及与oracle连接的方式
主要介绍了Hibernate的Annotation版Hello world实现方法,详细分析了Annotation的具体使用步骤与Hello world实现方法,需要的朋友可以参考下
这是hibernate 的超级简单的例子,只有一个持久化对象和一个辅助类,还有一个测试类,对于初学者很有参考价值
NULL 博文链接:https://chentian114.iteye.com/blog/2295338
这是开发hibernate框架的framework官方中文文档,可以参照此文档写hibernate的helloworld程序
本章学习目标 ... Hibernate 的HelloWorld hibernate.cfg.xml 配置详解 *.hbm.xml 对象映射文件详解 Hibernate 核心接口详解 抽取HibernateUtil 工具类 使用Session 接口进行增删改查
JPA的HelloWorld实现,使用实现JPA规范的Hibernate持久化框架