当前位置:网站首页>hibernate操作oracle数据库 主键自增
hibernate操作oracle数据库 主键自增
2022-06-27 11:56:00 【瑾琳】
相信使用过mysql,sql server,oracle的朋友都知道,oracle中的表的主键如果想设置成自增长,那么需要创建序列
在oracle中为
create table Student(
Student_ID number(6) NOT NULL PRIMARY KEY,
Student_Name varchar2(10) NOT NULL,
Student_Age number(2) NOT NULL
);
CREATE SEQUENCE student_sequence
INCREMENT BY 1
NOMAXVALUE
NOCYCLE
CACHE 10;
insert into Student values(student_sequence.nextval,'aa',20);
此时数据库中会保存会插入一条数据id为(2,aa,20)的数据
有了这些知识再加上hibernate对数据库的操作,我们就可以开始写代码了
无注解版的student JavaBean类为
package com.bean;
public class Student
{
private int student_id;
private String student_name;
private int student_age;
public int getStudent_id()
{
return student_id;
}
public String getStudent_name()
{
return student_name;
}
public int getStudent_age()
{
return student_age;
}
public void setStudent_id(int id)
{
this.student_id = id;
}
public void setStudent_name(String name)
{
this.student_name = name;
}
public void setStudent_age(int age)
{
this.student_age = age;
}
}
对应的student.hbm.xml文件核心代码
<class name="Student" table="Student">
<id name="student_id" column="student_id" type="java.lang.Integer">
<generator class="native">
<param name="sequence">student_sequence</param>
</generator>
</id>
<property name="student_name" column="Student_Name" type="java.lang.String"/>
<property name="student_age" column="Student_Age" type="java.lang.Integer"/>
</class>
注解版实体类代码
package com.bean;
import javax.persistence.*;
@Entity
@Table(name="student")
public class StudentAnnotation {
private int student_id;
private String student_name;
private int student_age;
@Id
@SequenceGenerator(name="generator",sequenceName="student_sequence")
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="generator")
/*@Id
@GenericGenerator(name = "idGenerator", strategy = "native")
@GeneratedValue(generator = "idGenerator") 有网友说这种方式也可以,但是实际上错误*/
public int getStudent_id()
{
return student_id;
}
@Column
public String getStudent_name()
{
return student_name;
}
@Column
public int getStudent_age()
{
return student_age;
}
public void setStudent_id(int id)
{
this.student_id = id;
}
public void setStudent_name(String name)
{
this.student_name = name;
}
public void setStudent_age(int age)
{
this.student_age = age;
}
}
当然,配置文件hibernate.cfg.xml代码如下,oracle数据库为11g
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property><!--数据库连接url-->
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:orcl</property>
<property name="connection.username">go</property>
<property name="connection.password">go</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- <mapping resource="com/bean/Student.hbm.xml"/> --> <!-- 使用*.hbm.xml请保留该行,否则注释 -->
<mapping class="com.bean.StudentAnnotation" /> <!-- 使用注解方式请保留该行,否则注释 -->
</session-factory>
</hibernate-configuration>
最后就测试类了
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
//import com.bean.Student; //无注解
import com.bean.StudentAnnotation; //有注解
public class Test {
public static void main(String[] args) {
try {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
StudentAnnotation stu = new StudentAnnotation(); //有注解
//Student stu = new Student(); //无注解
stu.setStudent_name("zhangsan");
stu.setStudent_age(18);
session.save(stu);
tx.commit();
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试后,数据库中应该会有zhagnsan的学生信息
测试的数据库驱动为ojdbc-14的版本,测试的时候,会出现一条INFO信息,
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
忽略即可,网上说改为ojdbc-6的版本就没问题了,正解,但是数据库中数据为
https://blog.csdn.net/thepeakofmountain/article/details/17173715
边栏推荐
- MIT6.031 软件构造 Reading7阅读笔记Designing Specifications(设计规范)
- 千万不要错过,新媒体运营15个宝藏公众号分享
- master公式
- 2022CISCN华中 Web
- [tcapulusdb knowledge base] tcapulusdb system user group introduction
- MapReduce principle analysis (in-depth source code)
- Getting started with go web programming: validators
- [high frequency interview questions] difficulty 1.5/5, LCS template questions
- Interview shock 60: what will cause MySQL index invalidation?
- 一个有趣的网络掩码的实验
猜你喜欢

Nvme2.0 protocol - new features

Drive to APasS! Use Mingdao cloud to manage F1 events

Redis distributed lock 15 ask, what have you mastered?

【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(二)

How to adjust an integer that is entered in Excel but always displays decimals?

Wait, how do I use setmemorylimit?

I.MX6ULL启动方式

Private dry goods sharing: how to implement platform in Enterprise Architecture

怎么找相同台词的影视片段?这8个电影搜索神器,一句台词找到对应片段

干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
随机推荐
Topic37——64. 最小路径和
Go Web 编程入门:验证器
Talk about go language and cloud native technology
Hands on API development
Take stock of some easy-to-use and niche markdown editors
Drive to APasS! Use Mingdao cloud to manage F1 events
手把手带你入门 API 开发
Maximum path and problem (cherry picking problem)
面试突击60:什么情况会导致 MySQL 索引失效?
最大路径和问题(摘樱桃问题)
树莓派 3b+ 学习
PyQt,PySide-槽函数被执行了两次
C/s architecture
Peak store app imitation station development play mode explanation source code sharing
进程间通信详解
如何修改 node_modules 裏的文件
自学ADT和OOP
Llvm family (1) - Introduction to llvm
Shell script learning notes
想学好C语言,操作符也很重要