第一次学习hibernate,跟着网上的去做,到处报错,自己做了一遍,终于好了!
一、 实验环境
eclipse、oracle11g
然后就是所需的jar包,到http://www.hibernate.org下载相应的jar,我的是hibernate-release-4.2.21.Final,解压后所需要jar为 \hibernate-release-4.2.21.Final\lib\required;jdbc包ojdbc6.jar和geronimo-jta_1.1_spec-1.1.1.jar(另外找的,调试的时候有错误,网上说要这个jar,后来也不知道做什么的了),导入eclipse你的项目中就好。
哦,记得在数据库中建表,我的表是USERS
注意自己的元素类型什么的,不然后面做的时候很容易错。
为userid设置一个作为自动增长的数
CREATE SEQUENCE user1_sequence
INCREMENT BY 1 -- 增量
START WITH 1 -- 起始值1
NOMAXVALUE -- 最大值maxvalue 99999999这里设置为没有最大值
NOCYCLE -- 达到最大值后是否重新计算,当前为不重新计算,cycle为重新计算
NOCACHE -- 不要缓存,容易跳号
order;
到这里要注意两个点:
我们建立的序列是要在eclipse中添加记录时用到的,因为是从1开始增长,如果本来表中有了记录,而userid作为主键有可能会重复而报错唯一约束。
好像是哪个用户创建的序列user1_sequence只能由这个用户用,你用plsqldeveloper登录oracle的用户创建序列,在eclipse中登录数据库的用户名不同,是会报序列不存在的错误的。
二、配置文件
有两个吧,自己添加hibernate.cfg.xml和Users.hbm.xml。
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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!--以格式良好的方式显示SQL语句-->
<property name="format_sql">true</property>
<!--显示SQL语句-->
<property name="show_sql">true</property>
<!-- 方言类 根据oracle版本是不同的-->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!--连接Oracle的驱动类名-->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!--连接Oracle的IP地址及端口号-->
<property name="connection.url"> jdbc:oracle:thin:@localhost:1521:数据库名</property>
<!--连接Oracle的用户名-->
<property name="connection.username">用户名</property>
<!--连接Oracle的密码-->
<property name="connection.password">密码</property>
<!--数据库连接的名称,只是作为备注或者标识的属性 -->
<!--此属性值为定义数据库连接的一个自定义名称 -->
<property name="myeclipse.connection.profile">OracleDriver</property>
<property name="current_session_context_class">thread</property>
<!--加入映射-->
<mapping resource="Users.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Users.java
package com.lz.model;
public class Users {
private int userid;
private String username;
private String password;
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Users.hbm.xml(这个是对应类Users的,Users.hbm.xml是要加入到hibernate.cfg.xml中的)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.lz.model.Users" table="USERS">
<id name="userid" type="java.lang.Integer">
<column name="userid" length="10" />
<generator class="native">
<param name="sequence">user1_sequence</param>
</generator>
</id>
<property name="username" column="username" type="java.lang.String"/>
<property name="password" column="password" type="java.lang.String"/>
</class>
</hibernate-mapping>
注意主键在<id></id>中,其自动增长序列的设置;name和column为Users.java和数据库表之间的对应。
package com.lz.test;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import com.lz.model.Users;
public class Test {
private static SessionFactory sf;
private static ServiceRegistry serviceRegistry;
public static void main(String[] args) {
System.out.println("1111111111");
try
{
//通过Configuration获得一个SessionFactory对象
Configuration configuration =new Configuration();
configuration.configure();
serviceRegistry =new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sf = configuration.buildSessionFactory(serviceRegistry);
//打开一个Session
Session session = sf.openSession();
//开始一个事务
Transaction tx = session.beginTransaction();
//创建一个Student对象
Users stu = new Users();
//通过Student的setter方法改变它的属性
//注意student_id不用我们设置
stu.setUsername("lili");
stu.setPassword("12345");
//通过session的save()方法将Student对象保存到数据库中
session.save(stu);
//提交事务
tx.commit();
//关闭会话
session.close();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("1111111111");
}
}
注意新老版本获取configuration对象的写法是不同的,hibernate4.2以上的版本是想上面这样的,4.2以下的就不同了看你自己的版本吧,网上一搜都有的。
然后测试运行就好了,一般报错就基本上是jar包的问题,没有或者版本不对,还有就是注意其他东西的版本,有很多不同版本是有变化的。