Hibernate + Oracle 创建自增序列ID

Posted Roni

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate + Oracle 创建自增序列ID相关的知识,希望对你有一定的参考价值。

1.创建自增序列

2.对ID创建触发器

3.Userinfo.hbm.xml使得<generator class="increment">

序列:

CREATE SEQUENCE  "SYSTEM"."SEQUENCE_ID"  MINVALUE 1 MAXVALUE 99999999999 INCREMENT BY 1 START WITH 61 CACHE 20 ORDER  NOCYCLE ;

触发器:

create or replace trigger TRIG_USERINFO    
before insert on "USERINFO"    
for each row 
begin     
    select SEQUENCE_ID.nextval into :NEW."ID" from dual;      
end;

Userinfo.hbm.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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="org.bean.Userinfo" table="USERINFO" schema="SYSTEM">
        <id name="id" type="java.lang.Integer">
            <column name="ID" precision="9" scale="0" />
            <generator class="increment">
            </generator>
        </id>
        <property name="username" type="java.lang.String">
            <column name="USERNAME" length="50" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="PASSWORD" length="50" />
        </property>
    </class>
</hibernate-mapping>

DAO:

package org.dao;

import java.util.List;

import org.bean.Userinfo;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.util.HibernateSessionFactory;

public class UserDAO {
    public Session session;
    //获得session方法
    
        public void getCurrentSession(){
            //调用HibernateSessionFactory的getSession方法创建Session对象
            session = HibernateSessionFactory.getSession();
        }
        
        //关闭sesson方法
        public void closeSession(){
            if(session != null){
                HibernateSessionFactory.closeSession();
            }
        }
        
        //插入一条记录方法
        public void save(String username,String password) {
            Transaction t1 = session.beginTransaction();
            Userinfo userinfo = new Userinfo();
            userinfo.setUsername(username);
            userinfo.setPassword(password);
            session.save(userinfo);
            t1.commit();
        }
        
        //修改这条方法
        public void update(int id, String username){
            Transaction t2 = session.beginTransaction();
            Userinfo userinfo = (Userinfo) session.get(Userinfo.class, id);
            userinfo.setUsername(username);
            session.update(userinfo);
            t2.commit();
        }
        
        //查询数据库结果方法
        public void queryAllUser(){
            try{
                Query query =  session.createQuery("from Userinfo");
                List list = query.list();
                for (int i = 0; i < list.size(); i++) {
                    Userinfo user = (Userinfo)list.get(i);
                    System.out.println(user.getId() + " " + user.getUsername());
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        //删除该条记录方法
        public void deleteUser(int id) {
            Transaction t3 = session.beginTransaction();
            Userinfo userinfo = (Userinfo) session.get(Userinfo.class, id);
            session.delete(userinfo);
            t3.commit();
        }
}

Userinfo.java:

package org.bean;

/**
 * Userinfo entity. @author MyEclipse Persistence Tools
 */

public class Userinfo implements java.io.Serializable {

    // Fields

    private Integer id;
    private String username;
    private String password;

    // Constructors

    /** default constructor */
    public Userinfo() {
    }

    /** full constructor */
    public Userinfo(String username, String password) {
        this.username = username;
        this.password = password;
    }

    // Property accessors

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

 

以上是关于Hibernate + Oracle 创建自增序列ID的主要内容,如果未能解决你的问题,请参考以下文章

Oracle实现主键字段自增

Oracle 怎样查询所有自增的序列名

oracle自增主键从多少开始

oracle中如何创建表的自增ID(通过序列)

oracle怎么实现id自增和设置主键啊

Oracle实现自增方式:序列+触发器