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;
    }

}

 

转载于:https://www.cnblogs.com/Roni-i/p/9975141.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值