当前位置:网站首页>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
边栏推荐
- Interview shock 60: what will cause MySQL index invalidation?
- I.MX6ULL启动方式
- The DBSCAN function of FPC package in R language performs density clustering analysis on data, and the plot function visualizes the clustering graph
- Topic38——56. 合并区间
- Salesforce 容器化 ISV 场景下的软件供应链安全落地实践
- Minimum editing distance (linear DP writing method)
- 旭日3SDB,安装原版ros
- R语言fpc包的dbscan函数对数据进行密度聚类分析、plot函数可视化聚类图
- 树莓派 3b+ 学习
- MapReduce principle analysis (in-depth source code)
猜你喜欢

今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献

Drive to APasS!使用明道云管理F1赛事

56. Core principle of flutter - flutter startup process and rendering pipeline

动态规划【四】(计数类dp)例题:整数划分

优博讯出席OpenHarmony技术日,全新打造下一代安全支付终端

How to find the movie and TV clips with the same lines? These 8 movies search for artifact, and find the corresponding segment in one line
![[tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area](/img/b7/2358e8cf1cdaeaba77e52d04cc74d4.png)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area
![Dynamic programming [4] (counting class DP) example: integer partition](/img/06/4b3863bbb85582348c6f4ea7c5511e.png)
Dynamic programming [4] (counting class DP) example: integer partition

Topic37——64. 最小路径和

Online bidding of Oracle project management system
随机推荐
Deep understanding of happens before principle
Fork/Join 框架基本使用和原理
Interviewer: with the for loop, why do you need foreach?
R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, uses step function to realize stepwise re
一个有趣的网络掩码的实验
Youboxun attended the openharmony technology day to create a new generation of secure payment terminals
Excel中输入整数却总是显示小数,如何调整?
In 2021, the global professional liability insurance revenue was about USD 44740million, and it is expected to reach USD 55980million in 2028. From 2022 to 2028, the CAGR was 3.5%
.NET6接入Skywalking链路追踪完整流程
解开C语言的秘密《关键字》(第六期)
Comment modifier Node Fichiers dans les modules
Minimum editing distance (linear DP writing method)
Rxjs mergeMap 的使用场合
Time management understood after being urged to work at home
C # WPF realizes undo redo function
[tcapulusdb knowledge base] Introduction to tmonitor system upgrade
干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
Building crud applications in golang
面试突击60:什么情况会导致 MySQL 索引失效?
alibaba jarslink